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/SelectResource/index.tsx
var SelectResource_exports = {};
__export(SelectResource_exports, {
  default: () => SelectResource_default
});
module.exports = __toCommonJS(SelectResource_exports);
var import_react = __toESM(require("react"));
var import_AlertTriangle = __toESM(require("@pisell/icon/es/AlertTriangle"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_utils2 = require("@pisell/utils");
var import_index = require("./index.less");
var SelectResource = ({
  lists,
  value,
  onChange,
  resourceMode,
  allowClear = false,
  onClearResource,
  maxLength,
  // 是否显示资源错误
  isErrorResource,
  // 资源错误文案
  resourceErrorText,
  ...props
}) => {
  const [selectedValue, setSelectedValue] = (0, import_react.useState)();
  (0, import_react.useEffect)(() => {
    setSelectedValue(value);
  }, [value]);
  const resourceMap = (0, import_react.useMemo)(() => {
    return (lists || []).reduce((acc, item) => {
      acc[item.id] = item;
      return acc;
    }, {});
  }, [lists]);
  const renderOptions = (0, import_react.useCallback)(() => {
    return (lists || []).map((d) => {
      return /* @__PURE__ */ import_react.default.createElement(import_antd.Select.Option, { value: d.id, key: d.id, ...d || {} }, /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", alignItems: "center", gap: "8px" } }, /* @__PURE__ */ import_react.default.createElement("span", null, d["label"]), (d == null ? void 0 : d.isError) ? /* @__PURE__ */ import_react.default.createElement(import_AlertTriangle.default, { style: { color: "#b42318", cursor: "pointer" } }) : null));
    });
  }, [lists]);
  const handleChange = (0, import_react.useCallback)(
    (val) => {
      if (["multiple", "tags"].includes(resourceMode) && maxLength) {
        if (value && value.length >= maxLength && val.length > value.length) {
          return;
        }
      }
      setSelectedValue(val);
      if (props.isParallelResource) {
        const valueArray = (0, import_utils2.isArr)(val) ? val : [val];
        return onChange({
          value: valueArray,
          options: valueArray.map((v) => resourceMap[v]),
          resourceMode,
          props
        });
      }
      const item = resourceMap[val];
      onChange(item);
    },
    [resourceMode, maxLength, value, onChange, props, resourceMap]
  );
  const isError = (0, import_react.useMemo)(() => {
    if (!selectedValue && isErrorResource) return true;
  }, [selectedValue, isErrorResource]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `pisell-lowcode-product-card-select-resource ${isError ? "pisell-lowcode-product-card-select-resource-error" : ""}`
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        disabled: props.disabled,
        mode: resourceMode,
        size: "large",
        showSearch: true,
        allowClear,
        onClear: () => onChange(""),
        onClick: (e) => e.stopPropagation(),
        style: { flex: 1 },
        value: selectedValue,
        placeholder: import_utils.locales.getText(
          "pisell2.product.card.add.holder.placeholder"
        )(""),
        onChange: handleChange
      },
      renderOptions()
    ),
    isError ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-product-select-resource-error" }, import_utils.locales.getText("pisell2.product.card.add.holder.placeholder")(
      resourceErrorText
    )) : null
  );
};
var SelectResource_default = SelectResource;
