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/giftSelect/openGiftSkuDetail.ts
var openGiftSkuDetail_exports = {};
__export(openGiftSkuDetail_exports, {
  openGiftSkuDetail: () => openGiftSkuDetail
});
module.exports = __toCommonJS(openGiftSkuDetail_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("../../../components/booking/info/service2/utils");
var import_enrichCacheItemBeforeAdd = require("../utils/enrichCacheItemBeforeAdd");
var import_mergeCatalogDurationIntoCacheItem = require("../utils/mergeCatalogDurationIntoCacheItem");
var import_patchCacheItemDateRange = require("../utils/patchCacheItemDateRange");
var import_bookingDate = require("../utils/bookingDate");
var import_buildGiftCacheItemFromDetail = require("./buildGiftCacheItemFromDetail");
var import_giftSkuPrefill = require("./giftSkuPrefill");
var import_resolveCatalogProductId = require("./resolveCatalogProductId");
async function openGiftSkuDetail(params) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
  const {
    product,
    currentItem,
    bookingTicket,
    uiHosts,
    customerId,
    date,
    bookingConfig,
    tempOrder,
    locale,
    normalProductDetailBridge,
    onComplete
  } = params;
  const openDetail = uiHosts == null ? void 0 : uiHosts.openProductDetail;
  if (!openDetail || !bookingTicket) {
    console.warn("[SalesSdk/giftSelect] openProductDetail 或 bookingTicket 不可用，无法选择规格");
    return;
  }
  const catalogProduct = (0, import_resolveCatalogProductId.normalizeCatalogProduct)(product);
  const flowDate = (0, import_bookingDate.formatBookingDateTime)(date ?? ((_a = bookingTicket.getBookingDate) == null ? void 0 : _a.call(bookingTicket)) ?? null) ?? (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
  const flowCtx = {
    type: "select",
    quantity: Number(((_b = catalogProduct == null ? void 0 : catalogProduct._extend) == null ? void 0 : _b.quantity) ?? 1) || 1,
    date: flowDate,
    customerId
  };
  if ((0, import_giftSkuPrefill.hasExistingGiftSkuSelection)(catalogProduct) && normalProductDetailBridge && (0, import_utils.isNormalProductByDurationSchedule)(catalogProduct)) {
    const editResult = await normalProductDetailBridge.request({
      mode: "edit",
      cacheItem: catalogProduct
    });
    if (!editResult || editResult.mode !== "edit") {
      return;
    }
    const editedQuantity = Number(((_d = (_c = editResult.cacheItem) == null ? void 0 : _c._extend) == null ? void 0 : _d.quantity) ?? flowCtx.quantity) || 1;
    await finalizeGiftCacheItem(editResult.cacheItem, params, flowDate, {
      ...flowCtx,
      quantity: editedQuantity
    });
    return;
  }
  const decision = (_e = bookingTicket.decideAddProduct) == null ? void 0 : _e.call(bookingTicket, catalogProduct, flowCtx);
  if ((decision == null ? void 0 : decision.action) === "add") {
    await finalizeGiftCacheItem(decision.cacheItem, params, flowDate, flowCtx);
    return;
  }
  const payload = (decision == null ? void 0 : decision.action) === "requiresDetail" ? {
    ...decision.payload,
    item: (0, import_resolveCatalogProductId.normalizeCatalogProduct)(((_f = decision.payload) == null ? void 0 : _f.item) ?? catalogProduct),
    productData: (0, import_resolveCatalogProductId.normalizeCatalogProduct)(
      ((_g = decision.payload) == null ? void 0 : _g.productData) ?? catalogProduct
    )
  } : ((_h = bookingTicket.buildRequiresDetailPayload) == null ? void 0 : _h.call(bookingTicket, catalogProduct, flowCtx)) ?? null;
  if (!payload) {
    console.warn("[SalesSdk/giftSelect] buildRequiresDetailPayload 失败");
    return;
  }
  const detailPayload = (0, import_giftSkuPrefill.hasExistingGiftSkuSelection)(catalogProduct) ? (0, import_giftSkuPrefill.enrichDetailPayloadWithInitValue)(payload, catalogProduct) : payload;
  const detailResult = await openDetail(detailPayload);
  if (!detailResult) {
    return;
  }
  const confirmQuantity = Number(((_i = catalogProduct == null ? void 0 : catalogProduct._extend) == null ? void 0 : _i.quantity) ?? ((_j = detailResult == null ? void 0 : detailResult.e) == null ? void 0 : _j.quantity) ?? 1) || 1;
  const cacheItem = (0, import_buildGiftCacheItemFromDetail.resolveGiftCacheItemAfterDetail)(
    bookingTicket,
    catalogProduct,
    detailResult,
    {
      quantity: confirmQuantity,
      date: flowDate,
      customerId
    }
  );
  if (!cacheItem) {
    return;
  }
  await finalizeGiftCacheItem(cacheItem, params, flowDate, {
    ...flowCtx,
    quantity: confirmQuantity
  });
}
async function finalizeGiftCacheItem(initialCacheItem, params, flowDate, flowCtx) {
  const {
    product,
    currentItem,
    bookingTicket,
    uiHosts,
    customerId,
    bookingConfig,
    tempOrder,
    locale,
    onComplete
  } = params;
  if (!bookingTicket) {
    return;
  }
  const catalogProduct = (0, import_resolveCatalogProductId.normalizeCatalogProduct)(product);
  let cacheItem = (0, import_mergeCatalogDurationIntoCacheItem.mergeCatalogDurationIntoCacheItem)(initialCacheItem, catalogProduct);
  cacheItem = (0, import_patchCacheItemDateRange.applyDefaultMultiDayRangeIfNeeded)(cacheItem, catalogProduct, flowDate);
  if (typeof bookingTicket.decideAfterDetail === "function") {
    const decision = bookingTicket.decideAfterDetail(cacheItem, {
      quantity: flowCtx.quantity,
      customerId,
      date: flowDate,
      bookingConfig: bookingConfig ?? void 0
    });
    if (decision.action === "requiresBookingEdit") {
      const openEdit = uiHosts == null ? void 0 : uiHosts.openBookingEdit;
      if (!openEdit) {
        console.warn("[SalesSdk/giftSelect] openBookingEdit 未配置，无法完成预约类赠品选择");
        return;
      }
      const editPayload = {
        ...decision.payload ?? {},
        cacheItem: decision.cacheItem ?? cacheItem,
        date: flowDate,
        customerId
      };
      const edited = await openEdit(editPayload);
      if (!edited) {
        return;
      }
      cacheItem = edited;
    } else if (decision.cacheItem) {
      cacheItem = decision.cacheItem;
    }
  }
  const enriched = await (0, import_enrichCacheItemBeforeAdd.enrichCacheItemBeforeAdd)({
    cacheItem,
    catalogItem: catalogProduct,
    uiHosts,
    bookingConfig: bookingConfig ?? null,
    tempOrder: tempOrder ?? null,
    customerId,
    date: flowDate,
    locale,
    mode: "direct"
  });
  if (enriched.status === "cancelled") {
    return;
  }
  const resolvedCacheItem = enriched.status === "ok" ? enriched.cacheItem : cacheItem;
  const finalItem = applyGiftPriceToCacheItem(
    resolvedCacheItem,
    catalogProduct == null ? void 0 : catalogProduct._initPrice,
    !!(currentItem == null ? void 0 : currentItem.isGift)
  );
  onComplete(finalItem);
}
function applyGiftPriceToCacheItem(cacheItem, initPrice, isGift = true) {
  var _a;
  if (!isGift) {
    return cacheItem;
  }
  const originPrice = initPrice ?? (cacheItem == null ? void 0 : cacheItem._initPrice) ?? (cacheItem == null ? void 0 : cacheItem.origin_price) ?? (cacheItem == null ? void 0 : cacheItem.price) ?? 0;
  return {
    ...cacheItem,
    price: 0,
    base_price: 0,
    origin_price: originPrice,
    _initPrice: originPrice,
    _extend: {
      ...cacheItem._extend || {},
      price: 0,
      quantity: Number(((_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.quantity) ?? (cacheItem == null ? void 0 : cacheItem.num) ?? 1) || 1,
      total: 0,
      origin_total: originPrice
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  openGiftSkuDetail
});
