import { makeRequest } from "@frontend/kitui"

import { parseAxiosError } from "@root/utils/parse-error"

import type { GetDiscountsResponse } from "@root/model/discount"

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

export async function getDiscounts(signal: AbortSignal | undefined): Promise<[GetDiscountsResponse | null, string?]> {
	try {
		const discountsResponse = await makeRequest<never, GetDiscountsResponse>({
			url: apiUrl + "/discounts",
			method: "get",
			useToken: true,
			signal,
		})

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

		return [null, `Ошибка получения списка скидок. ${error}`]
	}
}
