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";

function getCookie(name: string) {
  const value = `; ${document.cookie}`;
  console.log("val", value);
  const parts: any = value.split(`; ${name}=`);
  if (parts.length === 2) {
    return parts.pop().split(";").shift();
  }
  return undefined;
}

function removeCookies() {
  console.log("run");
  Cookies.remove("csrf_");
  Cookies.remove("session_id");

  document.cookie = "csrf_=; Max-Age=0;path=/";
  document.cookie = "session_id=; Max-Age=0;path=/";
}
export async function httpPost(pathUrl: any, headers: any, data?: any) {
  // const aCookie = getCookie("session_id");

  // if (aCookie === undefined) {
  //   console.log("kosong");
  // } else {
  //   console.log("cookie  ada", aCookie);
  // }
  // removeCookies();
  // const csrfNow = Cookies.get("csrf_");
  // const sessionNow = Cookies.get("session_id");
  // console.log("now", csrfNow, sessionNow);
  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
    .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,
    };
  }
}
