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/pisellFinancialSummary/utils/buildSummaryItems.ts
var buildSummaryItems_exports = {};
__export(buildSummaryItems_exports, {
  buildSummaryItems: () => buildSummaryItems
});
module.exports = __toCommonJS(buildSummaryItems_exports);
var import_utils = require("@pisell/utils");
function formatAmount(amount, options) {
  if (amount == null || typeof amount === "number" && Number.isNaN(amount)) {
    return options.missingValuePlaceholder ?? "—";
  }
  const symbol = options.currencySymbol ?? "";
  return (0, import_utils.formatAmountWithOptions)(amount, symbol, {
    precision: options.amountPrecision ?? 2,
    showCurrencySymbol: !!symbol,
    useThousandsSeparator: options.useThousandsSeparator ?? false,
    hideDecimalForWholeNumbers: options.hideDecimalForWholeNumbers ?? true
  });
}
function lineItemToSummary(item, options, level) {
  if (options.hideZeroAmountRows && item.value === 0) return null;
  const valueDisplay = item.value != null && !Number.isNaN(Number(item.value)) ? formatAmount(Number(item.value), options) : options.missingValuePlaceholder ?? "—";
  const children = (item.children ?? []).map((c) => lineItemToSummary(c, options, level + 1)).filter((x) => x != null);
  return {
    key: item.key ?? `line-${level}-${item.label}`,
    label: item.label,
    value: valueDisplay,
    children: children.length > 0 ? children : void 0,
    itemsCount: options.showItemsCount ? item.itemsCount : void 0,
    infoTooltip: options.showInfoIcon ? item.infoTooltip : void 0
  };
}
function totalBlockToChildren(block, options) {
  var _a;
  if (!((_a = block == null ? void 0 : block.children) == null ? void 0 : _a.length)) return [];
  return block.children.map((c) => lineItemToSummary(c, options, 2)).filter((x) => x != null);
}
function sumLineItems(items) {
  var _a;
  let sum = 0;
  for (const item of items) {
    if (typeof item.value === "number" && !Number.isNaN(item.value)) {
      sum += item.value;
    }
    if ((_a = item.children) == null ? void 0 : _a.length) {
      sum += sumLineItems(item.children);
    }
  }
  return sum;
}
function paymentToItem(p, options) {
  const labelParts = [p.methodName ?? "", p.time ?? ""].filter(Boolean);
  const label = labelParts.length > 0 ? labelParts.join(" · ") : void 0;
  return {
    key: p.key ?? `paid-${p.amount}-${p.methodName}`,
    label: label ?? "—",
    value: formatAmount(p.amount, options),
    extra: p.detail
  };
}
function refundToItem(r, options) {
  return {
    key: r.key ?? `refund-${r.amount}-${r.label}`,
    label: r.label ?? "—",
    value: formatAmount(r.amount, options),
    extra: r.detail
  };
}
function buildSummaryItems(data, options) {
  var _a, _b, _c;
  const items = [];
  const getTitle = (key, override) => override !== void 0 ? override : options.getText(key);
  const secondary = [];
  if (options.showTotal !== false && data.total) {
    const totalChildren = totalBlockToChildren(data.total, options);
    const totalValueRaw = data.total.value != null && !Number.isNaN(Number(data.total.value)) ? Number(data.total.value) : ((_a = data.total.children) == null ? void 0 : _a.length) ? sumLineItems(data.total.children) : void 0;
    const totalValue = totalValueRaw !== void 0 && !Number.isNaN(totalValueRaw) ? formatAmount(totalValueRaw, options) : void 0;
    secondary.push({
      key: "total",
      label: getTitle("pisell2.financialSummary.total", options.titleTotal),
      value: totalValue,
      children: totalChildren.length > 0 ? totalChildren : void 0
    });
  }
  if (!options.skipPaidAndRefund && options.showPaid !== false && ((_b = data.paid) == null ? void 0 : _b.length)) {
    const paidChildren = data.paid.map((p) => paymentToItem(p, options));
    secondary.push({
      key: "paid",
      label: getTitle("pisell2.financialSummary.paid", options.titlePaid),
      children: paidChildren
    });
  }
  if (!options.skipPaidAndRefund && options.showRefund !== false && ((_c = data.refund) == null ? void 0 : _c.length)) {
    const refundChildren = data.refund.map((r) => refundToItem(r, options));
    secondary.push({
      key: "refund",
      label: getTitle("pisell2.financialSummary.refund", options.titleRefund),
      children: refundChildren
    });
  }
  const groupNode = secondary.length > 0 ? {
    key: "summary-sections",
    label: "",
    value: void 0,
    children: secondary
  } : { key: "summary-sections", label: "", value: void 0, children: [] };
  items.push({
    key: "balance-due",
    label: getTitle(
      "pisell2.financialSummary.balanceDue",
      options.titleBalanceDue
    ),
    value: formatAmount(data.balanceDue, options),
    children: secondary.length > 0 ? [groupNode] : void 0
  });
  return items;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildSummaryItems
});
