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 { isWalkIn } from "../../../components/booking/utils";
import PisellSelectHolder from "../../../plus/selectHolder";
function buildHolderOptions(holders) {
  return holders.map(function (item) {
    var id = (item === null || item === void 0 ? void 0 : item.id) || (item === null || item === void 0 ? void 0 : item.form_record_id) || (item === null || item === void 0 ? void 0 : item.value);
    if (id === undefined || id === null) return null;
    return {
      label: (item === null || item === void 0 ? void 0 : item.main_field) || (item === null || item === void 0 ? void 0 : item.label) || (item === null || item === void 0 ? void 0 : item.name) || locales.getText('pisell-select-holder-please-select'),
      value: id,
      avatar: item === null || item === void 0 ? void 0 : item.avatar,
      subtitle: (item === null || item === void 0 ? void 0 : item.subtitle) || (item === null || item === void 0 ? void 0 : item.second_field) || (item === null || item === void 0 ? void 0 : item.sub_field)
    };
  }).filter(Boolean);
}
function buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount) {
  var _bookingConfig$config;
  var selectType = maxSelectedCount > 1 ? 'multiple' : 'single';
  return {
    direction: 'vertical',
    selectType: selectType,
    showAvatar: true,
    showTitle: true,
    showSubtitle: Boolean(holderOptions.some(function (option) {
      return option.subtitle;
    })),
    isSkipable: (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config = bookingConfig.config) === null || _bookingConfig$config === void 0 || (_bookingConfig$config = _bookingConfig$config.basic) === null || _bookingConfig$config === void 0 ? void 0 : _bookingConfig$config.subject_require) === 'optional'
  };
}

/**
 * SalesSdk 专用 Holder 弹窗（从 booking selectHolderModal 复制，props 驱动，无 miniRedux dispatch）。
 */
export var SalesSdkSelectHolderModal = function SalesSdkSelectHolderModal(_ref) {
  var _holderConfig$form;
  var open = _ref.open,
    holders = _ref.holders,
    bookingConfig = _ref.bookingConfig,
    customerId = _ref.customerId,
    initialValue = _ref.initialValue,
    _ref$maxSelectedCount = _ref.maxSelectedCount,
    maxSelectedCount = _ref$maxSelectedCount === void 0 ? 1 : _ref$maxSelectedCount,
    onAddHolder = _ref.onAddHolder,
    onConfirm = _ref.onConfirm,
    onCancel = _ref.onCancel,
    onSkip = _ref.onSkip;
  var _useState = useState(open),
    _useState2 = _slicedToArray(_useState, 2),
    innerOpen = _useState2[0],
    setInnerOpen = _useState2[1];
  var holderOptions = useMemo(function () {
    return buildHolderOptions(holders);
  }, [holders]);
  var _useState3 = useState(function () {
      if (Array.isArray(initialValue)) {
        return initialValue.filter(function (item) {
          return item !== undefined && item !== null;
        });
      }
      if (initialValue === undefined || initialValue === null || initialValue === '') {
        return [];
      }
      return [initialValue];
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    selectedValues = _useState4[0],
    setSelectedValues = _useState4[1];
  useEffect(function () {
    setInnerOpen(open);
  }, [open]);
  var holderConfig = (bookingConfig === null || bookingConfig === void 0 ? void 0 : bookingConfig.holder) || {};
  var title = (holderConfig === null || holderConfig === void 0 || (_holderConfig$form = holderConfig.form) === null || _holderConfig$form === void 0 ? void 0 : _holderConfig$form.title) || (holderConfig === null || holderConfig === void 0 ? void 0 : holderConfig.title) || locales.getText('pisell2.text.holder');
  var displayConfig = useMemo(function () {
    return buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount);
  }, [bookingConfig, holderOptions, maxSelectedCount]);
  var closeWith = function closeWith(fn) {
    setInnerOpen(false);
    setTimeout(fn, 300);
  };
  var handleClose = function handleClose() {
    return closeWith(onCancel);
  };
  var handleConfirm = function handleConfirm(nextValues) {
    closeWith(function () {
      if (nextValues !== null && nextValues !== void 0 && nextValues.length) {
        onConfirm(nextValues.map(function (val) {
          return Number(val);
        }));
      } else {
        onCancel();
      }
    });
  };
  var handleSkip = function handleSkip() {
    if (onSkip) {
      closeWith(onSkip);
    } else {
      handleClose();
    }
  };
  var allowAdd = Boolean(onAddHolder) && !isWalkIn(customerId);
  return /*#__PURE__*/React.createElement(PisellSelectHolder, {
    open: innerOpen,
    modalConfig: {
      title: "".concat(locales.getText('pisell-select-holder-please-select'), " ").concat(maxSelectedCount || 1, " ").concat(title),
      confirmText: locales.getText('pisell2.text.confirm')
    },
    dataSource: holderOptions,
    value: selectedValues,
    displayConfig: displayConfig,
    maxSelectedCount: maxSelectedCount,
    allowAdd: allowAdd,
    onClose: handleClose,
    onConfirm: handleConfirm,
    onAdd: onAddHolder,
    onSkip: handleSkip
  });
};