import { getCsrfToken } from "../master-user";
import axiosInterceptorInstance from "./axios-interceptor-instance";
import axiosBaseInstance from "./http-base-instance";
import mediahubBaseInstance from "./mediahub-base-service";
import Cookies from "js-cookie";

const defaultHeaders = {
  "Content-Type": "application/json",
};

export async function httpPost(pathUrl: any, headers: any, data?: any) {
  const resCsrf = await getCsrfToken();
  const csrfToken = resCsrf?.data?.csrf_token;

  const mergedHeaders = {
    ...defaultHeaders,
    ...headers,
    ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
  };

  const response = await axiosBaseInstance
    .post(pathUrl, data, { headers: mergedHeaders })
    .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 httpGet(pathUrl: any, headers: any) {
  const response = await axiosInterceptorInstance
    .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 httpPut(pathUrl: any, headers: any, data?: any) {
  const resCsrf = await getCsrfToken();
  const csrfToken = resCsrf?.data?.csrf_token;

  const defaultHeaders = {
    "Content-Type": "application/json",
  };
  const mergedHeaders = {
    ...defaultHeaders,
    ...headers,
    ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
  };

  const response = await axiosBaseInstance
    .put(pathUrl, data, { headers: mergedHeaders })
    .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 httpDeleteInterceptor(pathUrl: any, headers: any) {
  const resCsrf = await getCsrfToken();
  const csrfToken = resCsrf?.data?.csrf_token;

  const defaultHeaders = {
    "Content-Type": "application/json",
  };
  const mergedHeaders = {
    ...defaultHeaders,
    ...headers,
    ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
  };

  const response = await axiosBaseInstance
    .delete(pathUrl, { headers: mergedHeaders })
    .catch((error) => error.response);
  console.log("Response interceptor : ", 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 mediahubGet(pathUrl: any, headers: any) {
  const response = await mediahubBaseInstance
    .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,
    };
  }
}
