import useEngineContext from "../../../../hooks/useEngineContext.js";
import { resolveSaleDetailOrderId } from "./utils.js";
import { EBizPrintType } from "./types.js";
import { createElement, useCallback, useMemo, useState } from "react";
import { message } from "antd";
import { locales } from "@pisell/utils";
import { PrinterOutlined } from "@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] = useState(false);
	const context = useEngineContext();
	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 = resolveSaleDetailOrderId(dataSource);
	const handlePrint = useCallback(() => {
		if (loading) return;
		if (orderId == null) {
			message.error(locales.getText("pisell2.sale-detail.missing-order-id"));
			return;
		}
		if (typeof action !== "function") {
			message.error(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;
					message.error((result === null || result === void 0 ? void 0 : result.msg) || (result === null || result === void 0 ? void 0 : result.message) || locales.getText("pisell2.sale-detail.print-failed"));
				}
			});
		} catch (error) {
			setLoading(false);
			message.error(String((error === null || error === void 0 ? void 0 : error.message) || 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, EBizPrintType.INVOICE);
	return useMemo(() => ({
		id: "print-receipts",
		buttonProps: {
			block: true,
			children: locales.getText("pisell2.sale-detail.action.print-receipts"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: createElement(PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintActionItem() {
	return useMemo(() => ({
		id: "print",
		isMore: true,
		buttonProps: {
			block: true,
			children: locales.getText("pisell2.sale-detail.action.print"),
			className: "pisell-sale-detail-button-actions__button",
			icon: createElement(PrinterOutlined)
		}
	}), []);
}
function usePrintProductVoucherActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, EBizPrintType.WALLET);
	return useMemo(() => ({
		id: "print-product-voucher",
		buttonProps: {
			block: true,
			children: locales.getText("pisell2.sale-detail.action.print-product-voucher"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: createElement(PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintWristbandActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, EBizPrintType.WRISTBAND, buildSaleDetailWristbandPrintData);
	return useMemo(() => ({
		id: "print-wristband",
		buttonProps: {
			block: true,
			children: locales.getText("pisell2.sale-detail.action.print-wristband"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: createElement(PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
function usePrintAllActionItem({ dataSource }) {
	const { disabled, handlePrint, loading } = useSaleDetailPrintAction(dataSource, EBizPrintType.PRINT_ALL);
	return useMemo(() => ({
		id: "print-all",
		buttonProps: {
			block: true,
			children: locales.getText("pisell2.sale-detail.action.print-all"),
			className: "pisell-sale-detail-button-actions__button",
			disabled,
			icon: createElement(PrinterOutlined),
			loading,
			onClick: handlePrint
		}
	}), [
		disabled,
		handlePrint,
		loading
	]);
}
//#endregion
export { usePrintActionItem, usePrintAllActionItem, usePrintProductVoucherActionItem, usePrintReceiptsActionItem, usePrintWristbandActionItem };
