var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/workSpaceList/components/EditableString/index.tsx
var EditableString_exports = {};
__export(EditableString_exports, {
  default: () => EditableString_default
});
module.exports = __toCommonJS(EditableString_exports);
var import_icon = require("@pisell/icon");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_index = require("./index.less");
var EditableString = (props) => {
  const {
    text,
    style,
    onChange,
    editable,
    editing: editingProps,
    maxLength,
    className,
    ellipsis
  } = props;
  const [editing, setEditing] = (0, import_react.useState)(false);
  const [originText, setOriginText] = (0, import_react.useState)(text);
  const [temporaryValue, setTemporaryValue] = (0, import_react.useState)(text);
  const inputRef = (0, import_react.useRef)(null);
  const textRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    setOriginText(text);
    setTemporaryValue(text);
  }, [text]);
  (0, import_react.useEffect)(() => {
    (0, import_utils.isBoolean)(editingProps) && setEditing(editingProps);
  }, [editingProps]);
  (0, import_react.useEffect)(() => {
    if (editing) {
      setTimeout(() => {
        var _a;
        (_a = inputRef.current) == null ? void 0 : _a.focus();
      });
    }
  }, [editing]);
  (0, import_react.useEffect)(() => {
    if (textRef.current) {
      textRef.current.style.webkitLineClamp = ellipsis == null ? void 0 : ellipsis.row;
    }
  }, [ellipsis, editing]);
  const handleChangeText = () => {
    if (!temporaryValue) return;
    onChange == null ? void 0 : onChange(temporaryValue);
    setEditing(false);
  };
  const handleEdit = () => {
    setEditing(true);
  };
  return (0, import_react.useMemo)(() => {
    if (editing) {
      return /* @__PURE__ */ import_react.default.createElement("span", { style }, /* @__PURE__ */ import_react.default.createElement(
        import_materials.Input,
        {
          ref: inputRef,
          value: temporaryValue,
          onChange: (e) => setTemporaryValue(e.target.value),
          onPressEnter: handleChangeText,
          onBlur: handleChangeText,
          maxLength
        }
      ));
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { style, size: 16, className: (0, import_classnames.default)(className, "edit-wrap") }, /* @__PURE__ */ import_react.default.createElement("span", { className: "text", ref: textRef }, temporaryValue), editable && temporaryValue && /* @__PURE__ */ import_react.default.createElement(import_icon.Edit05, { className: "edit-icon", onClick: handleEdit }));
  }, [editing, temporaryValue, ellipsis]);
};
var EditableString_default = EditableString;
