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/hosts/ConfirmHolderHostRenderer.tsx
var ConfirmHolderHostRenderer_exports = {};
__export(ConfirmHolderHostRenderer_exports, {
  ConfirmHolderHostRenderer: () => ConfirmHolderHostRenderer
});
module.exports = __toCommonJS(ConfirmHolderHostRenderer_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_SalesSdkHostBridgesContext = require("../context/SalesSdkHostBridgesContext");
var import_useSalesSdk = require("../hooks/useSalesSdk");
var import_useSalesSdkCart = require("../hooks/useSalesSdkCart");
var import_useSalesSdkUIHosts = require("../hooks/useSalesSdkUIHosts");
var import_SalesSdkHolderContext = require("../context/SalesSdkHolderContext");
var import_SalesSdkConfirmHolderModal = require("../components/SalesSdkConfirmHolderModal");
var import_assignHolderToCartLine = require("../utils/assignHolderToCartLine");
var import_holderSubmitValidation = require("../utils/holderSubmitValidation");
var ConfirmHolderHostRenderer = () => {
  const bridges = (0, import_SalesSdkHostBridgesContext.useSalesSdkHostBridges)();
  const holderConfirm = bridges.holderConfirm;
  const pending = (0, import_react.useSyncExternalStore)(
    holderConfirm.subscribe,
    holderConfirm.getSnapshot,
    () => null
  );
  const uiHosts = (0, import_useSalesSdkUIHosts.useSalesSdkUIHosts)();
  const { bookingTicket, tempOrder } = (0, import_useSalesSdk.useSalesSdk)();
  const cart = (0, import_useSalesSdkCart.useSalesSdkCart)();
  const { holderOptions } = (0, import_SalesSdkHolderContext.useSalesSdkHolderOptional)();
  const [refreshKey, setRefreshKey] = (0, import_react.useState)(0);
  const ctx = pending == null ? void 0 : pending.ctx;
  const open = Boolean((pending == null ? void 0 : pending.open) && ctx);
  const snapshotLineKeys = (ctx == null ? void 0 : ctx.snapshotLineKeys) ?? [];
  const liveUnassignedCount = (0, import_react.useMemo)(() => {
    if (!open || !bookingTicket) return 0;
    return (0, import_holderSubmitValidation.calculateUnassignedHolderCount)(
      (0, import_holderSubmitValidation.collectUnassignedHolderLines)(bookingTicket)
    );
  }, [bookingTicket, open, refreshKey]);
  const lines = (0, import_react.useMemo)(() => {
    if (!open || !bookingTicket || !snapshotLineKeys.length) return [];
    return (0, import_holderSubmitValidation.buildHolderConfirmDisplayLines)({
      bookingTicket,
      snapshotLineKeys,
      cartBookings: cart.bookings || []
    });
  }, [bookingTicket, cart.bookings, open, refreshKey, snapshotLineKeys]);
  const handleMutate = (0, import_ahooks.useMemoizedFn)(() => {
    var _a, _b;
    (_a = cart.recalcSummary) == null ? void 0 : _a.call(cart).catch(() => void 0);
    setRefreshKey((key) => key + 1);
    holderConfirm.refresh();
    (_b = ctx == null ? void 0 : ctx.onMutate) == null ? void 0 : _b.call(ctx);
  });
  const handleAssign = (0, import_ahooks.useMemoizedFn)(async (line) => {
    if (!bookingTicket || !ctx) return;
    await (0, import_assignHolderToCartLine.assignHolderToCartLineWithFeedback)({
      line,
      bookingTicket,
      tempOrder: ctx.tempOrder ?? tempOrder,
      updateItem: cart.updateItem,
      openHolderSelect: uiHosts == null ? void 0 : uiHosts.openHolderSelect,
      holderOptions,
      onMutate: handleMutate
    });
  });
  const handleConfirm = (0, import_react.useCallback)(() => {
    if (liveUnassignedCount > 0) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("pisell2.text.press-card-assign-holder")
      });
      setRefreshKey((key) => key + 1);
      holderConfirm.refresh();
      return;
    }
    holderConfirm.complete();
  }, [holderConfirm, liveUnassignedCount]);
  const handleSkip = (0, import_react.useCallback)(() => {
    holderConfirm.complete();
  }, [holderConfirm]);
  const handleDismissConfirmed = (0, import_react.useCallback)(() => {
    holderConfirm.cancel();
  }, [holderConfirm]);
  if (!open || !ctx) {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_SalesSdkConfirmHolderModal.SalesSdkConfirmHolderModal,
    {
      open,
      lines,
      liveUnassignedCount,
      bookingConfig: ctx.bookingConfig,
      onAssign: handleAssign,
      onConfirm: handleConfirm,
      onSkip: handleSkip,
      onDismissConfirmed: handleDismissConfirmed
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ConfirmHolderHostRenderer
});
