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/productCard/components/Time/components/SelectHolderMultiple/index.tsx
var SelectHolderMultiple_exports = {};
__export(SelectHolderMultiple_exports, {
  default: () => SelectHolderMultiple_default
});
module.exports = __toCommonJS(SelectHolderMultiple_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_SelectHolderModal = __toESM(require("../SelectHolderModal"));
var SelectHolderMultiple = ({
  lists,
  value,
  onChange,
  holderMode,
  isShowAddHolderButton,
  onAddHolder,
  addHolderButtonText,
  onClearHolder,
  isErrorHolder,
  allowClear = false,
  holderMaxCount
}) => {
  const [selectedValue, setSelectedValue] = (0, import_react.useState)();
  const [open, setOpen] = (0, import_react.useState)(false);
  const [selectedHolderModalVisible, setSelectedHolderModalVisible] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    setSelectedValue(value);
  }, [value]);
  const addItem = (e) => {
    e.preventDefault();
    e.stopPropagation();
    onAddHolder();
    setOpen(false);
  };
  const isError = (0, import_react.useMemo)(() => {
    if (!isErrorHolder) {
      return false;
    }
    if (!selectedValue) {
      return true;
    }
    if ((0, import_utils.isArr)(selectedValue) && (selectedValue == null ? void 0 : selectedValue.length) !== holderMaxCount) {
      return true;
    }
    return false;
  }, [selectedValue, isErrorHolder]);
  const handleChange = (val) => {
    let item;
    if ((0, import_utils.isArr)(val)) {
      if (val.length > holderMaxCount) return;
      item = lists.filter((d) => val.includes(d.id));
    } else {
      item = lists.find((d) => d.id === val);
    }
    setSelectedValue(val);
    onChange(item);
  };
  const optionsMap = (0, import_react.useMemo)(() => {
    return (lists || []).reduce((acc, item) => {
      acc[item.id] = item;
      return acc;
    }, {});
  }, [lists]);
  console.log(optionsMap, value, "optionsMap");
  const handleClose = (item) => {
    console.log("item", item, value);
    handleChange(value.filter((d) => d !== item));
  };
  const renderHolder = () => {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, (value || []).map((item) => {
      var _a;
      return /* @__PURE__ */ import_react.default.createElement(
        import_antd.Tag,
        {
          key: item,
          closable: true,
          onClose: () => handleClose(item),
          className: "select-holder-multiple-tag"
        },
        (_a = optionsMap[item]) == null ? void 0 : _a.label
      );
    }), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        onClick: handleEdit,
        type: "text",
        className: "select-holder-multiple-edit-button"
      },
      import_utils.locales.getText("pisell2.product.card.edit")
    ));
  };
  const handleEdit = (e) => {
    e.stopPropagation();
    setSelectedHolderModalVisible(true);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `pisell-lowcode-product-card-select-holder ${isError ? "pisell-lowcode-product-card-select-holder-error" : ""}`,
      onClick: (e) => e.stopPropagation()
    },
    (value == null ? void 0 : value.length) ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-holder-multiple-wrap" }, renderHolder()) : /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", block: true, onClick: handleEdit }, import_utils.locales.getText(
      "pisell2.product.card.add.holder.placeholder.multiple"
    )(holderMaxCount, addHolderButtonText)),
    /* @__PURE__ */ import_react.default.createElement(
      import_SelectHolderModal.default,
      {
        holderMaxCount,
        addHolderButtonText,
        lists,
        value: selectedValue,
        visible: selectedHolderModalVisible,
        onClose: () => setSelectedHolderModalVisible(false),
        onAdd: onAddHolder,
        onConfirm: handleChange
      }
    ),
    isError ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-holder-error" }, `${import_utils.locales.getText(
      "pisell2.product.card.add.holder.modal.title.1"
    )} `, /* @__PURE__ */ import_react.default.createElement("span", null, (value == null ? void 0 : value.length) || 0, "/", holderMaxCount), ` ${addHolderButtonText}`) : null
  );
};
var SelectHolderMultiple_default = SelectHolderMultiple;
