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/SelectHolder/index.tsx
var SelectHolder_exports = {};
__export(SelectHolder_exports, {
  default: () => SelectHolder_default
});
module.exports = __toCommonJS(SelectHolder_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_iconfont = __toESM(require("../../../../../iconfont"));
var import_index = require("./index.less");
var SelectHolder = ({
  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);
  (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);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `pisell-lowcode-product-card-select-holder ${isError ? "pisell-lowcode-product-card-select-holder-error" : ""}`
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        open,
        onDropdownVisibleChange: setOpen,
        onClick: (e) => e.stopPropagation(),
        mode: holderMode,
        size: "large",
        showSearch: true,
        allowClear,
        onClear: () => onChange(""),
        value: selectedValue,
        placeholder: import_utils.locales.getText(
          "pisell2.product.card.add.holder.placeholder"
        )(addHolderButtonText),
        onChange: handleChange,
        options: lists.map((item) => ({
          label: item == null ? void 0 : item.label,
          value: item == null ? void 0 : item.id
        })),
        dropdownRender: (menu) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, menu, isShowAddHolderButton ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { style: { margin: "8px 0" } }), /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            onClick: addItem,
            className: "select-holder-add-btn-wrap",
            style: {
              display: "flex",
              alignContent: "center",
              padding: "4px 10px 10px 8px",
              gap: "8px",
              flex: "1 0 0",
              color: "var(--Primary-600, #7f56d9)",
              fontSize: "14px",
              fontWeight: 600,
              lineHeight: "20px",
              cursor: "pointer"
            }
          },
          /* @__PURE__ */ import_react.default.createElement(
            import_iconfont.default,
            {
              type: "pisell2-plus-circle",
              style: { fontSize: "20px" }
            }
          ),
          /* @__PURE__ */ import_react.default.createElement(
            "span",
            {
              style: {
                flex: 1,
                display: "inline-block",
                maxWidth: "100%",
                whiteSpace: "nowrap",
                overflow: "hidden",
                textOverflow: "ellipsis",
                verticalAlign: "middle"
              }
            },
            import_utils.locales.getText(
              "pisell2.product.card.add.holder.button.text"
            )(addHolderButtonText)
          )
        )) : null)
      }
    ),
    isError ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-holder-error" }, import_utils.locales.getText("pisell2.product.card.add.holder.placeholder")(
      addHolderButtonText
    )) : null
  );
};
var SelectHolder_default = SelectHolder;
