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/solution/BaseSales/utils/parseSalesResponse.ts
var parseSalesResponse_exports = {};
__export(parseSalesResponse_exports, {
  parseSalesResponse: () => parseSalesResponse
});
module.exports = __toCommonJS(parseSalesResponse_exports);
var HEADER_KEYS = [
  "order_id",
  "order_number",
  "shop_order_number",
  "shop_full_order_number",
  "type",
  "business_code",
  "platform",
  "sales_channel",
  "order_sales_channel",
  "vouchers",
  "status",
  "payment_status",
  "shipping_status",
  "customer_id",
  "customer_name",
  "country_calling_code",
  "phone",
  "email",
  "is_price_include_tax",
  "tax_title",
  "tax_country_code",
  "currency_code",
  "currency_symbol",
  "currency_format",
  "is_deposit",
  "deposit_amount",
  "shop_discount",
  "surcharge_fee",
  "note",
  "schedule_date",
  "created_at",
  "updated_at",
  "metadata",
  "total_amount",
  "shop_id",
  "create_date",
  "holder",
  "relation_forms",
  "contacts",
  "contacts_info"
];
function pickHeader(raw) {
  const header = { order_id: (raw == null ? void 0 : raw.order_id) ?? null };
  if (!raw || typeof raw !== "object")
    return header;
  for (const key of HEADER_KEYS) {
    if (raw[key] !== void 0)
      header[key] = raw[key];
  }
  return header;
}
function pickCustomer(raw) {
  if (!raw || typeof raw !== "object")
    return null;
  const explicit = raw.customer;
  if (explicit && typeof explicit === "object") {
    return {
      id: explicit.id ?? raw.customer_id ?? null,
      name: explicit.name ?? raw.customer_name ?? "",
      phone: explicit.phone ?? raw.phone,
      email: explicit.email ?? raw.email,
      country_calling_code: explicit.country_calling_code ?? raw.country_calling_code,
      ...explicit
    };
  }
  if (raw.customer_id == null && !raw.customer_name)
    return null;
  return {
    id: raw.customer_id ?? null,
    name: raw.customer_name ?? "",
    phone: raw.phone,
    email: raw.email,
    country_calling_code: raw.country_calling_code
  };
}
function isRootBooking(booking) {
  if (!booking || typeof booking !== "object")
    return false;
  const parentId = booking.parent_id;
  if (parentId === 0 || parentId === null || parentId === void 0)
    return true;
  return false;
}
function indexBookingsByProductUid(bookings) {
  var _a;
  const result = {};
  for (const b of bookings) {
    const uid = b && (b.product_uid || ((_a = b.metadata) == null ? void 0 : _a.product_uid)) || "";
    if (!uid)
      continue;
    const key = String(uid);
    if (!result[key])
      result[key] = [];
    result[key].push(b);
  }
  return result;
}
function parseSalesResponse(input) {
  const raw = input && typeof input === "object" && input.data && input.order_id == null ? input.data : input || {};
  const productsRaw = Array.isArray(raw == null ? void 0 : raw.products) ? raw.products : [];
  const products = productsRaw.map((p, index) => {
    const row = { ...p || {} };
    const metadata = row.metadata && typeof row.metadata === "object" ? { ...row.metadata } : {};
    if (!metadata.unique_identification_number) {
      metadata.unique_identification_number = `sales-product-${row.product_id ?? "unknown"}-${row.product_variant_id ?? 0}-${index}`;
    }
    delete metadata.price_schema_version;
    delete row.identity_key;
    return { ...row, metadata };
  });
  const bookingsRaw = Array.isArray(raw == null ? void 0 : raw.bookings) ? raw.bookings : [];
  const bookings = bookingsRaw.map((b) => ({ ...b || {} }));
  const rootBooking = bookings.find(isRootBooking) || null;
  const bookingsByProductUid = indexBookingsByProductUid(bookings);
  const payments = Array.isArray(raw == null ? void 0 : raw.payments) ? raw.payments.map((p) => ({ ...p || {} })) : [];
  const surcharges = Array.isArray(raw == null ? void 0 : raw.surcharges) ? raw.surcharges.map((s) => ({ ...s || {} })) : [];
  const discount_list = Array.isArray(raw == null ? void 0 : raw.discount_list) ? raw.discount_list.map((d) => ({ ...d || {} })) : [];
  const summary = (raw == null ? void 0 : raw.summary) && typeof raw.summary === "object" ? { ...raw.summary } : {};
  return {
    header: pickHeader(raw),
    products,
    bookings,
    rootBooking,
    bookingsByProductUid,
    payments,
    surcharges,
    discount_list,
    customer: pickCustomer(raw),
    summary,
    raw
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  parseSalesResponse
});
