"use server";
import { fetchData, fetchDataWithToken } from "@/apicalls/api-fetcher";
import { baseUrl, cookieObject, tokenSecret } from "@/apicalls/basics";
import { HeadersAndValidations } from "@/apicalls/validations/validationProcesser";

const headersAndValidationEndpoint = `${baseUrl}/validations/endpoint`;

interface EndpointInterface {
  endpoint: string;
}

async function retrieveHeadersEndpoint({ endpoint }: EndpointInterface) {
  const selectResponse: any = await fetchDataWithToken(
    headersAndValidationEndpoint,
    {
      endpoint: endpoint,
    },
    "POST",
    false
  );
  if (selectResponse.status === 200) {
    return {
      status: selectResponse.status,
      headers: selectResponse?.headers,
      message: selectResponse.message,
    };
  }
  return {
    status: selectResponse.status,
    message: selectResponse.message,
    headers: {},
  };
}

async function retrieveHeadersAndValidationByEndpoint({
  endpoint,
}: EndpointInterface) {
  const selectResponse: any = await fetchDataWithToken(
    headersAndValidationEndpoint,
    {
      endpoint: endpoint,
    },
    "POST",
    false
  );
  if (selectResponse.status === 200) {
    const responseParsed = new HeadersAndValidations(selectResponse);
    return {
      status: selectResponse.status,
      headers: responseParsed.headers,
      validated: responseParsed.validated,
      language: responseParsed.language,
      message: selectResponse.message,
    };
  }
  return {
    status: selectResponse.status,
    message: selectResponse.message,
    headers: null,
    validated: null,
  };
}

export { retrieveHeadersAndValidationByEndpoint, retrieveHeadersEndpoint };
