import axios from "axios";

const tbnInstance = axios.create({
  baseURL: "https://portal.humas.polri.go.id/v1/api",
  headers: {
    "content-type": "application/json",
  },
});
const inpInstance = axios.create({
  baseURL: "https://inp.indoplusmedia.id/wp-json/wp/v2",
  headers: {
    "content-type": "application/json",
  },
});

async function tbnGetNews(pathUrl: any, headers: any) {
  const response = await tbnInstance
    .get(pathUrl, { headers })
    .catch(function (error: any) {
      console.log(error);
      return error.response;
    });
  console.log("Response base svc : ", response);
  if (response?.status == 200 || response?.status == 201) {
    return {
      error: false,
      message: "success",
      data: response?.data,
    };
  } else {
    return {
      error: true,
      message: response?.data?.message || response?.data || null,
      data: null,
    };
  }
}
async function inpGetNews(pathUrl: any, headers: any) {
  const response = await inpInstance
    .get(pathUrl, { headers })
    .catch(function (error: any) {
      console.log(error);
      return error.response;
    });
  console.log("Response base svc : ", response);
  if (response?.status == 200 || response?.status == 201) {
    return {
      error: false,
      message: "success",
      data: response?.data,
    };
  } else {
    return {
      error: true,
      message: response?.data?.message || response?.data || null,
      data: null,
    };
  }
}

export async function topNewsTbn() {
  const headers = {
    "content-type": "application/json",
  };
  return await tbnGetNews(
    `/public/articles?page=1&limit=10&order_by=terkini&source=tbnews`,
    headers
  );
}
export async function topNewsInp() {
  const headers = {
    "content-type": "application/json",
  };
  return await inpGetNews(`/posts`, headers);
}

export async function getImageInp(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await inpGetNews(`/media?parent=${id}`, headers);
}
