var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/utils/runAddProductFlow.ts
var runAddProductFlow_exports = {};
__export(runAddProductFlow_exports, {
  runAddProductFlow: () => runAddProductFlow
});
module.exports = __toCommonJS(runAddProductFlow_exports);
var import_enrichCacheItemBeforeAdd = require("./enrichCacheItemBeforeAdd");
var import_patchCacheItemDateRange = require("./patchCacheItemDateRange");
var import_mergeCatalogDurationIntoCacheItem = require("./mergeCatalogDurationIntoCacheItem");
var import_ensureMultiDayRangeSelected = require("./ensureMultiDayRangeSelected");
var import_buildBookingEditDrawerState = require("../bookingEditService/buildBookingEditDrawerState");
var import_resolveConfirmDetailQuantity = require("./resolveConfirmDetailQuantity");
async function openBookingEditAfterEnrich(cart, hosts, payload, flowOptions, options, multiDayRange, catalogProduct) {
  const catalogItem = catalogProduct ?? payload.catalogProduct ?? payload.cacheItem;
  let cacheItem = (0, import_mergeCatalogDurationIntoCacheItem.mergeCatalogDurationIntoCacheItem)(payload.cacheItem, catalogItem);
  const multiDayGate = await (0, import_ensureMultiDayRangeSelected.ensureMultiDayRangeSelected)({
    hosts,
    cacheItem,
    catalogItem,
    product: catalogProduct,
    defaultStartDate: flowOptions.date ?? payload.date,
    existingRange: multiDayRange
  });
  if (multiDayGate.status === "missing_host") {
    throw new Error(
      "[SalesSdk] 跨日商品需配置 uiHosts.openMultiDaySelect（createOpenMultiDaySelectHost）"
    );
  }
  if (multiDayGate.status === "cancelled") {
    return { status: "cancelled" };
  }
  if (multiDayGate.status === "ok" && multiDayGate.cacheItem) {
    cacheItem = multiDayGate.cacheItem;
  } else {
    cacheItem = (0, import_patchCacheItemDateRange.applyDefaultMultiDayRangeIfNeeded)(
      cacheItem,
      catalogItem,
      flowOptions.date ?? payload.date
    );
  }
  const enriched = await (0, import_enrichCacheItemBeforeAdd.enrichCacheItemBeforeAdd)({
    cacheItem,
    catalogItem,
    uiHosts: hosts,
    bookingConfig: flowOptions.bookingConfig,
    tempOrder: flowOptions.tempOrder,
    customerId: flowOptions.customerId ?? payload.customerId,
    date: payload.date,
    locale: flowOptions.locale,
    mode: "drawer"
  });
  if (enriched.status === "cancelled" || enriched.status === "missing_host") {
    return { status: "cancelled" };
  }
  const openEdit = hosts == null ? void 0 : hosts.openBookingEdit;
  if (!openEdit) {
    return {
      status: "requiresBookingEdit",
      payload: { ...payload, cacheItem: enriched.cacheItem }
    };
  }
  const drawerCustomerId = (0, import_buildBookingEditDrawerState.resolveAddFlowCustomerId)(
    flowOptions.tempOrder,
    flowOptions.customerId ?? payload.customerId
  );
  const edited = await openEdit({
    ...payload,
    cacheItem: enriched.cacheItem,
    catalogProduct: catalogProduct ?? payload.catalogProduct,
    customerId: drawerCustomerId
  });
  if (!edited) {
    return { status: "cancelled" };
  }
  const { products } = await cart.confirmBookingEdit({
    cacheItem: edited,
    options: { quantity: options == null ? void 0 : options.quantity, note: options == null ? void 0 : options.note }
  });
  return { status: "added", products };
}
async function runAddProductFlow(cart, hosts, product, options) {
  var _a, _b;
  const flowOptions = options ?? {};
  let result = await cart.addProduct(product, options);
  let multiDayRange = null;
  if (result.status === "requiresDetail") {
    const openDetail = hosts == null ? void 0 : hosts.openProductDetail;
    if (!openDetail) {
      return result;
    }
    const detailResult = await openDetail(result.payload);
    if (!detailResult) {
      return { status: "cancelled" };
    }
    const catalog = ((_a = result.payload) == null ? void 0 : _a.productData) ?? product;
    const multiDayGate = await (0, import_ensureMultiDayRangeSelected.ensureMultiDayRangeSelected)({
      hosts,
      detail: detailResult.detail,
      catalogItem: catalog,
      product,
      defaultStartDate: (_b = result.payload) == null ? void 0 : _b.date
    });
    if (multiDayGate.status === "missing_host") {
      throw new Error(
        "[SalesSdk] 跨日商品需配置 uiHosts.openMultiDaySelect（createOpenMultiDaySelectHost）"
      );
    }
    if (multiDayGate.status === "cancelled") {
      return { status: "cancelled" };
    }
    if (multiDayGate.status === "ok") {
      multiDayRange = multiDayGate.range;
    }
    const confirmInput = {
      item: catalog,
      detailResult,
      options: {
        quantity: (0, import_resolveConfirmDetailQuantity.resolveConfirmDetailQuantity)({
          detailResult,
          options: flowOptions
        }),
        multiDayRange: multiDayRange || void 0
      }
    };
    result = await cart.confirmDetail(confirmInput);
    if (result.status === "requiresBookingEdit") {
      return openBookingEditAfterEnrich(
        cart,
        hosts,
        result.payload,
        flowOptions,
        options,
        multiDayRange,
        product
      );
    }
    return result;
  }
  if (result.status === "requiresBookingEdit") {
    return openBookingEditAfterEnrich(
      cart,
      hosts,
      result.payload,
      flowOptions,
      options,
      multiDayRange,
      product
    );
  }
  return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  runAddProductFlow
});
