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/buildCustomProductUpdatesFromEditedCacheItem.ts
var buildCustomProductUpdatesFromEditedCacheItem_exports = {};
__export(buildCustomProductUpdatesFromEditedCacheItem_exports, {
  buildCustomProductUpdatesFromEditedCacheItem: () => buildCustomProductUpdatesFromEditedCacheItem
});
module.exports = __toCommonJS(buildCustomProductUpdatesFromEditedCacheItem_exports);
function toPriceString(value, fallback = "0.00") {
  const parsed = Number(value);
  if (!Number.isFinite(parsed)) {
    return fallback;
  }
  return parsed.toFixed(2);
}
function resolveQuantity(edited, orderLine) {
  var _a;
  const raw = Number(
    ((_a = edited == null ? void 0 : edited._extend) == null ? void 0 : _a.quantity) ?? (edited == null ? void 0 : edited.num) ?? orderLine.num ?? 1
  );
  if (!Number.isFinite(raw)) {
    return 1;
  }
  return Math.max(1, Math.floor(raw));
}
function resolveUnitPrice(edited, orderLine) {
  const extend = (edited == null ? void 0 : edited._extend) || {};
  const raw = extend.price ?? extend.total ?? (edited == null ? void 0 : edited.selling_price) ?? orderLine.selling_price ?? "0";
  return toPriceString(raw, toPriceString(orderLine.selling_price, "0.00"));
}
function resolveProductName(edited, orderLine) {
  var _a, _b, _c, _d, _e;
  const fromExtend = (_a = edited == null ? void 0 : edited._extend) == null ? void 0 : _a.product_name;
  if (typeof fromExtend === "string" && fromExtend.trim()) {
    return fromExtend.trim();
  }
  if (typeof (edited == null ? void 0 : edited.title) === "string" && edited.title.trim()) {
    return edited.title.trim();
  }
  const originTitle = ((_c = (_b = orderLine.metadata) == null ? void 0 : _b.origin) == null ? void 0 : _c.title) ?? ((_e = (_d = orderLine.metadata) == null ? void 0 : _d.origin) == null ? void 0 : _e.name);
  if (typeof originTitle === "string" && originTitle.trim()) {
    return originTitle.trim();
  }
  return void 0;
}
function buildCustomProductUpdatesFromEditedCacheItem(edited, orderLine) {
  const extend = (edited == null ? void 0 : edited._extend) || {};
  const unitPrice = resolveUnitPrice(edited, orderLine);
  const productName = resolveProductName(edited, orderLine);
  const existingMeta = orderLine.metadata || {};
  const existingOrigin = existingMeta.origin || {};
  return {
    num: resolveQuantity(edited, orderLine),
    note: String(edited.note ?? extend.note ?? orderLine.note ?? ""),
    selling_price: unitPrice,
    original_price: unitPrice,
    product_option_item: [],
    product_bundle: [],
    metadata: {
      ...existingMeta,
      origin: {
        ...existingOrigin,
        isCustomItem: true,
        ...productName ? {
          title: productName,
          name: productName
        } : {}
      }
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCustomProductUpdatesFromEditedCacheItem
});
