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/buildProductLineUpdates.ts
var buildProductLineUpdates_exports = {};
__export(buildProductLineUpdates_exports, {
  buildProductLineUpdates: () => buildProductLineUpdates
});
module.exports = __toCommonJS(buildProductLineUpdates_exports);
var import_productSkuOptions = require("./productSkuOptions");
var import_syncBundleFromExtendOther = require("./syncBundleFromExtendOther");
function stableSerialize(value) {
  return JSON.stringify(value ?? null);
}
function hasAppliedLineDiscount(orderLine) {
  return Array.isArray(orderLine.discount_list) && orderLine.discount_list.length > 0;
}
function buildProductLineUpdates(orderLine, productPatch) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const patchMeta = productPatch.metadata || {};
  const lineMeta = orderLine.metadata || {};
  const priceOverride = patchMeta.price_override ?? ((_b = (_a = patchMeta.origin) == null ? void 0 : _a._extend) == null ? void 0 : _b.priceOverride);
  const otherBundle = ((_e = (_d = (_c = patchMeta.origin) == null ? void 0 : _c._extend) == null ? void 0 : _d.other) == null ? void 0 : _e.bundle) ?? ((_h = (_g = (_f = patchMeta.origin) == null ? void 0 : _f._extend) == null ? void 0 : _g.skuValue) == null ? void 0 : _h.bundle);
  const hasPriceOverride = priceOverride !== void 0 && priceOverride !== null && priceOverride !== "";
  const baseBundle = productPatch.product_bundle ?? orderLine.product_bundle;
  const productBundle = hasPriceOverride ? (0, import_syncBundleFromExtendOther.syncBundleFromExtendOther)(baseBundle, otherBundle) : productPatch.product_bundle ?? orderLine.product_bundle;
  const patchVariantId = Number(productPatch.product_variant_id);
  const hasPatchVariantId = Number.isFinite(patchVariantId) && patchVariantId > 0;
  const nextOptions = (0, import_productSkuOptions.resolvePatchProductOptions)(orderLine, productPatch);
  const hasOptionChange = stableSerialize(nextOptions) !== stableSerialize((0, import_productSkuOptions.getLineProductOptions)(orderLine));
  const hasBundleChange = productPatch.product_bundle !== void 0 && stableSerialize(productBundle) !== stableSerialize(orderLine.product_bundle);
  const hasVariantChange = hasPatchVariantId && patchVariantId !== Number(orderLine.product_variant_id || 0);
  const shouldKeepDiscountedLinePriceBasis = hasAppliedLineDiscount(orderLine) && !hasPriceOverride && !hasVariantChange && !hasOptionChange && !hasBundleChange;
  const sourceProductPrice = shouldKeepDiscountedLinePriceBasis ? lineMeta.source_product_price ?? patchMeta.source_product_price : patchMeta.source_product_price ?? lineMeta.source_product_price;
  const mainProductSellingPrice = shouldKeepDiscountedLinePriceBasis ? lineMeta.main_product_selling_price ?? patchMeta.main_product_selling_price : patchMeta.main_product_selling_price ?? lineMeta.main_product_selling_price;
  const mainProductOriginalPrice = shouldKeepDiscountedLinePriceBasis ? lineMeta.main_product_original_price ?? patchMeta.main_product_original_price : patchMeta.main_product_original_price ?? lineMeta.main_product_original_price;
  const updates = {
    num: productPatch.num,
    ...productPatch.selling_price !== void 0 ? { selling_price: productPatch.selling_price } : {},
    ...productPatch.original_price !== void 0 ? { original_price: productPatch.original_price } : {},
    note: productPatch.note,
    tax_fee: productPatch.tax_fee,
    is_charge_tax: productPatch.is_charge_tax,
    discount_list: shouldKeepDiscountedLinePriceBasis ? orderLine.discount_list : productPatch.discount_list ?? orderLine.discount_list,
    product_sku: (0, import_productSkuOptions.buildProductSkuPatch)(orderLine, nextOptions),
    ...hasPatchVariantId ? { product_variant_id: patchVariantId } : {},
    metadata: {
      ...lineMeta,
      ...patchMeta,
      ...sourceProductPrice !== void 0 ? { source_product_price: String(sourceProductPrice) } : {},
      ...mainProductSellingPrice !== void 0 ? { main_product_selling_price: String(mainProductSellingPrice) } : {},
      ...mainProductOriginalPrice !== void 0 ? { main_product_original_price: String(mainProductOriginalPrice) } : {},
      ...hasPriceOverride ? { price_override: String(priceOverride) } : {},
      ...hasPriceOverride ? { is_manual_discount: 1 } : {},
      price_schema_version: 2
    }
  };
  if (productBundle !== void 0) {
    updates.product_bundle = productBundle;
  }
  return updates;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildProductLineUpdates
});
