function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 { locales } from '@pisell/utils';
import PisellModal from "../../../../../pisellModal";
import PisellCustomCheckboxGroup from "../../../../../pisellCustomCheckboxGroup";
import PisellCard from "../../../../../pisellCard";
import PisellAvatar from "../../../../../pisellAvatar";
import { Button, Checkbox } from 'antd';
import Iconfont from "../../../../../iconfont";
import "./index.less";
var SelectHolderModal = function SelectHolderModal(props) {
  var lists = props.lists,
    value = props.value,
    visible = props.visible,
    holderMaxCount = props.holderMaxCount,
    addHolderButtonText = props.addHolderButtonText,
    onClose = props.onClose,
    onAdd = props.onAdd,
    onConfirm = props.onConfirm;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selectedValue = _useState2[0],
    setSelectedValue = _useState2[1];
  var renderItem = function renderItem(item) {
    return /*#__PURE__*/React.createElement(PisellCard, {
      key: item.id,
      padding: 12,
      selected: selectedValue.includes(item.id)
    }, /*#__PURE__*/React.createElement("div", {
      className: "select-holder-modal-card"
    }, /*#__PURE__*/React.createElement(Checkbox, {
      checked: selectedValue.includes(item.id)
    }), /*#__PURE__*/React.createElement(PisellAvatar, {
      src: item.avatar,
      size: "lg",
      useTextAvatar: true,
      className: "select-holder-modal-add-item-avatar"
    }, /*#__PURE__*/React.createElement("span", {
      className: "select-holder-name"
    }, item.label)), /*#__PURE__*/React.createElement("span", {
      className: "select-holder-name"
    }, item.label)));
  };
  var handleConfirm = function handleConfirm() {
    onConfirm(selectedValue);
    onClose();
  };
  useEffect(function () {
    setSelectedValue(value || []);
  }, [value, visible]);
  var handleClick = function handleClick(value) {
    if (value.id === 0) {
      onAdd();
      return;
    }
    setSelectedValue(function (v) {
      if (v.some(function (v) {
        return v === value.id;
      })) {
        return v.filter(function (v) {
          return v !== value.id;
        });
      }
      if (v.length >= holderMaxCount) {
        return v;
      }
      return [].concat(_toConsumableArray(v), [value.id]);
    });
  };
  var handleClear = function handleClear() {
    setSelectedValue([]);
  };
  var handleApply = function handleApply() {
    onConfirm(selectedValue);
    onClose();
  };
  var title = useMemo(function () {
    return /*#__PURE__*/React.createElement("span", null, "".concat(locales.getText('pisell2.product.card.add.holder.modal.title.1'), " "), /*#__PURE__*/React.createElement("span", {
      className: "select-holder-modal-title-count"
    }, (selectedValue === null || selectedValue === void 0 ? void 0 : selectedValue.length) || 0, "/", holderMaxCount), " ".concat(addHolderButtonText));
  }, [selectedValue, holderMaxCount, addHolderButtonText]);
  var renderAddItem = function renderAddItem(item) {
    return /*#__PURE__*/React.createElement(PisellCard, {
      key: item.id,
      padding: 12
    }, /*#__PURE__*/React.createElement("div", {
      className: "select-holder-modal-card"
    }, /*#__PURE__*/React.createElement(PisellAvatar, {
      src: item.avatar,
      size: "lg",
      useTextAvatar: true,
      className: "select-holder-modal-add-item-avatar",
      style: {
        backgroundColor: 'var(--Pisell-, #EEE5FF)'
      }
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-plus",
      style: {
        fontSize: '24px'
      }
    })), /*#__PURE__*/React.createElement("span", {
      className: "select-holder-name"
    }, locales.getText('pisell2.product.card.add.holder.modal.add'))));
  };
  return /*#__PURE__*/React.createElement("div", {
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement(PisellModal, {
    open: visible,
    onCancel: onClose,
    onOk: handleConfirm,
    width: 780,
    footer: null,
    title: title,
    headerDivider: false
  }, /*#__PURE__*/React.createElement("div", {
    className: "select-holder-modal-content"
  }, /*#__PURE__*/React.createElement(PisellCustomCheckboxGroup, {
    className: "select-holder-modal-checkbox-group",
    allowAdd: true,
    dataSource: lists,
    renderItem: renderItem,
    renderAddItem: renderAddItem,
    value: selectedValue,
    isMultiple: true,
    columns: 3,
    horizontalGap: 16,
    verticalGap: 16,
    onClick: handleClick
  }), /*#__PURE__*/React.createElement("div", {
    className: "select-holder-modal-footer"
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
    size: "large",
    onClick: onClose,
    className: "select-holder-modal-cancel-button"
  }, locales.getText('pisell2.product.card.add.holder.modal.cancel'))), /*#__PURE__*/React.createElement("div", {
    className: "select-holder-modal-footer-right"
  }, /*#__PURE__*/React.createElement(Button, {
    size: "large",
    onClick: handleClear,
    className: "select-holder-modal-clear-button"
  }, locales.getText('pisell2.product.card.add.holder.modal.clear')), /*#__PURE__*/React.createElement(Button, {
    size: "large",
    type: "primary",
    onClick: handleApply,
    className: "select-holder-modal-apply-button"
  }, locales.getText('pisell2.product.card.add.holder.modal.apply')))))));
};
export default SelectHolderModal;