import axios from "axios";
import {NewsSingle} from "../models/Wordpress/NewsSingleWP";
import CategoryWP from "../models/Wordpress/CateroryWP";
import {onRequestFulfilled, onResponseRejected} from "./axios";

const baseurl = "https://egov.demokritos.gr/wp-json/wp/v2";
// const baseurl = "http://localhost/egovsupport/wp-json/wp/v2";
const postsEndpoint = "/posts";
const categoriesEndpoint = "/dua_header";
export const instance = axios.create({
	timeout: 15000,
	baseURL: baseurl,
});

instance.interceptors.request.use(
	//@ts-ignore
	onRequestFulfilled
);

/**
 * Global Error handling
 * Errors are returned, so they can more specifically handled where they are called?
 */
instance.interceptors.response.use(
	function (response) {
		// Any status code that lie within the range of 2xx cause this function to trigger
		return response;
	},
	onResponseRejected
);


export function getPostsWP(currentPage: number,
						   currentOrder: string,
						   currentCategory: string,) {
	return instance.get<NewsSingle[]>(postsEndpoint, {
		params: {
			per_page: 5,
			page: currentPage,
			order: currentOrder,
			...(currentCategory ? { dua_header_slug: currentCategory } : {}),
		}
	});
}





export function getCategoriesWP() {
	return instance.get<CategoryWP[]>(categoriesEndpoint);
}