import axios from "axios";

interface StrapiResponse<T> {
	data: {
		id: number;
		attributes: {
			createdAt: Date;
			updatedAt: Date;
			publishedAt: Date;
			locale: "en" | "ar";
		} & T;
	};
	meta: object;
}

type Result<T> = {data: StrapiResponse<T>; isError: boolean};

export async function getData<T>(page: string, locale: "ar" | "en"): Promise<Result<T>> {
	const request = await axios.get<StrapiResponse<T>>(`${process.env.STRAPI_URL}/api/${page}/?populate=deep&locale=${locale}`, {
		headers: {Authorization: `Bearer ${process.env.STRAPI_TOKEN}`},
	});
	return {data: request.data, isError: request.status !== 200};
}
