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/components/pisellCashCounting/components/CashCountingModal/index.tsx
var CashCountingModal_exports = {};
__export(CashCountingModal_exports, {
  default: () => CashCountingModal_default
});
module.exports = __toCommonJS(CashCountingModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_NumericStepper = __toESM(require("../../../../pro/NumericStepper"));
var import_utils = require("@pisell/utils");
var import_icons = require("@ant-design/icons");
var import_utils2 = require("../../utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_useResponsive = require("../../../../hooks/useResponsive");
var import_index = require("./index.less");
var PREFIX = "pisell-private-cash-counting-modal";
var formatAmount = (value, symbol, precision) => {
  const formatter = new Intl.NumberFormat(void 0, {
    minimumFractionDigits: precision,
    maximumFractionDigits: precision
  });
  return `${symbol}${formatter.format(value)}`;
};
var CashCountingModal = (0, import_react.forwardRef)(
  (props, ref) => {
    var _a;
    const {
      currencyCode,
      currencyDenominations,
      decimalPlaces,
      symbol,
      modalWidth,
      onCancel,
      onConfirm
    } = props;
    const context = (0, import_useEngineContext.default)();
    const { isTerminal } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
    const [open, setOpen] = (0, import_react.useState)(false);
    const [draftQuantities, setDraftQuantities] = (0, import_react.useState)({});
    const isIpad = (0, import_useResponsive.useResponsive)(1200);
    const isMobile = (0, import_useResponsive.useResponsive)(450);
    (0, import_react.useImperativeHandle)(ref, () => ({
      open: ({ initialQuantities }) => {
        setDraftQuantities(initialQuantities);
        setOpen(true);
      },
      close: () => setOpen(false)
    }));
    const groupedDenominations = (0, import_react.useMemo)(() => {
      return currencyDenominations.reduce(
        (acc, item) => {
          acc[item.denomination_type].push(item);
          return acc;
        },
        { bill: [], coin: [] }
      );
    }, [currencyDenominations]);
    const calculatedDetails = (0, import_react.useMemo)(() => {
      return currencyDenominations.map((d) => {
        const qty = draftQuantities[(0, import_utils2.getDenominationId)(d)] ?? 0;
        return { denomination: d, quantity: qty, amount: (0, import_utils2.normalizeAmount)(d.denomination_value * qty, decimalPlaces) };
      });
    }, [currencyDenominations, draftQuantities, decimalPlaces, import_utils2.getDenominationId, import_utils2.normalizeAmount]);
    const billSubtotal = (0, import_react.useMemo)(
      () => calculatedDetails.filter((i) => i.denomination.denomination_type === "bill").reduce((s, i) => s + i.amount, 0),
      [calculatedDetails]
    );
    const coinSubtotal = (0, import_react.useMemo)(
      () => calculatedDetails.filter((i) => i.denomination.denomination_type === "coin").reduce((s, i) => s + i.amount, 0),
      [calculatedDetails]
    );
    const totalAmount = (0, import_react.useMemo)(() => billSubtotal + coinSubtotal, [billSubtotal, coinSubtotal]);
    const handleClose = () => {
      setOpen(false);
      if (typeof onCancel === "function") {
        onCancel();
      }
    };
    const handleClear = () => {
      const reset = {};
      currencyDenominations.forEach((d) => reset[(0, import_utils2.getDenominationId)(d)] = 0);
      setDraftQuantities(reset);
    };
    const handleConfirm = () => {
      const payload = {
        amount: totalAmount,
        currencyCode,
        decimalPlaces,
        billSubtotal,
        coinSubtotal,
        details: calculatedDetails,
        quantities: draftQuantities
      };
      if (typeof onConfirm === "function") {
        onConfirm(payload);
      }
      setOpen(false);
    };
    const renderSection = (type, title, subtotal) => {
      const items = groupedDenominations[type];
      if (!items.length) return null;
      return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__section` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__section-header` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__section-title` }, /* @__PURE__ */ import_react.default.createElement("span", { className: `${PREFIX}__dot ${PREFIX}__dot--${type}` }), /* @__PURE__ */ import_react.default.createElement("span", null, title)), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__section-subtotal` }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell-cash-counting.subtotal")), /* @__PURE__ */ import_react.default.createElement("strong", null, formatAmount(subtotal, symbol, decimalPlaces)))), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__denomination-list` }, items.map((item) => {
        const id = (0, import_utils2.getDenominationId)(item);
        const qty = draftQuantities[id] ?? 0;
        const amt = (0, import_utils2.normalizeAmount)(item.denomination_value * qty, decimalPlaces);
        return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__denomination-row`, key: id }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__denomination-left` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__denomination-label ${PREFIX}__denomination-label--${type}` }, item.denomination_label), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__stepper-wrap` }, /* @__PURE__ */ import_react.default.createElement(
          import_NumericStepper.default,
          {
            className: `${PREFIX}__stepper`,
            min: 0,
            max: 9999,
            value: qty,
            plusOnlyAtZero: false,
            shape: "square",
            onChange: (val) => setDraftQuantities((prev) => ({ ...prev, [id]: val }))
          }
        ))), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__row-amount` }, formatAmount(amt, symbol, decimalPlaces)));
      })));
    };
    const renderHeader = (0, import_react.useCallback)(() => {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__modal-header` }, /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-cash", className: `${PREFIX}__modal-header-icon` }), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__modal-header-title` }, import_utils.locales.getText("pisell-cash-counting.countCash")), /* @__PURE__ */ import_react.default.createElement(import_materials.Tag, { color: "blue", style: { border: "none" } }, currencyCode));
    }, [currencyCode]);
    const renderFooter = (0, import_react.useCallback)(() => {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__footer` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__summary` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__summary-row` }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell-cash-counting.billsSubtotal")), /* @__PURE__ */ import_react.default.createElement("strong", null, formatAmount(billSubtotal, symbol, decimalPlaces))), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__summary-row` }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell-cash-counting.coinsSubtotal")), /* @__PURE__ */ import_react.default.createElement("strong", null, formatAmount(coinSubtotal, symbol, decimalPlaces))), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__total-row` }, /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell-cash-counting.total")), /* @__PURE__ */ import_react.default.createElement("strong", null, formatAmount(totalAmount, symbol, decimalPlaces)))), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__actions` }, /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { type: "text", size: "large", onClick: handleClear }, import_utils.locales.getText("pisell-cash-counting.clearAll")), /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { size: "large", onClick: handleClose }, import_utils.locales.getText("pisell-cash-counting.cancel")), /* @__PURE__ */ import_react.default.createElement(import_materials.Button, { type: "primary", size: "large", disabled: !currencyDenominations.length, onClick: handleConfirm }, import_utils.locales.getText("pisell-cash-counting.confirm"))));
    }, [coinSubtotal, billSubtotal, totalAmount, currencyDenominations, handleClear, handleClose, handleConfirm]);
    return /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        destroyOnClose: true,
        maskClosable: false,
        footer: renderFooter(),
        open,
        title: renderHeader(),
        className: `${PREFIX}__modal-wrap`,
        bodyStyle: { padding: 0 },
        width: modalWidth ? modalWidth : (isTerminal == null ? void 0 : isTerminal()) ? "80%" : "880px",
        onCancel: handleClose
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__modal` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__modal-intro` }, /* @__PURE__ */ import_react.default.createElement(import_icons.ExclamationCircleOutlined, { style: { fontSize: 18, color: "#F2A63A" } }), import_utils.locales.getText("pisell-cash-counting.subtitle")), /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__scroll ${isIpad || isMobile ? `${PREFIX}__scroll-column` : ""} ${isMobile ? `${PREFIX}__scroll-mobile` : ""}` }, currencyDenominations.length === 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__empty` }, import_utils.locales.getText("pisell-cash-counting.empty")) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderSection("bill", import_utils.locales.getText("pisell-cash-counting.bills"), billSubtotal), renderSection("coin", import_utils.locales.getText("pisell-cash-counting.coins"), coinSubtotal))))
    );
  }
);
var CashCountingModal_default = CashCountingModal;
