import { makeRequest } from "@frontend/kitui"
import { SendPaymentRequest, SendPaymentResponse } from "@root/model/wallet"
import { parseAxiosError } from "@root/utils/parse-error"

const apiUrl = process.env.REACT_APP_DOMAIN + "/customer"

const testPaymentBody: SendPaymentRequest = {
	type: "bankCard",
	amount: 15020,
	currency: "RUB",
	bankCard: {
		number: "RUB",
		expiryYear: "2021",
		expiryMonth: "05",
		csc: "05",
		cardholder: "IVAN IVANOV",
	},
	phoneNumber: "79000000000",
	login: "login_test",
	returnUrl: window.location.origin + "/wallet",
}

export async function sendPayment(
	{body = testPaymentBody, fromSquiz = false}: {body?: SendPaymentRequest, fromSquiz:boolean}
): Promise<[SendPaymentResponse | null, string?]> {
	if (fromSquiz) body.returnUrl = "squiz.pena.digital/list?action=fromhub"
	try {
		const sendPaymentResponse = await makeRequest<
      SendPaymentRequest,
      SendPaymentResponse
    >({
    	url: apiUrl + "/wallet",
    	contentType: true,
    	method: "POST",
    	useToken: true,
    	withCredentials: false,
    	body,
    })

		return [sendPaymentResponse]
	} catch (nativeError) {
		const [error] = parseAxiosError(nativeError)

		return [null, `Ошибка оплаты. ${error}`]
	}
}
