import { type IorangePaymentStarter, getTransactionsData } from "@/utils/requests/orangePayment";

import { useMutation } from "@tanstack/react-query";
import * as WebBrowser from "expo-web-browser";
import { useState } from "react";

const paymentObjectDefault: IorangePaymentStarter = {
	// biome-ignore lint/style/useNamingConvention: <api expect type_paiement>
	type_paiement: 1,
	marchand: "1",
	service: "1",
	montant: 0,
	numero: "0707070707",
	commentaire: "Un commentaire",
};
const useOrangeMoney = () => {
	const [isBrowserOpen, setIsBrowserOpen] = useState(false);

	const handlePaymentUsingBrowser = async (url: string) => {
		setIsBrowserOpen(true);
		const result = await WebBrowser.openBrowserAsync(url);
		// setResult(result);
		console.log("result", result);
		setIsBrowserOpen(false);
	};

	const { isPending, mutate } = useMutation({
		mutationFn: (amount: number) =>
			getTransactionsData({
				...paymentObjectDefault,
				montant: amount,
			}),
		onSuccess: async (data) => {
			await handlePaymentUsingBrowser(data.payment_url);
			// setResult(result);
		},
		onError: (err) => {
			console.log("om start error ", err);
		},
	});

	return { getOmPaymentUrl: mutate, isBrowserOpen, isWaitingForOmPaymentUrl: isPending };
};

export default useOrangeMoney;
