import {useEffect, useState} from "react"
import { useTariffStore } from "@root/stores/tariffs";
import {getRecentlyPurchasedTariffs} from "@root/api/recentlyPurchasedTariffs"
import { getTariffById } from "@root/api/tariff"
import {useHistoryStore} from "@stores/history"

export const useRecentlyPurchasedTariffs = () => {
	const [recentlyPurchased, setRecentlyPurchased] = useState<any>([])
	const tariffs = useTariffStore((state) => state.tariffs);
	const historyData = useHistoryStore(state => state.history);
	useEffect(() => {
		console.log("юзэффект начинает работаььб")
		async function fetchData() {
			try {
				const [response, errorMsg] = await getRecentlyPurchasedTariffs();
console.log("responce" , response)
				if (errorMsg) {
					console.error("Произошла ошибка при вызове getRecentlyPurchasedTariffs:", errorMsg);
				}
				if (response) {
					const recentlyTariffs = response.slice(0, 10).map((obj: { id: string })=>obj.id)
					console.log("responce22222222222222222" , recentlyTariffs)
					console.log("tariffstariffstariffstariffstariffstariffs" , tariffs)
					setRecentlyPurchased(tariffs.filter((tariffs)=>{
						return recentlyTariffs.includes(tariffs._id)}));
				}
			} catch (err) {
				console.error("Произошла ошибка при вызове getRecentlyPurchasedTariffs:", err);
			}
		}

		fetchData();
	}, [tariffs]);
						console.log(recentlyPurchased)
	return {recentlyPurchased}
}