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/mapGiftSelectContext.ts
var mapGiftSelectContext_exports = {};
__export(mapGiftSelectContext_exports, {
  buildGiftSelectContextKey: () => buildGiftSelectContextKey,
  convertLegacySelectedToChoices: () => convertLegacySelectedToChoices,
  mapGiftSelectContextToLegacyItem: () => mapGiftSelectContextToLegacyItem,
  shouldOpenGiftSelectModal: () => shouldOpenGiftSelectModal
});
module.exports = __toCommonJS(mapGiftSelectContext_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("../../../components/shoppingCart/utils");
function buildGiftSelectContextKey(ctx) {
  const optionKey = (ctx.giftOptions || []).map((o) => `${o.product_id}:${o.product_variant_id ?? 0}`).join("|");
  return [
    ctx.strategyId,
    ctx.mode || "add",
    ctx.giftCount,
    (ctx.sourceProductIds || []).join(","),
    optionKey
  ].join("#");
}
function shouldOpenGiftSelectModal(ctx) {
  if (!ctx || !ctx.giftCount || ctx.giftCount <= 0) return false;
  if (!Array.isArray(ctx.giftOptions) || ctx.giftOptions.length === 0) return false;
  if (ctx.mode === "edit") return true;
  return ctx.giftOptions.length >= 2;
}
function mapGiftSelectContextToLegacyItem(ctx, options) {
  var _a, _b, _c, _d, _e;
  const giftOptions = ctx.giftOptions || [];
  const products = (0, import_utils.formatProductsByVariant)(giftOptions);
  const isMultiple = ctx.giftCount > 1;
  const selectedOrigin = ctx._selectedOriginGifts || [];
  let selected = isMultiple ? [] : "";
  if ((_a = ctx.initialChoices) == null ? void 0 : _a.length) {
    if (isMultiple) {
      selected = ctx.initialChoices.map((c) => ({
        value: c.product_id,
        quantity: c.quantity
      }));
    } else {
      selected = ((_b = ctx.initialChoices[0]) == null ? void 0 : _b.product_id) || "";
    }
  } else if (selectedOrigin.length) {
    if (isMultiple) {
      selected = selectedOrigin.map((item) => ({
        value: item.product_id || item.id,
        quantity: Number(item.num ?? item.quantity ?? 1) || 1
      }));
    } else {
      selected = ((_c = selectedOrigin[0]) == null ? void 0 : _c.product_id) || ((_d = selectedOrigin[0]) == null ? void 0 : _d.id) || "";
    }
  }
  const giftInfo = {
    giftOptions,
    giftCount: ctx.giftCount
  };
  const scheduleDate = options == null ? void 0 : options.scheduleDate;
  const legacyState = {
    amountSymbol: (options == null ? void 0 : options.amountSymbol) || "$",
    ...scheduleDate ? { date: { value: (0, import_dayjs.default)(scheduleDate) } } : {}
  };
  return {
    giftInfo,
    strategyId: ctx.strategyId,
    strategyName: ctx.strategyName,
    sourceProductId: (_e = ctx.sourceProductIds) == null ? void 0 : _e[0],
    sourceProductIds: ctx.sourceProductIds,
    products,
    isMultiple,
    isGift: true,
    selected,
    selectedProducts: selectedOrigin,
    isEdit: ctx.mode === "edit" || selectedOrigin.length > 0,
    _selectedOriginGifts: selectedOrigin,
    promotionInfo: {
      strategyId: ctx.strategyId,
      strategyName: ctx.strategyName,
      giftInfo
    },
    /** legacy usePromotionModal 读取 schedule_date 用 */
    state: legacyState
  };
}
function convertLegacySelectedToChoices(selectedProducts) {
  if (!Array.isArray(selectedProducts) || !selectedProducts.length) return [];
  return selectedProducts.map((product) => {
    var _a, _b, _c, _d, _e;
    const productId = Number((product == null ? void 0 : product.product_id) ?? (product == null ? void 0 : product.id));
    const variantId = Number(
      (product == null ? void 0 : product.product_variant_id) ?? ((_b = (_a = product == null ? void 0 : product._extend) == null ? void 0 : _a.other) == null ? void 0 : _b.product_variant_id) ?? ((_d = (_c = product == null ? void 0 : product._extend) == null ? void 0 : _c._skuValue) == null ? void 0 : _d.product_variant_id) ?? 0
    );
    const quantity = Number(((_e = product == null ? void 0 : product._extend) == null ? void 0 : _e.quantity) ?? (product == null ? void 0 : product.num) ?? (product == null ? void 0 : product.quantity) ?? 1) || 1;
    if (!productId) return null;
    return {
      product_id: productId,
      product_variant_id: variantId,
      quantity,
      // confirm 后 buildGiftOrderProductsFromChoices 优先复用，避免二次打接口
      _productSnapshot: product
    };
  }).filter(Boolean);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildGiftSelectContextKey,
  convertLegacySelectedToChoices,
  mapGiftSelectContextToLegacyItem,
  shouldOpenGiftSelectModal
});
