export type CheckoutReturnStatus = 'success' | 'cancelled' | 'timeout';

export interface CheckoutReturnExtraData {
	status: CheckoutReturnStatus;
	trade_no: string;
	pay_status?: number;
}

let closing = false;

export const isCheckoutClosing = () => closing;

export const returnToHostMiniProgram = (extraData: CheckoutReturnExtraData) => {
	if (closing) {
		return Promise.resolve(false);
	}

	closing = true;

	return new Promise<boolean>((resolve) => {
		// #ifdef MP-WEIXIN
		uni.navigateBackMiniProgram({
			extraData,
			success: () => resolve(true),
			fail: () => {
				closing = false;
				resolve(false);
			}
		});
		// #endif
		// #ifndef MP-WEIXIN
		closing = false;
		resolve(false);
		// #endif
	});
};

export const resetCheckoutClosing = () => {
	closing = false;
};
