import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { StatusEnum } from "../const.js";
import { StemTypeEnum, StepStatusEnum, getStepMapping } from "./const.js";
import { updateCustom, updateState, updateStatus, useContextData, useDispatch } from "../store/index.js";
import { useStoreRef } from "../hooks.js";
import payLog from "../utils/payLog.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Button } from "antd";
import { locales } from "@pisell/utils";
//#region src/components/eftposPay/windcave/receiptAction.tsx
/**
* @title: 按钮操作区
* @description: 仅包含签名
* @Author: hongbing.wang
* @Date: 2024-08-20 12:33
*/
const ReceiptAction = ({ api, onChange }) => {
	const dispatch = useDispatch();
	const { custom, mode } = useContextData();
	const { numRef } = useStoreRef();
	const listRef = useRef(custom === null || custom === void 0 ? void 0 : custom.list);
	const [receiptLoading, setReceiptLoading] = useState((custom === null || custom === void 0 ? void 0 : custom.receiptLoading) || false);
	const unloadRef = useRef(false);
	useEffect(() => {
		dispatch(updateCustom({ receiptLoading }));
	}, [receiptLoading]);
	const receiptPage = useMemo(() => (custom === null || custom === void 0 ? void 0 : custom.step) !== StepStatusEnum.Receipt ? "" : (custom === null || custom === void 0 ? void 0 : custom.receiptPage) || "receipt", [custom === null || custom === void 0 ? void 0 : custom.step, custom === null || custom === void 0 ? void 0 : custom.receiptPage]);
	useEffect(() => {
		listRef.current = custom === null || custom === void 0 ? void 0 : custom.list;
	}, [custom === null || custom === void 0 ? void 0 : custom.list]);
	/**
	* @title: 确认签名
	* @description: 确认签名 不阻碍-查询
	* @return null
	* @Author: hongbing.wang
	* @Date: 2024-08-12 11:30
	*/
	const onAccept = () => {
		payLog.log("Windcave：签名-点击确认");
		dispatch(updateStatus(StatusEnum.Loading));
		dispatch(updateCustom({ list: listRef.current.map((item) => item.index === StepStatusEnum.Receipt ? _objectSpread2(_objectSpread2({}, item), {}, { status: "resolve" }) : item) }));
		dispatch(updateState({ title: locales.getText("page.eftpos-pay.text.processing") }));
		setReceiptLoading(true);
		api.update({
			type: "YES",
			number: numRef.current
		}).then((res) => {
			if (unloadRef.current) return res;
			if (res && `${res.code}` === "200") {
				setReceiptLoading(false);
				const current = listRef.current.map((item) => {
					if (Number(item === null || item === void 0 ? void 0 : item.index) < StepStatusEnum.Wait) return _objectSpread2(_objectSpread2({}, item), {}, { status: "resolve" });
					return item;
				});
				const nextStep = getStepMapping(StepStatusEnum.Wait, { mode });
				if (nextStep) {
					current.push(nextStep);
					if (nextStep.type === StemTypeEnum.Passive) dispatch(updateStatus(StatusEnum.Question));
					dispatch(updateState({ title: nextStep.index < StepStatusEnum.Receipt ? locales.getText("page.eftpos-pay.text.loading")(mode) : nextStep.index === StepStatusEnum.Receipt ? locales.getText("page.eftpos-pay.text.waiting.signature") : locales.getText("page.eftpos-pay.text.loading")(mode) }));
				}
				dispatch(updateCustom({
					step: StepStatusEnum.Wait,
					list: current
				}));
				onChange(true);
				return res;
			}
			return Promise.reject(res);
		}).catch(() => {
			if (unloadRef.current) return;
			dispatch(updateCustom({ list: listRef.current.map((item) => item.index === StepStatusEnum.Receipt ? _objectSpread2(_objectSpread2({}, item), {}, { status: "pedding" }) : item) }));
			dispatch(updateState({ title: locales.getText("page.eftpos-pay.text.waiting.signature") }));
			dispatch(updateStatus(StatusEnum.Question));
			setReceiptLoading(false);
			onChange(false);
		});
	};
	/**
	* @title: 后退
	* @description: 
	* @Author: hongbing.wang
	* @Date: 2024-08-20 12:34
	*/
	const onBack = () => {
		payLog.log("Windcave：签名-点击后退");
		dispatch(updateState({
			title: locales.getText("page.eftpos-pay.text.waiting.signature"),
			status: StatusEnum.Question
		}));
		dispatch(updateCustom({ receiptPage: "receipt" }));
	};
	/**
	* @title: 取消签名
	* @description: 取消签名 不阻碍-状态查询
	* @return null
	* @Author: hongbing.wang
	* @Date: 2024-08-12 11:30
	*/
	const onConfirm = () => {
		payLog.log("Windcave：签名-点击取消");
		dispatch(updateState({
			title: locales.getText("page.eftpos-pay.text.processing"),
			status: StatusEnum.Loading
		}));
		dispatch(updateCustom({
			list: listRef.current.map((item) => item.index === StepStatusEnum.Receipt ? _objectSpread2(_objectSpread2({}, item), {}, { status: "reject" }) : item),
			receiptPage: "receipt"
		}));
		setReceiptLoading(true);
		api.update({
			type: "NO",
			number: numRef.current
		}).then((res) => {
			if (unloadRef.current) return res;
			setReceiptLoading(false);
			if (res && `${res.code}` === "200") {
				const current = listRef.current.map((item) => {
					if (Number(item === null || item === void 0 ? void 0 : item.index) === StepStatusEnum.Receipt) return _objectSpread2(_objectSpread2({}, item), {}, {
						resolve: item.reject,
						status: "resolve"
					});
					if (Number(item === null || item === void 0 ? void 0 : item.index) < StepStatusEnum.Wait) return _objectSpread2(_objectSpread2({}, item), {}, { status: "resolve" });
					return item;
				});
				const nextStep = getStepMapping(StepStatusEnum.Wait, { mode });
				if (nextStep) {
					nextStep.pedding = locales.getText("page.eftpos-pay.text.waiting.decline.signature");
					if (nextStep.type === StemTypeEnum.Passive) dispatch(updateStatus(StatusEnum.Question));
					dispatch(updateState({ title: nextStep.index < StepStatusEnum.Receipt ? locales.getText("page.eftpos-pay.text.loading")(mode) : nextStep.index === StepStatusEnum.Receipt ? locales.getText("page.eftpos-pay.text.waiting.signature") : locales.getText("page.eftpos-pay.text.loading")(mode) }));
					current.push(nextStep);
				}
				dispatch(updateCustom({
					step: StepStatusEnum.Wait,
					list: current
				}));
				onChange(true);
				return res;
			}
			return Promise.reject(res);
		}).catch(() => {
			if (unloadRef.current) return;
			onChange(false);
			setReceiptLoading(false);
			dispatch(updateCustom({ list: listRef.current.map((item) => item.index === StepStatusEnum.Receipt ? _objectSpread2(_objectSpread2({}, item), {}, { status: "pedding" }) : item) }));
			dispatch(updateState({ title: locales.getText("page.eftpos-pay.text.waiting.signature") }));
			dispatch(updateStatus(StatusEnum.Question));
			setReceiptLoading(false);
		});
	};
	/**
	* @title: 前往取消签名
	* @description: 前往取消签名确认流程
	* @Author: hongbing.wang
	* @Date: 2024-08-20 12:40
	*/
	const onDecline = () => {
		payLog.log("Windcave：签名-点击取消");
		dispatch(updateState({
			title: locales.getText("page.eftpos-pay.text.signature.title"),
			status: StatusEnum.Warn,
			custom: _objectSpread2(_objectSpread2({}, custom), {}, { receiptPage: "again" })
		}));
	};
	useEffect(() => () => {
		unloadRef.current = true;
	}, []);
	if (receiptLoading) return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		disabled: true
	}, locales.getText("page.eftpos-pay.text.leave")));
	if (receiptPage === "receipt") return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onDecline
	}, locales.getText("page.eftpos-pay.text.decline")), /* @__PURE__ */ React.createElement(Button, {
		block: true,
		type: "primary",
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onAccept
	}, locales.getText("page.eftpos-pay.text.accept")));
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onBack
	}, locales.getText("page.eftpos-pay.text.back")), /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		type: "primary",
		onClick: onConfirm,
		disabled: receiptLoading
	}, locales.getText("page.eftpos-pay.text.confirm.decline")));
};
/**
* @title: 按钮操作区
* @description: 不包含签名
* @Author: hongbing.wang
* @Date: 2024-08-20 12:32
*/
const Action = ({ index, onCancel, disabled }) => {
	useDispatch();
	const { custom, mode } = useContextData();
	/**
	* @title: 前往手动标记页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 12:18
	*/
	if (index < StepStatusEnum.Application) return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onCancel,
		disabled
	}, locales.getText("page.eftpos-pay.text.cancel")));
	if (index >= StepStatusEnum.Application && index < StepStatusEnum.Receipt) return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		disabled: true
	}, locales.getText("page.eftpos-pay.text.leave")));
	if (index === StepStatusEnum.Receipt) return null;
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		disabled: true
	}, locales.getText("page.eftpos-pay.text.leave")));
};
//#endregion
export { ReceiptAction, Action as default };
