import https from "https";

export const getPT = async (params) => {
	try {
		const controller = new AbortController();
		const timeoutId = setTimeout(() => controller.abort(), 10000);

		const httpsAgent = new https.Agent({
			rejectUnauthorized: false,
		});
		let url = "https://api.kemdikbud.go.id:8243/pddikti/1.2/pt";
		if (params.id) {
			url += `/${params.id}`;
		} else if (params.search) {
			url += `?q=${params.search}`;
		}

		const res = await fetch(url, {
			method: "GET",
			headers: new Headers({
				Authorization: "Bearer 5b62f743-eef2-3370-8c66-6951b2e9c2c5",
				Accept: "application/json",
				"Access-Control-Allow-Origin": "*",
			}),
			agent: httpsAgent,
			signal: controller.signal,
		});
		if (!res.ok) {
			throw new Error(`Error! status: ${response.status}`);
		}

		clearTimeout(timeoutId);
		const result = await res.json();
		return result;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};
