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/productSkuOptions.ts
var productSkuOptions_exports = {};
__export(productSkuOptions_exports, {
  buildProductSkuPatch: () => buildProductSkuPatch,
  getDetailProductOptions: () => getDetailProductOptions,
  getLineProductOptions: () => getLineProductOptions,
  normalizeOptionForDisplay: () => normalizeOptionForDisplay,
  normalizeOptionUnitPrice: () => normalizeOptionUnitPrice,
  resolvePatchProductOptions: () => resolvePatchProductOptions,
  sumLineOptionUnitPrice: () => sumLineOptionUnitPrice
});
module.exports = __toCommonJS(productSkuOptions_exports);
var import_resolveLocalizedText = require("./resolveLocalizedText");
function getLineProductOptions(line) {
  var _a;
  if (!line) return [];
  const skuOptions = (_a = line.product_sku) == null ? void 0 : _a.option;
  if (Array.isArray(skuOptions)) {
    return skuOptions;
  }
  const legacyOptions = line.product_option_item;
  if (Array.isArray(legacyOptions)) {
    return legacyOptions;
  }
  return [];
}
function getDetailProductOptions(detail) {
  var _a;
  if (!detail) return [];
  const skuOptions = (_a = detail.product_sku) == null ? void 0 : _a.option;
  if (Array.isArray(skuOptions)) {
    return skuOptions;
  }
  const legacyOptions = detail.product_option_item;
  if (Array.isArray(legacyOptions)) {
    return legacyOptions;
  }
  return [];
}
function normalizeOptionUnitPrice(option) {
  const price = Number((option == null ? void 0 : option.add_price) ?? (option == null ? void 0 : option.price)) || 0;
  const num = Number((option == null ? void 0 : option.num) ?? (option == null ? void 0 : option.quantity)) || 0;
  return price * num;
}
function sumLineOptionUnitPrice(line) {
  return getLineProductOptions(line).reduce(
    (total, option) => total + normalizeOptionUnitPrice(option),
    0
  );
}
function normalizeOptionForDisplay(option) {
  const name = (0, import_resolveLocalizedText.resolveLocalizedText)((option == null ? void 0 : option.name) ?? (option == null ? void 0 : option.title));
  const price = (option == null ? void 0 : option.add_price) ?? (option == null ? void 0 : option.price);
  const num = (option == null ? void 0 : option.num) ?? (option == null ? void 0 : option.quantity) ?? 1;
  return {
    ...option,
    ...name ? { name } : {},
    ...price !== void 0 && price !== null && price !== "" ? { price } : {},
    num
  };
}
function buildProductSkuPatch(line, options) {
  return {
    ...(line == null ? void 0 : line.product_sku) && typeof line.product_sku === "object" ? line.product_sku : {},
    option: options
  };
}
function resolvePatchProductOptions(orderLine, productPatch) {
  var _a;
  if (Array.isArray((_a = productPatch == null ? void 0 : productPatch.product_sku) == null ? void 0 : _a.option)) {
    return productPatch.product_sku.option;
  }
  if (Array.isArray(productPatch == null ? void 0 : productPatch.product_option_item)) {
    return productPatch.product_option_item;
  }
  return getLineProductOptions(orderLine);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildProductSkuPatch,
  getDetailProductOptions,
  getLineProductOptions,
  normalizeOptionForDisplay,
  normalizeOptionUnitPrice,
  resolvePatchProductOptions,
  sumLineOptionUnitPrice
});
