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/components/scanOrder/utils/transformProductDetail.ts
var transformProductDetail_exports = {};
__export(transformProductDetail_exports, {
  transformProductDetailToOrderProduct: () => transformProductDetailToOrderProduct
});
module.exports = __toCommonJS(transformProductDetail_exports);
function toNumber(value, fallback = 0) {
  const parsedValue = Number(value);
  if (Number.isNaN(parsedValue)) return fallback;
  return parsedValue;
}
function toProductId(value) {
  if (value === null || value === void 0 || value === "") return null;
  const parsedValue = Number(value);
  if (Number.isNaN(parsedValue)) return null;
  return parsedValue;
}
function toSafePositiveInt(value, fallback = 1) {
  const parsedValue = Math.floor(Number(value));
  if (Number.isNaN(parsedValue) || parsedValue < 1) return fallback;
  return parsedValue;
}
function toPriceString(value, fallback = "0.00") {
  const parsedValue = Number(value);
  if (Number.isNaN(parsedValue)) return fallback;
  return parsedValue.toFixed(2);
}
function normalizeOptionItems(optionItems) {
  if (!Array.isArray(optionItems)) return [];
  return optionItems.map((optionItem) => {
    const optionItemId = toNumber(
      (optionItem == null ? void 0 : optionItem.product_option_item_id) ?? (optionItem == null ? void 0 : optionItem.option_group_item_id)
    );
    return {
      product_option_item_id: optionItemId,
      option_group_id: toNumber(optionItem == null ? void 0 : optionItem.option_group_id),
      num: toSafePositiveInt(optionItem == null ? void 0 : optionItem.num, 1),
      price: toNumber(optionItem == null ? void 0 : optionItem.price, 0)
    };
  });
}
function normalizeBundleItems(bundleItems) {
  if (!Array.isArray(bundleItems)) return [];
  return bundleItems.map((bundleItem) => {
    return {
      ...bundleItem,
      option: normalizeOptionItems(bundleItem == null ? void 0 : bundleItem.option)
    };
  });
}
function findVariantById(origin, variantId) {
  if (!origin || !variantId || variantId <= 0) return null;
  const variants = Array.isArray(origin == null ? void 0 : origin.variant) ? origin.variant : [];
  const matched = variants.find((v) => Number(v == null ? void 0 : v.id) === variantId);
  return matched || null;
}
function transformProductDetailToOrderProduct(params) {
  var _a;
  const { payload, fallbackProductId = null, sourceItem = {} } = params;
  if (!payload || typeof payload !== "object") return null;
  const productId = toProductId(
    payload.product_id ?? payload.id ?? (sourceItem == null ? void 0 : sourceItem.product_id) ?? fallbackProductId
  );
  if (productId === null) return null;
  const productVariantId = toNumber(
    payload.product_variant_id ?? payload.variant_id ?? (sourceItem == null ? void 0 : sourceItem.product_variant_id),
    0
  );
  const callbackOrigin = (payload == null ? void 0 : payload.origin) || ((_a = payload == null ? void 0 : payload._origin) == null ? void 0 : _a.sourceItem) || (payload == null ? void 0 : payload._origin) || {};
  const matchedVariant = findVariantById(callbackOrigin, productVariantId) ?? findVariantById(sourceItem, productVariantId);
  const sellingPrice = toPriceString(
    (payload == null ? void 0 : payload.price) ?? (payload == null ? void 0 : payload.selling_price) ?? (matchedVariant == null ? void 0 : matchedVariant.price) ?? (matchedVariant == null ? void 0 : matchedVariant.base_price) ?? (callbackOrigin == null ? void 0 : callbackOrigin.price) ?? (sourceItem == null ? void 0 : sourceItem.price) ?? (sourceItem == null ? void 0 : sourceItem.selling_price) ?? (sourceItem == null ? void 0 : sourceItem.base_price),
    "0.00"
  );
  const originalPrice = sellingPrice;
  const taxFee = toPriceString(
    (payload == null ? void 0 : payload.tax_fee) ?? (matchedVariant == null ? void 0 : matchedVariant.tax_fee) ?? (callbackOrigin == null ? void 0 : callbackOrigin.tax_fee) ?? (sourceItem == null ? void 0 : sourceItem.tax_fee),
    "0.00"
  );
  const isChargeTax = toNumber(
    (payload == null ? void 0 : payload.is_charge_tax) ?? (matchedVariant == null ? void 0 : matchedVariant.is_charge_tax) ?? (callbackOrigin == null ? void 0 : callbackOrigin.is_charge_tax) ?? (sourceItem == null ? void 0 : sourceItem.is_charge_tax),
    0
  );
  return {
    product_id: productId,
    product_variant_id: productVariantId,
    num: toSafePositiveInt(payload.quantity ?? payload.num, 1),
    product_option_item: normalizeOptionItems(
      payload.option ?? payload.product_option_item
    ),
    product_bundle: normalizeBundleItems(
      payload.bundle ?? payload.product_bundle
    ),
    selling_price: sellingPrice,
    original_price: originalPrice,
    tax_fee: taxFee,
    is_charge_tax: isChargeTax,
    metadata: {
      unique: payload.unique,
      session: payload.session,
      form: payload.form,
      service_times: payload.service_times,
      machine_code: payload.machine_code,
      schedule_id: payload.schedule_id,
      duration: payload.duration,
      policy_id: payload.policy_id,
      source_shop_id: payload.source_shop_id,
      origin: payload.origin
    },
    _origin: {
      ...sourceItem || {},
      callbackData: payload
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  transformProductDetailToOrderProduct
});
