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/pisellText/components/Editable/index.tsx
var Editable_exports = {};
__export(Editable_exports, {
  default: () => Editable_default
});
module.exports = __toCommonJS(Editable_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icon = __toESM(require("../../../icon"));
var import_utils = require("@pisell/utils");
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_translation = __toESM(require("../../../translation"));
var import_index = require("./index.less");
var Editable = (props) => {
  const {
    value,
    children,
    editable = false,
    onChange,
    className,
    size = "middle",
    ...others
  } = props;
  const context = (0, import_useEngineContext.default)();
  const [isEditing, setIsEditing] = (0, import_react.useState)(false);
  const editableConfig = (0, import_react.useMemo)(() => {
    if (typeof editable === "boolean") {
      return editable ? {} : null;
    }
    return editable || null;
  }, [editable]);
  const currentLocale = (0, import_react.useMemo)(() => {
    var _a, _b;
    const locale = (_b = (_a = context.appHelper.utils) == null ? void 0 : _a.storage) == null ? void 0 : _b.get("umi_locale");
    return import_utils.locales.formatLocale(locale);
  }, []);
  const displayValue = (0, import_react.useMemo)(() => {
    if (typeof children === "string") return children;
    if (typeof value === "string") {
      return value;
    }
    if (value && typeof value === "object") {
      return value[currentLocale] || "";
    }
    return "";
  }, [value, currentLocale, children]);
  const isMultiLanguage = (0, import_react.useMemo)(() => {
    return !!value && typeof value === "object";
  }, [value]);
  const handleEdit = () => {
    if (editableConfig == null ? void 0 : editableConfig.disabled) return;
    setIsEditing(true);
  };
  const handleInputChange = (newValue) => {
    if (!onChange) return;
    onChange(newValue);
  };
  if (isEditing && editableConfig) {
    if (isMultiLanguage) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("pisell-text-editable-editing", className) }, /* @__PURE__ */ import_react.default.createElement(
        import_translation.default,
        {
          value,
          onChange: handleInputChange,
          onPressEnter: () => setIsEditing(false),
          maxLength: editableConfig.maxLength,
          size,
          autoFocus: true
        }
      ));
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Input,
      {
        value: displayValue,
        onChange: (e) => handleInputChange(e.target.value),
        onPressEnter: () => setIsEditing(false),
        maxLength: editableConfig.maxLength,
        size,
        autoFocus: true,
        className: (0, import_classnames.default)("pisell-text-editable-input", className),
        style: others.style
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      className: (0, import_classnames.default)("pisell-text-editable", className, {
        "pisell-text-editable-enabled": editableConfig && !editableConfig.disabled
      }),
      ...others
    },
    displayValue,
    editableConfig && !editableConfig.disabled && !isEditing && /* @__PURE__ */ import_react.default.createElement(
      "span",
      {
        className: "pisell-text-editable-edit-icon",
        onClick: (e) => {
          e.preventDefault();
          e.stopPropagation();
          handleEdit();
        },
        title: "点击编辑"
      },
      editableConfig.icon || /* @__PURE__ */ import_react.default.createElement(import_icon.default, { type: "pisell2-edit-05", size: 16 })
    )
  );
};
var Editable_default = Editable;
