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, useState } from 'react';
import SelectHolder from "../selectHolder";
import { locales } from '@pisell/utils';
import { useHolder, useSales, useStep, useCart } from "../../../Sales";
var SelectHolderModal = function SelectHolderModal(props) {
  var _holderData$holderLis;
  var open = props.open;
  var _useSales = useSales(),
    data = _useSales.data;
  var _useHolder = useHolder(),
    onHolderClose = _useHolder.onHolderClose,
    onOpenCreateHolder = _useHolder.onOpenCreateHolder,
    selectHolder = _useHolder.selectHolder,
    holderData = _useHolder.holderData,
    updateHolderList = _useHolder.updateHolderList,
    defaultSelectedHolder = _useHolder.defaultSelectedHolder;
  var _useStep = useStep(),
    nextStep = _useStep.nextStep,
    prevStep = _useStep.prevStep,
    currentStepData = _useStep.currentStepData;
  var _useState = useState(defaultSelectedHolder),
    _useState2 = _slicedToArray(_useState, 2),
    selectedHolder = _useState2[0],
    setSelectedHolder = _useState2[1];
  var _useCart = useCart(),
    onCloseCart = _useCart.onCloseCart;
  useEffect(function () {
    setSelectedHolder(defaultSelectedHolder);
  }, [defaultSelectedHolder]);

  /**
   * @title: 点击holder卡片
   * @description:
   * @param {any} e
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-16 22:29
   */
  var handleHolder = function handleHolder(e) {
    var _data$initData;
    // 当前选中的holder
    if (!(e !== null && e !== void 0 && e.isAddCard)) {
      setSelectedHolder(e);
      // return onSelect(e);
      return;
    }

    // 添加holder
    onOpenCreateHolder({
      form_id: (_data$initData = data.initData) === null || _data$initData === void 0 || (_data$initData = _data$initData.boardSetting) === null || _data$initData === void 0 || (_data$initData = _data$initData.config) === null || _data$initData === void 0 || (_data$initData = _data$initData.basic) === null || _data$initData === void 0 || (_data$initData = _data$initData.appointment_subject) === null || _data$initData === void 0 ? void 0 : _data$initData.form_id,
      form_title: locales.getText('pisell2.event.create.form'),
      callback: function callback(res) {
        updateHolderList(res === null || res === void 0 ? void 0 : res.values);
      }
    });
  };
  return /*#__PURE__*/React.createElement(SelectHolder, {
    open: open,
    options: (holderData === null || holderData === void 0 ? void 0 : holderData.holderList) || [],
    onCancel: function onCancel() {
      return onHolderClose();
    },
    value: selectedHolder === null || selectedHolder === void 0 ? void 0 : selectedHolder.id,
    title: locales.getText('pisell2.event.holder.modal.title'),
    okText: locales.getText('pisell2.event.confirm'),
    onClick: function onClick(e) {
      return handleHolder(e);
    },
    disabled: !(holderData !== null && holderData !== void 0 && (_holderData$holderLis = holderData.holderList) !== null && _holderData$holderLis !== void 0 && _holderData$holderLis.length) || !(selectedHolder !== null && selectedHolder !== void 0 && selectedHolder.id),
    errorTipText: locales.getText('pisell2.event.error.holder'),
    onOk: function onOk() {
      onCloseCart();
      var isEvent = (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === 'select-event';
      selectHolder(selectedHolder, isEvent ? 'addToCart' : 'createCartItem');
      if (isEvent) {
        nextStep();
      } else {
        prevStep();
      }
      onHolderClose();
    }
  });
};
export default SelectHolderModal;