require("../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_utils = require("./utils.js");
const require_types = require("./types.js");
let react = require("react");
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _ant_design_icons = require("@ant-design/icons");
//#region src/plus/saleDetail/hooks/saleDetailActions/printActions.ts
const WRISTBAND_PRINT_TAGS = [
	"ticket",
	"product_package_ticket",
	"appointment_ticket",
	"session_ticket"
];
function hasOwnValue(record, key) {
	return record[key] !== void 0 && record[key] !== null;
}
function getSaleDetailPrintSource(dataSource) {
	var _ref, _dataSource$sales$sal, _dataSource$sales$sal2;
	return (_ref = (_dataSource$sales$sal = (_dataSource$sales$sal2 = dataSource.sales.salesDetail) === null || _dataSource$sales$sal2 === void 0 ? void 0 : _dataSource$sales$sal2.raw) !== null && _dataSource$sales$sal !== void 0 ? _dataSource$sales$sal : dataSource.sales.tempOrder) !== null && _ref !== void 0 ? _ref : {};
}
function getArrayValue(record, key) {
	const value = record[key];
	return Array.isArray(value) ? value : void 0;
}
function buildSaleDetailPrintData(dataSource, orderId) {
	const source = getSaleDetailPrintSource(dataSource);
	const paymentInfo = source.payment_info;
	const printData = { order_id: orderId };
	if (hasOwnValue(source, "machine_code_print_info")) printData.machine_code_print_info = source.machine_code_print_info;
	if (hasOwnValue(source, "machine_code_print_info_v2")) printData.machine_code_print_info_v2 = source.machine_code_print_info_v2;
	if (hasOwnValue(source, "small_ticket_data")) printData.small_ticket_data = source.small_ticket_data;
	else if (paymentInfo && hasOwnValue(paymentInfo, "small_ticket_data")) printData.small_ticket_data = paymentInfo.small_ticket_data;
	if (hasOwnValue(source, "voucher_ids")) printData.voucher_ids = source.voucher_ids;
	return printData;
}
function buildSaleDetailWristbandPrintData(dataSource, orderId) {
	const source = getSaleDetailPrintSource(dataSource);
	const machineCodePrintInfoV2 = getArrayValue(source, "machine_code_print_info_v2");
	const voucherIds = getArrayValue(source, "voucher_ids");
	const printData = { order_id: orderId };
	if (voucherIds === null || voucherIds === void 0 ? void 0 : voucherIds.length) {
		printData.voucher_ids = voucherIds;
		return printData;
	}
	const wristbandPrintInfo = machineCodePrintInfoV2 === null || machineCodePrintInfoV2 === void 0 ? void 0 : machineCodePrintInfoV2.filter((item) => WRISTBAND_PRINT_TAGS.includes(String((item === null || item === void 0 ? void 0 : item.tag) || "")));
	if (wristbandPrintInfo === null || wristbandPrintInfo === void 0 ? void 0 : wristbandPrintInfo.length) printData.machine_code_print_info_v2 = wristbandPrintInfo;
	return printData;
}
function isPrintSuccessful(result) {
	return (result === null || result === void 0 ? void 0 : result.status) === "success" || (result === null || result === void 0 ? void 0 : result.result) === true;
}
function useSaleDetailPrintAction(dataSource, printType, buildPrintData = buildSaleDetailPrintData) {
	var _context$appHelper;
	const [loading, setLoading] = (0, react.useState)(false);
	const context = require_useEngineContext.default();
	const action = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.action;
	const orderId = require_utils.resolveSaleDetailOrderId(dataSource);
	const handlePrint = (0, react.useCallback)(() => {
		if (loading) return;
		if (orderId == null) {
			antd.message.error(_pisell_utils.locales.getText("pisell2.sale-detail.missing-order-id"));
			return;
		}
		if (typeof action !== "function") {
			antd.message.error(_pisell_utils.locales.getText("pisell2.sale-detail.action-unavailable"));
			return;
		}
		setLoading(true);
		try {
			action({
				type: "webpos.handlePrint",
				data: {
					type: printType,
					data: buildPrintData(dataSource, orderId)
				},
				callback: (result) => {
					setLoading(false);
					if (isPrintSuccessful(result)) return;
					antd.message.error((result === null || result === void 0 ? void 0 : result.msg) || (result === null || result === void 0 ? void 0 : result.message) || _pisell_utils.locales.getText("pisell2.sale-detail.print-failed"));
				}
			});
		} catch (error) {
			setLoading(false);
			antd.message.error(String((error === null || error === void 0 ? void 0 : error.message) || _pisell_utils.locales.getText("pisell2.sale-detail.print-failed")));
		}
	}, [
		action,
		buildPrintData,
		dataSource,
		loading,
		orderId,
		printType
	]);
	return {
		disabled: orderId == null || typeof action !== "function" || loading,
		handlePrint,
		loading
	};
}
function usePrintReceiptsActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, require_types.EBizPrintType.INVOICE);
	return (0, react.useMemo)(() => ({
		id: "print-receipts",
		buttonProps: {
			block: true,
			children: _pisell_utils.locales.getText("pisell2.sale-detail.action.print-receipts"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: (0, react.createElement)(_ant_design_icons.PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintActionItem() {
	return (0, react.useMemo)(() => ({
		id: "print",
		isMore: true,
		buttonProps: {
			block: true,
			children: _pisell_utils.locales.getText("pisell2.sale-detail.action.print"),
			className: "pisell-sale-detail-button-actions__button",
			icon: (0, react.createElement)(_ant_design_icons.PrinterOutlined)
		}
	}), []);
}
function usePrintProductVoucherActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, require_types.EBizPrintType.WALLET);
	return (0, react.useMemo)(() => ({
		id: "print-product-voucher",
		buttonProps: {
			block: true,
			children: _pisell_utils.locales.getText("pisell2.sale-detail.action.print-product-voucher"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: (0, react.createElement)(_ant_design_icons.PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintWristbandActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, require_types.EBizPrintType.WRISTBAND, buildSaleDetailWristbandPrintData);
	return (0, react.useMemo)(() => ({
		id: "print-wristband",
		buttonProps: {
			block: true,
			children: _pisell_utils.locales.getText("pisell2.sale-detail.action.print-wristband"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: (0, react.createElement)(_ant_design_icons.PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintAllActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, require_types.EBizPrintType.PRINT_ALL);
	return (0, react.useMemo)(() => ({
		id: "print-all",
		buttonProps: {
			block: true,
			children: _pisell_utils.locales.getText("pisell2.sale-detail.action.print-all"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: (0, react.createElement)(_ant_design_icons.PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
//#endregion
exports.usePrintActionItem = usePrintActionItem;
exports.usePrintAllActionItem = usePrintAllActionItem;
exports.usePrintProductVoucherActionItem = usePrintProductVoucherActionItem;
exports.usePrintReceiptsActionItem = usePrintReceiptsActionItem;
exports.usePrintWristbandActionItem = usePrintWristbandActionItem;
