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/enrich.ts
var enrich_exports = {};
__export(enrich_exports, {
  attachGiftDataToCartItems: () => attachGiftDataToCartItems,
  enrichCartItem: () => enrichCartItem,
  enrichCartItems: () => enrichCartItems,
  enrichCustomer: () => enrichCustomer
});
module.exports = __toCommonJS(enrich_exports);
var import_selectors = require("../selectors");
var import_identity = require("./identity");
var import_resolveLocalizedText = require("./resolveLocalizedText");
var import_utils = require("../../../components/shoppingCart/utils");
function resolveFirstLocalizedText(...sources) {
  for (const source of sources) {
    const resolved = (0, import_resolveLocalizedText.resolveLocalizedText)(source);
    if (resolved) return resolved;
  }
  return "";
}
function pickBookingForProductUid(bookings) {
  if (!Array.isArray(bookings) || bookings.length === 0) return void 0;
  const childBooking = bookings.find(
    (booking) => booking.parent_id !== 0 && booking.parent_id !== null && booking.parent_id !== void 0
  );
  return childBooking ?? bookings[0];
}
function normalizeBundleForDisplay(bundle) {
  var _a;
  const origin = ((_a = bundle == null ? void 0 : bundle.metadata) == null ? void 0 : _a.origin) || (bundle == null ? void 0 : bundle.origin) || (bundle == null ? void 0 : bundle.product);
  const title = resolveFirstLocalizedText(
    bundle == null ? void 0 : bundle.bundle_title,
    bundle == null ? void 0 : bundle.title,
    bundle == null ? void 0 : bundle.name,
    bundle == null ? void 0 : bundle.product_title,
    origin == null ? void 0 : origin.title,
    origin == null ? void 0 : origin.name
  );
  const price = (bundle == null ? void 0 : bundle.bundle_sum_price) ?? (bundle == null ? void 0 : bundle.bundle_payment_price) ?? (bundle == null ? void 0 : bundle.custom_price) ?? (bundle == null ? void 0 : bundle.price) ?? (bundle == null ? void 0 : bundle.selling_price) ?? (origin == null ? void 0 : origin.price);
  return {
    ...bundle,
    bundle_title: title ?? (bundle == null ? void 0 : bundle.bundle_title),
    bundle_sum_price: price ?? (bundle == null ? void 0 : bundle.bundle_sum_price),
    bundle_payment_price: (bundle == null ? void 0 : bundle.bundle_payment_price) ?? price,
    bundle_selling_price: (bundle == null ? void 0 : bundle.bundle_selling_price) ?? price,
    custom_price: (bundle == null ? void 0 : bundle.custom_price) ?? price
  };
}
function enrichCartItem(line, ctx) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const productUid = (_a = line.metadata) == null ? void 0 : _a.unique_identification_number;
  const matchedBookings = productUid ? (_b = ctx.bookingDetailMap) == null ? void 0 : _b[String(productUid)] : void 0;
  const matched = pickBookingForProductUid(matchedBookings);
  const detail = matched == null ? void 0 : matched.detail;
  const productSummary = matched == null ? void 0 : matched.product;
  const catalog = ctx.productsCatalog.find(
    (p) => String(p.id) === String(line.product_id)
  );
  const runtimeOrigin = productUid ? (_d = (_c = ctx.productsByUid) == null ? void 0 : _c[String(productUid)]) == null ? void 0 : _d.origin : void 0;
  const lineOrigin = ((_e = line.metadata) == null ? void 0 : _e.origin) ?? runtimeOrigin;
  const lineExtend = line._extend || (lineOrigin == null ? void 0 : lineOrigin._extend) || {};
  const variant = (_f = catalog == null ? void 0 : catalog.variants) == null ? void 0 : _f.find(
    (v) => (v == null ? void 0 : v.id) === line.product_variant_id
  );
  const detailBundles = Array.isArray(detail == null ? void 0 : detail.product_bundle) ? detail.product_bundle : [];
  const detailOptions = Array.isArray(detail == null ? void 0 : detail.product_option_item) ? detail.product_option_item : [];
  const catalogBundles = Array.isArray(catalog == null ? void 0 : catalog.bundles) ? catalog.bundles : [];
  const catalogOptions = Array.isArray(catalog == null ? void 0 : catalog.options) ? catalog.options : [];
  const bundleTitles = (line.product_bundle || []).map((b) => {
    const matchedDetail = detailBundles.find(
      (db) => (db == null ? void 0 : db.bundle_id) === (b == null ? void 0 : b.bundle_id)
    );
    const matchedCatalog = catalogBundles.find((cb) => (cb == null ? void 0 : cb.id) === (b == null ? void 0 : b.bundle_id));
    return resolveFirstLocalizedText(
      matchedDetail == null ? void 0 : matchedDetail.title,
      matchedCatalog == null ? void 0 : matchedCatalog.title,
      b == null ? void 0 : b.bundle_title,
      b == null ? void 0 : b.title,
      b == null ? void 0 : b.name
    );
  });
  const optionTitles = (line.product_option_item || []).map((o) => {
    const matchedDetail = detailOptions.find(
      (d) => (d == null ? void 0 : d.product_option_item_id) === (o == null ? void 0 : o.product_option_item_id)
    );
    const matchedCatalog = catalogOptions.find(
      (co) => (co == null ? void 0 : co.id) === (o == null ? void 0 : o.product_option_item_id)
    );
    return resolveFirstLocalizedText(
      matchedDetail == null ? void 0 : matchedDetail.title,
      matchedCatalog == null ? void 0 : matchedCatalog.title,
      o == null ? void 0 : o.title,
      o == null ? void 0 : o.name
    );
  });
  const startAt = matched && matched.start_date && matched.start_time ? `${matched.start_date} ${matched.start_time}` : null;
  const endAt = matched && matched.end_date && matched.end_time ? `${matched.end_date} ${matched.end_time}` : null;
  const lineAny = line;
  const metadataAny = lineAny.metadata || {};
  const promotionInfo = metadataAny._promotion;
  const giftInfo = metadataAny._giftInfo;
  let promotionTags = Array.isArray(metadataAny._promotionTags) ? metadataAny._promotionTags : void 0;
  if (!promotionTags && promotionInfo) {
    const strategyMeta = promotionInfo.strategyMetadata || {};
    const text = ((_g = strategyMeta.display) == null ? void 0 : _g.text) ?? strategyMeta.name ?? strategyMeta.title ?? "";
    if (text) {
      promotionTags = [
        {
          text,
          type: ((_h = strategyMeta.display) == null ? void 0 : _h.type) || "info",
          strategyId: promotionInfo.strategyId
        }
      ];
    }
  }
  const resolvedPromotionTags = promotionTags ? promotionTags.map((tag) => ({
    ...tag,
    text: (0, import_resolveLocalizedText.resolveLocalizedText)((tag == null ? void 0 : tag.text) ?? (tag == null ? void 0 : tag.title))
  })) : void 0;
  const resolvedTitle = resolveFirstLocalizedText(
    detail == null ? void 0 : detail.product_title,
    lineAny.product_title,
    metadataAny.product_title,
    productSummary == null ? void 0 : productSummary.title,
    productSummary == null ? void 0 : productSummary.name,
    catalog == null ? void 0 : catalog.title,
    lineOrigin == null ? void 0 : lineOrigin.title,
    lineOrigin == null ? void 0 : lineOrigin.name,
    lineExtend == null ? void 0 : lineExtend.product_name,
    metadataAny.product_name
  );
  return {
    ...line,
    product_bundle: (line.product_bundle || []).map(normalizeBundleForDisplay),
    _identity: (0, import_identity.pickIdentity)(line),
    _extend: {
      booking: matched ?? null
    },
    ...(resolvedPromotionTags == null ? void 0 : resolvedPromotionTags.length) ? {
      _promotionTags: resolvedPromotionTags,
      promotions: resolvedPromotionTags
    } : {},
    ...giftInfo ? { _isGiftPromotion: true, _giftInfo: giftInfo, isGift: true } : {},
    title: resolvedTitle,
    cover: (detail == null ? void 0 : detail.product_cover) ?? (catalog == null ? void 0 : catalog.cover) ?? (lineOrigin == null ? void 0 : lineOrigin.cover) ?? (lineOrigin == null ? void 0 : lineOrigin.cover_url) ?? "",
    variant_title: resolveFirstLocalizedText(
      detail == null ? void 0 : detail.variant_title,
      variant == null ? void 0 : variant.title,
      lineOrigin == null ? void 0 : lineOrigin.variant_title
    ),
    bundle_titles: bundleTitles,
    option_titles: optionTitles,
    note: (detail == null ? void 0 : detail.note) ?? line.note ?? "",
    duration: (productSummary == null ? void 0 : productSummary.duration) ?? (catalog == null ? void 0 : catalog.duration) ?? (lineOrigin == null ? void 0 : lineOrigin.duration),
    product_resource: (productSummary == null ? void 0 : productSummary.product_resource) ?? (catalog == null ? void 0 : catalog.product_resource) ?? (lineOrigin == null ? void 0 : lineOrigin.product_resource),
    capacity: (productSummary == null ? void 0 : productSummary.capacity) ?? (lineExtend == null ? void 0 : lineExtend.capacity),
    start_at: startAt,
    end_at: endAt,
    resource_id: (matched == null ? void 0 : matched.relation_id) ?? null
  };
}
function buildGiftDataForMainLine(line, allLines) {
  var _a, _b, _c, _d;
  const giftOptions = (_c = (_b = (_a = line.metadata) == null ? void 0 : _a._promotion) == null ? void 0 : _b.giftInfo) == null ? void 0 : _c.giftOptions;
  if (!Array.isArray(giftOptions) || giftOptions.length === 0) return null;
  const mainUid = String(((_d = line.metadata) == null ? void 0 : _d.unique_identification_number) || "");
  if (!mainUid) return null;
  const pureMainUid = mainUid.replace(/___.*/, "");
  const selectedOriginGifts = allLines.filter((giftLine) => {
    var _a2;
    const giftMeta = (_a2 = giftLine.metadata) == null ? void 0 : _a2._giftInfo;
    if (!giftMeta) return false;
    const pureSource = String(giftMeta.sourceProductId || "").replace(/___.*/, "");
    return mainUid.includes(pureSource) || pureSource === pureMainUid;
  });
  const selectedGifts = selectedOriginGifts.filter((gift) => gift.product_id != null).map((gift) => ({
    id: gift.product_id,
    title: gift.title,
    image: gift.cover
  }));
  const totalCount = (0, import_utils.formatProductsByVariant)(giftOptions).length;
  return {
    isShow: true,
    totalCount,
    selectedGifts,
    _selectedOriginGifts: selectedOriginGifts
  };
}
function attachGiftDataToCartItems(lines) {
  if (!Array.isArray(lines) || lines.length === 0) return [];
  return lines.map((line) => {
    var _a;
    if ((_a = line.metadata) == null ? void 0 : _a._giftInfo) return line;
    const giftData = buildGiftDataForMainLine(line, lines);
    return giftData ? { ...line, giftData } : line;
  });
}
function enrichCartItems(lines, ctx) {
  if (!Array.isArray(lines) || lines.length === 0) return [];
  const enriched = lines.map((line) => enrichCartItem(line, ctx));
  return attachGiftDataToCartItems(enriched);
}
function enrichCustomer(selected, tempOrder) {
  if (selected && selected.id !== void 0 && selected.id !== null && selected.id !== "" && selected.id !== 0) {
    return {
      id: selected.id,
      name: selected.name ?? "",
      phone: selected.phone,
      email: selected.email,
      country_calling_code: selected.country_calling_code,
      cover: selected.cover,
      isWalkIn: (0, import_selectors.isWalkInCustomer)(selected)
    };
  }
  if (tempOrder && (tempOrder.customer_id != null || tempOrder.customer_name)) {
    const id = tempOrder.customer_id ?? "";
    return {
      id,
      name: tempOrder.customer_name ?? "",
      phone: tempOrder.phone,
      email: tempOrder.email,
      country_calling_code: tempOrder.country_calling_code,
      cover: void 0,
      isWalkIn: (0, import_selectors.isWalkInCustomer)({ id })
    };
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  attachGiftDataToCartItems,
  enrichCartItem,
  enrichCartItems,
  enrichCustomer
});
