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/plus/salesSdk/components/SalesSdkSelectHolderModal.tsx
var SalesSdkSelectHolderModal_exports = {};
__export(SalesSdkSelectHolderModal_exports, {
  SalesSdkSelectHolderModal: () => SalesSdkSelectHolderModal
});
module.exports = __toCommonJS(SalesSdkSelectHolderModal_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_utils2 = require("../../../components/booking/utils");
var import_selectHolder = __toESM(require("../../../plus/selectHolder"));
function buildHolderOptions(holders) {
  return holders.map((item) => {
    const id = (item == null ? void 0 : item.id) || (item == null ? void 0 : item.form_record_id) || (item == null ? void 0 : item.value);
    if (id === void 0 || id === null) return null;
    return {
      label: (item == null ? void 0 : item.main_field) || (item == null ? void 0 : item.label) || (item == null ? void 0 : item.name) || import_utils.locales.getText("pisell-select-holder-please-select"),
      value: id,
      avatar: item == null ? void 0 : item.avatar,
      subtitle: (item == null ? void 0 : item.subtitle) || (item == null ? void 0 : item.second_field) || (item == null ? void 0 : item.sub_field)
    };
  }).filter(Boolean);
}
function buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount) {
  var _a, _b;
  const selectType = maxSelectedCount > 1 ? "multiple" : "single";
  return {
    direction: "vertical",
    selectType,
    showAvatar: true,
    showTitle: true,
    showSubtitle: Boolean(holderOptions.some((option) => option.subtitle)),
    isSkipable: ((_b = (_a = bookingConfig == null ? void 0 : bookingConfig.config) == null ? void 0 : _a.basic) == null ? void 0 : _b.subject_require) === "optional"
  };
}
var SalesSdkSelectHolderModal = ({
  open,
  holders,
  bookingConfig,
  customerId,
  initialValue,
  maxSelectedCount = 1,
  onAddHolder,
  onConfirm,
  onCancel,
  onSkip
}) => {
  var _a;
  const [innerOpen, setInnerOpen] = (0, import_react.useState)(open);
  const holderOptions = (0, import_react.useMemo)(() => buildHolderOptions(holders), [holders]);
  const [selectedValues, setSelectedValues] = (0, import_react.useState)(() => {
    if (Array.isArray(initialValue)) {
      return initialValue.filter((item) => item !== void 0 && item !== null);
    }
    if (initialValue === void 0 || initialValue === null || initialValue === "") {
      return [];
    }
    return [initialValue];
  });
  (0, import_react.useEffect)(() => {
    setInnerOpen(open);
  }, [open]);
  const holderConfig = (bookingConfig == null ? void 0 : bookingConfig.holder) || {};
  const title = ((_a = holderConfig == null ? void 0 : holderConfig.form) == null ? void 0 : _a.title) || (holderConfig == null ? void 0 : holderConfig.title) || import_utils.locales.getText("pisell2.text.holder");
  const displayConfig = (0, import_react.useMemo)(
    () => buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount),
    [bookingConfig, holderOptions, maxSelectedCount]
  );
  const closeWith = (fn) => {
    setInnerOpen(false);
    setTimeout(fn, 300);
  };
  const handleClose = () => closeWith(onCancel);
  const handleConfirm = (nextValues) => {
    closeWith(() => {
      if (nextValues == null ? void 0 : nextValues.length) {
        onConfirm(nextValues.map((val) => Number(val)));
      } else {
        onCancel();
      }
    });
  };
  const handleSkip = () => {
    if (onSkip) {
      closeWith(onSkip);
    } else {
      handleClose();
    }
  };
  const allowAdd = Boolean(onAddHolder) && !(0, import_utils2.isWalkIn)(customerId);
  return /* @__PURE__ */ import_react.default.createElement(
    import_selectHolder.default,
    {
      open: innerOpen,
      modalConfig: {
        title: `${import_utils.locales.getText("pisell-select-holder-please-select")} ${maxSelectedCount || 1} ${title}`,
        confirmText: import_utils.locales.getText("pisell2.text.confirm")
      },
      dataSource: holderOptions,
      value: selectedValues,
      displayConfig,
      maxSelectedCount,
      allowAdd,
      onClose: handleClose,
      onConfirm: handleConfirm,
      onAdd: onAddHolder,
      onSkip: handleSkip
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesSdkSelectHolderModal
});
