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/plus/selectHolder/index.tsx
var selectHolder_exports = {};
__export(selectHolder_exports, {
  HolderList: () => import_HolderList2.default,
  PREFIX: () => PREFIX,
  default: () => selectHolder_default
});
module.exports = __toCommonJS(selectHolder_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_ErrorTip = __toESM(require("./components/ErrorTip"));
var import_HolderItem = __toESM(require("./components/HolderItem"));
var import_HolderList = __toESM(require("./components/HolderList"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_locales = __toESM(require("./locales"));
var import_index = require("./index.less");
var import_HolderList2 = __toESM(require("./components/HolderList"));
var PREFIX = "pisell-lowcode-select-holder";
var PisellSelectHolder = ({
  dataSource,
  open = true,
  onClose,
  onConfirm,
  onSkip,
  onAdd,
  value,
  modalConfig,
  displayConfig,
  allowAdd = true,
  horizontalGap = 16,
  verticalGap = 16,
  maxSelectedCount = 2,
  isInline = false,
  className,
  style
}) => {
  var _a, _b;
  const [errorTip, setErrorTip] = (0, import_react.useState)("");
  const [selectedValues, setSelectedValues] = (0, import_react.useState)([]);
  const mobile = (0, import_react.useMemo)(() => (0, import_utils.isMobile)(), []);
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  (0, import_react.useEffect)(() => {
    let _value = [];
    if (Array.isArray(value)) {
      _value = value;
    } else {
      _value = value ? [value] : [];
    }
    setSelectedValues(_value);
  }, [value]);
  (0, import_react.useEffect)(() => {
    if ((displayConfig == null ? void 0 : displayConfig.selectType) !== "single") {
      return;
    }
    if (selectedValues.length > 0) {
      return;
    }
    if (value !== void 0 && value !== null && (value == null ? void 0 : value.length) > 0) {
      return;
    }
    if (Array.isArray(dataSource) && dataSource.length > 0) {
      const firstValidOption = dataSource.find(
        (item) => (item == null ? void 0 : item.value) !== null && (item == null ? void 0 : item.value) !== void 0
      );
      if (firstValidOption) {
        handleClick(firstValidOption);
      }
    }
  }, [dataSource, displayConfig == null ? void 0 : displayConfig.selectType, selectedValues.length, value]);
  const handleOk = (0, import_react.useCallback)(() => {
    onConfirm == null ? void 0 : onConfirm(selectedValues);
  }, [selectedValues]);
  const handleClick = (0, import_react.useCallback)((item) => {
    if (!(item == null ? void 0 : item.value)) {
      return onAdd == null ? void 0 : onAdd();
    }
    const selectType = (displayConfig == null ? void 0 : displayConfig.selectType) || "single";
    if (selectType === "single") {
      setSelectedValues((prev) => {
        return prev.includes(item.value) ? [] : [item.value];
      });
    } else {
      setSelectedValues((prev) => {
        const newValues = prev.includes(item.value) ? prev.filter((v) => v !== item.value) : [...prev, item.value];
        return newValues;
      });
    }
  }, [displayConfig == null ? void 0 : displayConfig.selectType]);
  const handleSkip = (0, import_react.useCallback)(() => {
    onSkip == null ? void 0 : onSkip();
  }, [onSkip]);
  const addHolderLabel = import_utils.locales.getText("pisell-select-holder-create");
  const renderAddItem = (0, import_react.useCallback)(() => {
    if (!allowAdd) return void 0;
    return /* @__PURE__ */ import_react.default.createElement(
      import_HolderItem.default,
      {
        item: {
          label: addHolderLabel,
          value: null
        },
        isAddItem: true
      }
    );
  }, [allowAdd]);
  const modalClassName = (0, import_react.useMemo)(() => {
    const classes = [`${PREFIX}-modal`];
    if (mobile) {
      classes.push(`${PREFIX}-modal-mobile`);
    }
    return classes.join(" ");
  }, [mobile]);
  const okButtonProps = (0, import_react.useMemo)(() => {
    if ((displayConfig == null ? void 0 : displayConfig.selectType) === "multiple") {
      return {
        disabled: (selectedValues == null ? void 0 : selectedValues.length) > maxSelectedCount
      };
    }
    return {
      disabled: !(selectedValues == null ? void 0 : selectedValues.length)
    };
  }, [selectedValues, maxSelectedCount, displayConfig == null ? void 0 : displayConfig.selectType]);
  const footerCom = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-footer` }, (displayConfig == null ? void 0 : displayConfig.isSkipable) ? /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { onClick: handleSkip }, import_utils.locales.getText("pisell-select-holder-skip")) : null, /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { onClick: handleOk, ...okButtonProps, type: "primary" }, modalConfig == null ? void 0 : modalConfig.confirmText));
  }, [displayConfig == null ? void 0 : displayConfig.isSkipable, modalConfig == null ? void 0 : modalConfig.confirmText, okButtonProps]);
  const renderContent = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, mobile && (modalConfig == null ? void 0 : modalConfig.title) ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-modal-title` }, modalConfig == null ? void 0 : modalConfig.title) : null, errorTip && /* @__PURE__ */ import_react.default.createElement(import_ErrorTip.default, { text: errorTip }), (displayConfig == null ? void 0 : displayConfig.selectType) === "multiple" && maxSelectedCount ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `${PREFIX}-selected-info ${selectedValues.length === maxSelectedCount ? `${PREFIX}-selected-info-max` : ""}`
      },
      import_utils.locales.getText("pisell-select-holder-selected"),
      selectedValues.length,
      "/",
      maxSelectedCount,
      selectedValues.length > maxSelectedCount ? /* @__PURE__ */ import_react.default.createElement("span", null, " ", import_utils.locales.getText("pisell-select-holder-please-remove")(selectedValues.length - maxSelectedCount)) : null
    ) : null, /* @__PURE__ */ import_react.default.createElement(
      import_HolderList.default,
      {
        dataSource,
        selectedValues,
        displayConfig,
        renderAddItem,
        columns: mobile ? 2 : 3,
        horizontalGap,
        verticalGap,
        allowAdd,
        onClick: handleClick
      }
    ));
  }, [
    isInline,
    modalConfig,
    dataSource,
    errorTip,
    maxSelectedCount,
    selectedValues,
    displayConfig,
    renderAddItem,
    mobile,
    horizontalGap,
    verticalGap,
    allowAdd,
    handleClick
  ]);
  if (isInline) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-inline-wrap ${className}`, style }, renderContent);
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      title: mobile ? "" : modalConfig == null ? void 0 : modalConfig.title,
      platform: mobile ? "h5" : "pc",
      closable: modalConfig == null ? void 0 : modalConfig.showClose,
      bodyStyle: { padding: mobile ? "0 16px 16px" : 20 },
      mobileModalHeight: "auto",
      destroyOnClose: true,
      className: modalClassName,
      headerDivider: false,
      width: mobile ? "100%" : "640px",
      footer: footerCom,
      onCancel: onClose
    },
    renderContent
  );
};
var selectHolder_default = PisellSelectHolder;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HolderList,
  PREFIX
});
