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/translation/index.tsx
var translation_exports = {};
__export(translation_exports, {
  default: () => translation_default
});
module.exports = __toCommonJS(translation_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_icons = require("@ant-design/icons");
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_utils2 = require("./utils");
var import_index = require("./index.less");
var defaultGlobal = {
  shopLanguages: {
    primary: "en",
    list: [
      {
        id: 1,
        code: "zh-CN",
        text: "简体中文",
        primary: 1,
        status: "published"
      },
      {
        id: 2,
        code: "en",
        text: "English",
        primary: 0,
        status: "published"
      },
      {
        id: 3,
        code: "zh-HK",
        text: "繁體中文",
        primary: 0,
        status: "published"
      }
    ]
  },
  primary_locale: "en"
};
var defaultObj = {};
var Translation = (props) => {
  var _a, _b;
  const { value = defaultObj, onChange, ...others } = props;
  const context = (0, import_useEngineContext.default)();
  const [open, setOpen] = (0, import_react.useState)(false);
  const [_value, set_value] = (0, import_react.useState)(value);
  const store = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.store;
  const { shopLanguages } = ((_b = store == null ? void 0 : store.getState()) == null ? void 0 : _b.global) || defaultGlobal;
  (0, import_react.useEffect)(() => {
    let _newValue = { ...value };
    if ((0, import_utils.isString)(value)) {
      _newValue = (0, import_utils2.createTranslationText)(value);
    }
    set_value({ ...defaultValue, ..._newValue });
  }, [value]);
  const currentLocale = (0, import_react.useMemo)(() => {
    return shopLanguages.primary;
  }, [shopLanguages.primary]);
  const inputValue = (0, import_react.useMemo)(() => {
    if ((0, import_utils.isString)(value)) {
      return value;
    }
    return value[currentLocale];
  }, [value, currentLocale]);
  const languageList = (0, import_react.useMemo)(() => {
    return shopLanguages.list;
  }, [shopLanguages.list]);
  const defaultValue = (0, import_react.useMemo)(() => {
    let obj = {};
    for (let item in import_utils2.locales) {
      obj[item] = "";
    }
    return obj;
  }, []);
  const allChange = (e) => {
    let newValue = {
      ...defaultValue,
      ...value || {}
    };
    if ((0, import_utils2.checkIfAllValuesAreEqual)(newValue)) {
      for (let key in newValue) {
        newValue[key] = e || "";
      }
    } else {
      newValue[currentLocale] = e || "";
    }
    onChange == null ? void 0 : onChange(newValue);
  };
  const isActive = (0, import_react.useMemo)(() => {
    let bol = false;
    for (let key in value) {
      if (key !== currentLocale && value[key]) {
        bol = true;
      }
    }
    return bol;
  }, [value, currentLocale]);
  const _onOk = () => {
    onChange == null ? void 0 : onChange(_value);
    setOpen(false);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Input,
    {
      ...others,
      value: inputValue,
      onChange: (e) => allChange(e.target.value),
      suffix: /* @__PURE__ */ import_react.default.createElement(
        import_icons.GlobalOutlined,
        {
          className: (0, import_classnames.default)("pisell-lowcode-translation-icon", {
            active: isActive
          }),
          onClick: () => setOpen(true)
        }
      )
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      open,
      onOk: _onOk,
      onCancel: () => {
        set_value(value || defaultValue);
        setOpen(false);
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-translation-modal" }, languageList.map((d) => {
      return /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: d.text, labelCol: { span: 24 }, key: d.code }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Input,
        {
          value: _value[d.code],
          onChange: (e) => {
            set_value({ ..._value, [d.code]: e.target.value });
          }
        }
      ));
    }))
  ));
};
var translation_default = Translation;
