var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellFinancialSummary/PisellFinancialSummary.tsx
var PisellFinancialSummary_exports = {};
__export(PisellFinancialSummary_exports, {
  PisellFinancialSummary: () => PisellFinancialSummary
});
module.exports = __toCommonJS(PisellFinancialSummary_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_utils2 = require("./utils");
var import_Collapse = require("./components/Collapse");
var import_PisellFinancialSummary = require("./PisellFinancialSummary.less");
var PREFIX_CLS = "pisell-financial-summary";
var FIGMA_GAPS = { row: 8, rowLeft: 8, meta: 4, rowRight: 6 };
var DEFAULT_LEVEL_CONFIG = {
  1: {
    labelTextStyle: {
      fontSize: 16,
      fontWeight: 700,
      color: "#1B1B1B",
      lineHeight: "24px"
    },
    valueTextStyle: {
      fontSize: 16,
      fontWeight: 700,
      color: "#DC6803",
      lineHeight: "24px"
    },
    expandIconStyle: { size: 16, width: 16, height: 16 }
  },
  2: {
    labelTextStyle: {
      fontSize: 16,
      fontWeight: 700,
      color: "#1B1B1B",
      lineHeight: "24px"
    },
    valueTextStyle: {
      fontSize: 16,
      fontWeight: 700,
      color: "#DC6803",
      lineHeight: "24px"
    },
    expandIconStyle: { size: 16, width: 16, height: 16 },
    divider: { show: true, color: "#D0D5DD", style: "solid" },
    showDotSeparator: false
  },
  3: {
    labelTextStyle: {
      fontSize: 16,
      fontWeight: 500,
      color: "#000000",
      lineHeight: "24px"
    },
    valueTextStyle: {
      fontSize: 16,
      fontWeight: 500,
      color: "#000000",
      lineHeight: "24px"
    },
    metaTextStyle: {
      fontSize: 16,
      fontWeight: 400,
      color: "#667085",
      lineHeight: "24px"
    },
    infoIconStyle: { size: 16, width: 16, height: 16 }
  }
};
var PisellFinancialSummary = import_react.default.memo(
  ({
    data,
    showTotal = true,
    showPaid = true,
    showRefund = true,
    showItemsCount = true,
    showInfoIcon = true,
    hideZeroAmountRows = false,
    titleBalanceDue,
    titleTotal,
    titlePaid,
    titleRefund,
    currencySymbol = "",
    amountPrecision = 2,
    useThousandsSeparator = false,
    hideDecimalForWholeNumbers = true,
    missingValuePlaceholder = "—",
    defaultExpandedLevel = 0,
    className,
    style
  }) => {
    var _a, _b;
    const formatOpts = (0, import_react.useMemo)(
      () => ({
        precision: amountPrecision ?? 2,
        showCurrencySymbol: !!currencySymbol,
        useThousandsSeparator: useThousandsSeparator ?? false,
        hideDecimalForWholeNumbers: hideDecimalForWholeNumbers ?? true
      }),
      [
        amountPrecision,
        currencySymbol,
        useThousandsSeparator,
        hideDecimalForWholeNumbers
      ]
    );
    const formatAmountDisplay = (0, import_react.useCallback)(
      (amount) => (0, import_utils.formatAmountWithOptions)(amount, currencySymbol ?? "", formatOpts),
      [currencySymbol, formatOpts]
    );
    const getTitle = (0, import_react.useCallback)(
      (key, override) => override !== void 0 ? override : import_utils.locales.getText(key),
      [titleBalanceDue, titleTotal, titlePaid, titleRefund]
    );
    const paidSum = (0, import_react.useMemo)(
      () => (data.paid ?? []).reduce((s, p) => s + p.amount, 0),
      [data.paid]
    );
    const refundSum = (0, import_react.useMemo)(
      () => (data.refund ?? []).reduce((s, r) => s + r.amount, 0),
      [data.refund]
    );
    const useCollapseForPaidRefund = showPaid !== false && (((_a = data.paid) == null ? void 0 : _a.length) ?? 0) > 0 || showRefund !== false && (((_b = data.refund) == null ? void 0 : _b.length) ?? 0) > 0;
    const items = (0, import_react.useMemo)(() => {
      var _a2, _b2, _c;
      const base = (0, import_utils2.buildSummaryItems)(data, {
        currencySymbol,
        amountPrecision,
        useThousandsSeparator,
        hideDecimalForWholeNumbers,
        showItemsCount,
        showInfoIcon,
        hideZeroAmountRows,
        missingValuePlaceholder,
        getText: (key) => import_utils.locales.getText(key),
        titleBalanceDue,
        titleTotal,
        titlePaid,
        titleRefund,
        showTotal,
        showPaid: useCollapseForPaidRefund ? false : showPaid,
        showRefund: useCollapseForPaidRefund ? false : showRefund,
        skipPaidAndRefund: useCollapseForPaidRefund
      });
      if (!useCollapseForPaidRefund) return base;
      const list = base.slice();
      const root = list[0];
      const group = (_a2 = root == null ? void 0 : root.children) == null ? void 0 : _a2[0];
      if (!(group == null ? void 0 : group.children)) return base;
      const newChildren = group.children.slice();
      if (showPaid !== false && (((_b2 = data.paid) == null ? void 0 : _b2.length) ?? 0) > 0) {
        newChildren.push({
          key: "paid",
          label: getTitle("pisell2.financialSummary.paid", titlePaid),
          value: formatAmountDisplay(paidSum),
          expandedContent: /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              className: `${PREFIX_CLS}-collapse-list`,
              style: { marginTop: 8 }
            },
            (data.paid ?? []).map((p, i) => /* @__PURE__ */ import_react.default.createElement(
              PaymentCollapseItem,
              {
                key: p.key ?? i,
                item: p,
                formatAmount: formatAmountDisplay
              }
            ))
          )
        });
      }
      if (showRefund !== false && (((_c = data.refund) == null ? void 0 : _c.length) ?? 0) > 0) {
        newChildren.push({
          key: "refund",
          label: getTitle("pisell2.financialSummary.refund", titleRefund),
          value: formatAmountDisplay(refundSum),
          expandedContent: /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              className: `${PREFIX_CLS}-collapse-list`,
              style: { marginTop: 8 }
            },
            (data.refund ?? []).map((r, i) => /* @__PURE__ */ import_react.default.createElement(
              RefundCollapseItem,
              {
                key: r.key ?? i,
                item: r,
                formatAmount: formatAmountDisplay
              }
            ))
          )
        });
      }
      return [{ ...root, children: [{ ...group, children: newChildren }] }];
    }, [
      data,
      currencySymbol,
      amountPrecision,
      useThousandsSeparator,
      hideDecimalForWholeNumbers,
      showItemsCount,
      showInfoIcon,
      hideZeroAmountRows,
      missingValuePlaceholder,
      titleBalanceDue,
      titleTotal,
      titlePaid,
      titleRefund,
      showTotal,
      showPaid,
      showRefund,
      getTitle,
      formatAmountDisplay,
      paidSum,
      refundSum,
      useCollapseForPaidRefund
    ]);
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(PREFIX_CLS, className), style }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellHierarchicalSummaryList,
      {
        items,
        indent: 16,
        rowGap: 8,
        padding: 20,
        gaps: FIGMA_GAPS,
        levelConfig: DEFAULT_LEVEL_CONFIG,
        defaultExpandedLevel
      }
    ));
  }
);
function paymentToCollapseProps(p, formatAmount) {
  var _a;
  const bodyRows = (((_a = p.bodyRows) == null ? void 0 : _a.length) ?? 0) > 0 ? (p.bodyRows ?? []).map((r) => ({
    label: r.label,
    amount: r.amount ?? formatAmount(p.amount),
    action: r.action
  })) : [
    {
      label: p.methodName ?? "—",
      amount: formatAmount(p.amount),
      action: { label: "Void", onClick: void 0 }
    }
  ];
  return {
    headMeta: {
      time: p.time,
      date: p.date,
      customer: p.customer,
      channel: p.channel
    },
    headAmount: formatAmount(p.amount),
    bodyRows
  };
}
function PaymentCollapseItem({
  item,
  formatAmount
}) {
  const { headMeta, headAmount, bodyRows } = (0, import_react.useMemo)(
    () => paymentToCollapseProps(item, formatAmount),
    [item, formatAmount]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_Collapse.Collapse,
    {
      headMeta,
      headAmount,
      defaultExpanded: true,
      bodyRows
    }
  );
}
function RefundCollapseItem({
  item,
  formatAmount
}) {
  var _a;
  const bodyRows = (((_a = item.bodyRows) == null ? void 0 : _a.length) ?? 0) > 0 ? item.bodyRows ?? [] : [
    {
      label: item.label ?? "—",
      amount: formatAmount(item.amount),
      action: { label: "Void", onClick: void 0 }
    }
  ];
  return /* @__PURE__ */ import_react.default.createElement(
    import_Collapse.Collapse,
    {
      headMeta: {
        time: item.time,
        date: item.date,
        customer: item.customer,
        channel: item.channel
      },
      headAmount: formatAmount(item.amount),
      defaultExpanded: true,
      bodyRows
    }
  );
}
PisellFinancialSummary.displayName = "PisellFinancialSummary";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PisellFinancialSummary
});
