function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState } from 'react';
import { Select, Divider } from 'antd';
import { isArr, locales } from '@pisell/utils';
import Iconfont from "../../../../../iconfont";
import "./index.less";
var SelectHolder = function SelectHolder(_ref) {
  var lists = _ref.lists,
    value = _ref.value,
    onChange = _ref.onChange,
    holderMode = _ref.holderMode,
    isShowAddHolderButton = _ref.isShowAddHolderButton,
    onAddHolder = _ref.onAddHolder,
    addHolderButtonText = _ref.addHolderButtonText,
    onClearHolder = _ref.onClearHolder,
    isErrorHolder = _ref.isErrorHolder,
    _ref$allowClear = _ref.allowClear,
    allowClear = _ref$allowClear === void 0 ? false : _ref$allowClear,
    holderMaxCount = _ref.holderMaxCount;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    selectedValue = _useState2[0],
    setSelectedValue = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    open = _useState4[0],
    setOpen = _useState4[1];
  useEffect(function () {
    setSelectedValue(value);
  }, [value]);

  /**
   * @title: 点击添加holder事件
   * @description:
   * @param {Event} e
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-04-26 10:39
   */
  var addItem = function addItem(e) {
    e.preventDefault();
    e.stopPropagation();
    onAddHolder();
    setOpen(false);
  };
  var isError = useMemo(function () {
    if (!isErrorHolder) {
      return false;
    }
    if (!selectedValue) {
      return true;
    }
    if (isArr(selectedValue) && (selectedValue === null || selectedValue === void 0 ? void 0 : selectedValue.length) !== holderMaxCount) {
      return true;
    }
    return false;
  }, [selectedValue, isErrorHolder]);
  var handleChange = function handleChange(val) {
    var item;
    if (isArr(val)) {
      if (val.length > holderMaxCount) return;
      item = lists.filter(function (d) {
        return val.includes(d.id);
      });
    } else {
      item = lists.find(function (d) {
        return d.id === val;
      });
    }
    setSelectedValue(val);
    onChange(item);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-product-card-select-holder ".concat(isError ? 'pisell-lowcode-product-card-select-holder-error' : '')
  }, /*#__PURE__*/React.createElement(Select, {
    open: open,
    onDropdownVisibleChange: setOpen,
    onClick: function onClick(e) {
      return e.stopPropagation();
    },
    mode: holderMode,
    size: "large",
    showSearch: true,
    allowClear: allowClear,
    onClear: function onClear() {
      return onChange('');
    },
    value: selectedValue,
    placeholder: locales.getText('pisell2.product.card.add.holder.placeholder')(addHolderButtonText),
    onChange: handleChange,
    options: lists.map(function (item) {
      return {
        label: item === null || item === void 0 ? void 0 : item.label,
        value: item === null || item === void 0 ? void 0 : item.id
      };
    }),
    dropdownRender: function dropdownRender(menu) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, menu, isShowAddHolderButton ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Divider, {
        style: {
          margin: '8px 0'
        }
      }), /*#__PURE__*/React.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__*/React.createElement(Iconfont, {
        type: "pisell2-plus-circle",
        style: {
          fontSize: '20px'
        }
      }), /*#__PURE__*/React.createElement("span", {
        style: {
          flex: 1,
          display: 'inline-block',
          maxWidth: '100%',
          whiteSpace: 'nowrap',
          overflow: 'hidden',
          textOverflow: 'ellipsis',
          verticalAlign: 'middle'
        }
      }, locales.getText('pisell2.product.card.add.holder.button.text')(addHolderButtonText)))) : null);
    }
  }), isError ? /*#__PURE__*/React.createElement("div", {
    className: "select-holder-error"
  }, locales.getText('pisell2.product.card.add.holder.placeholder')(addHolderButtonText)) : null);
};
export default SelectHolder;