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