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/components/booking/utils/confirmHolderModal.tsx
var confirmHolderModal_exports = {};
__export(confirmHolderModal_exports, {
  ConfirmHolderModal: () => ConfirmHolderModal,
  showConfirmHolderModal: () => showConfirmHolderModal
});
module.exports = __toCommonJS(confirmHolderModal_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_ahooks = require("ahooks");
var import_Lists = __toESM(require("../info/service/Lists"));
var import_utils2 = require("../info/service2/utils");
var import_selectHolderModal = require("./selectHolderModal");
var import_confirmHolderModal = require("./confirmHolderModal.less");
var import_utils3 = require("../../ticketBooking/utils");
var import_utils4 = require("@pisell/utils");
var modalState = null;
var setModalState = null;
function calculateUnassignedHolderCount(state) {
  var _a;
  const unassignedProducts = (((_a = state == null ? void 0 : state.service) == null ? void 0 : _a.value) || []).filter(
    (item) => {
      var _a2, _b, _c;
      return (!((_a2 = item._extend) == null ? void 0 : _a2.holder_id) || ((_c = (_b = item._extend) == null ? void 0 : _b.holder_id) == null ? void 0 : _c.length) === 0) && !(0, import_utils2.isNormalProductByDurationSchedule)(item);
    }
  );
  const arr = [];
  unassignedProducts.forEach((item) => {
    var _a2;
    if ((_a2 = item == null ? void 0 : item._extend) == null ? void 0 : _a2.groupId) {
      if (!arr.some((sub) => {
        var _a3, _b;
        return ((_a3 = sub == null ? void 0 : sub._extend) == null ? void 0 : _a3.groupId) === ((_b = item == null ? void 0 : item._extend) == null ? void 0 : _b.groupId);
      })) {
        arr.push(item);
      }
    } else {
      arr.push(item);
    }
  });
  return arr.reduce((sum, item) => {
    var _a2;
    return sum + (((_a2 = item._extend) == null ? void 0 : _a2.quantity) || 1);
  }, 0);
}
var ConfirmHolderModal = () => {
  var _a, _b, _c;
  const [localState, setLocalState] = (0, import_react.useState)(modalState);
  (0, import_react.useEffect)(() => {
    setModalState = (newState) => {
      setLocalState(newState);
      modalState = newState;
    };
    return () => {
      setModalState = null;
    };
  }, []);
  const { state, dispatch, resolve } = localState || {};
  const handleClose = () => {
    if (!localState || !localState.open) {
      return;
    }
    import_antd.Modal.confirm({
      title: import_utils.locales.getText("pisell2.text.deposit-delete-message-title"),
      content: import_utils.locales.getText("pisell2.text.close-confirm-message"),
      okText: import_utils.locales.getText("pisell2.text.confirm"),
      cancelText: import_utils.locales.getText("pisell2.text.cancel"),
      centered: true,
      onOk: () => {
        if (setModalState) {
          setModalState({ ...localState, open: false });
        }
        if (resolve) {
          resolve();
        }
      },
      onCancel: () => {
      }
    });
  };
  const { run: debouncedHandleClose } = (0, import_ahooks.useDebounceFn)(handleClose, {
    wait: 500,
    leading: true,
    trailing: false
  });
  if (!localState || !localState.open) {
    return /* @__PURE__ */ import_react.default.createElement(import_selectHolderModal.SelectHolderModal, null);
  }
  const unassignedCount = calculateUnassignedHolderCount(state);
  const handleSkip = () => {
    if (setModalState) {
      setModalState({ ...localState, open: false });
    }
    if (resolve) {
      resolve();
    }
  };
  const handleConfirm = () => {
    if (setModalState) {
      setModalState({ ...localState, open: false });
    }
    if (resolve) {
      resolve();
    }
  };
  const handleChange = async (currentItem, type, other) => {
    var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k;
    const holderConfig = ((_a2 = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a2.holder) || {};
    const currentMaxSelectedCount = (holderConfig == null ? void 0 : holderConfig.maxSelectedCount) || ((_c2 = (_b2 = currentItem == null ? void 0 : currentItem.cacheItem) == null ? void 0 : _b2._extend) == null ? void 0 : _c2.quantity) || 1;
    const currentHolderId = (_d = currentItem == null ? void 0 : currentItem._extend) == null ? void 0 : _d.holder_id;
    const initialValue = currentHolderId ? Array.isArray(currentHolderId) ? currentHolderId : [currentHolderId] : void 0;
    const result = await (0, import_selectHolderModal.selectHolderModal)({
      state,
      dispatch,
      initialValue,
      maxSelectedCount: currentMaxSelectedCount
    });
    if (result && result.length > 0) {
      console.log(currentItem, result);
      const _list = state.service.value;
      const cacheItem = currentItem.cacheItem;
      if ((_e = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _e.groupId) {
        const groupId = (_f = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _f.groupId;
        const groupItems = _list.filter((item) => {
          var _a3;
          return ((_a3 = item == null ? void 0 : item._extend) == null ? void 0 : _a3.groupId) === groupId;
        });
        groupItems.forEach((item) => {
          item._extend.holder_id = result;
        });
      } else {
        cacheItem._extend.holder_id = result;
        const index = _list.findIndex((item) => item._id === cacheItem._id);
        if (index !== -1) {
          _list[index] = cacheItem;
        }
      }
      if (((_h = (_g = cacheItem._extend) == null ? void 0 : _g.holder_id) == null ? void 0 : _h.length) > 1 || ((_i = cacheItem._extend) == null ? void 0 : _i.quantity) > 1 && ((_k = (_j = cacheItem._extend) == null ? void 0 : _j.holder_id) == null ? void 0 : _k.length) >= 1) {
        let holder_ids = cacheItem._extend.holder_id;
        cacheItem._extend.holder_id = holder_ids[0];
        holder_ids = holder_ids.slice(1);
        cacheItem._holderKey = (0, import_utils3.genHolderKey)(cacheItem);
        holder_ids.forEach((holder_id) => {
          const newItem = { ...cacheItem, _id: (0, import_utils4.getUniqueId)(), _extend: { ...cacheItem._extend, holder_id: [holder_id], quantity: 1 } };
          newItem._holderKey = (0, import_utils3.genHolderKey)(newItem);
          _list.unshift(newItem);
          cacheItem._extend.quantity -= 1;
        });
        if (cacheItem._extend.quantity > 1) {
          const newItem = { ...cacheItem, _id: (0, import_utils4.getUniqueId)(), _extend: { ...cacheItem._extend, holder_id: void 0, quantity: cacheItem._extend.quantity - 1 } };
          newItem._holderKey = (0, import_utils3.genHolderKey)(newItem);
          _list.unshift(newItem);
          cacheItem._extend.quantity = 1;
        }
      }
      _list.sort((a, b) => {
        var _a3, _b3;
        const aHasHolderId = ((_a3 = a._extend) == null ? void 0 : _a3.holder_id) && (Array.isArray(a._extend.holder_id) ? a._extend.holder_id.length > 0 : true);
        const bHasHolderId = ((_b3 = b._extend) == null ? void 0 : _b3.holder_id) && (Array.isArray(b._extend.holder_id) ? b._extend.holder_id.length > 0 : true);
        if (!aHasHolderId && bHasHolderId) return -1;
        if (aHasHolderId && !bHasHolderId) return 1;
        return 0;
      });
      dispatch({
        type: "setService",
        payload: {
          value: _list,
          edit: true
        }
      });
    }
  };
  const title = import_utils.locales.getText("pisell2.text.holder-unassigned")(unassignedCount);
  const hintText = import_utils.locales.getText("pisell2.text.press-card-assign-holder");
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellContainer,
    {
      renderType: "modal",
      props: {
        open: localState.open,
        onCancel: () => {
          debouncedHandleClose();
        },
        title,
        footer: null,
        width: 800,
        destroyOnClose: true,
        maskClosable: false,
        closable: true
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "confirm-holder-modal-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "confirm-holder-modal-hint" }, hintText), /* @__PURE__ */ import_react.default.createElement("div", { className: "confirm-holder-modal-service" }, /* @__PURE__ */ import_react.default.createElement(
      import_Lists.default,
      {
        isParallelResource: true,
        isShowDelete: false,
        isShowAmountFooter: false,
        onChange: handleChange
      }
    )), /* @__PURE__ */ import_react.default.createElement("div", { className: "confirm-holder-modal-footer" }, ((_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.subject_require) !== "require" ? /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { onClick: handleSkip, className: "skip-button", size: "large" }, import_utils.locales.getText("pisell2.text.skip-for-now")) : null, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", onClick: handleConfirm, className: "confirm-button" }, import_utils.locales.getText("pisell2.text.confirm"))))
  ), /* @__PURE__ */ import_react.default.createElement(import_selectHolderModal.SelectHolderModal, null));
};
function showConfirmHolderModal(state, dispatch) {
  return new Promise((resolve) => {
    modalState = {
      open: true,
      state,
      dispatch,
      resolve
    };
    if (setModalState) {
      setModalState(modalState);
    } else {
      console.warn("ConfirmHolderModal component is not mounted. Please render <ConfirmHolderModal /> in your component tree.");
    }
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ConfirmHolderModal,
  showConfirmHolderModal
});
