"use server";
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
import {
  baseUrl,
  FilterList,
  FilterListInterface,
  defaultFilterList,
} from "../basics";

const buildListEndpoint = `${baseUrl}/building/build/list`;
const buildCreateEndpoint = `${baseUrl}/building/build/create`;
const buildUpdateEndpoint = `${baseUrl}/building/build/update`;

async function retrieveBuildList(payload: FilterListInterface) {
  const feedObject = new FilterList(payload).filter();
  const tokenResponse: any = await fetchDataWithToken(
    buildListEndpoint,
    feedObject,
    "POST",
    false
  );
  return tokenResponse;
}

interface BuildUpdateInterface {
  uuid: string;
  payload: any;
}

async function updateBuild(payload: any) {
  const { uu_id: extractedField, ...payloadBody } = payload;
  const tokenResponse: any = await updateDataWithToken(
    buildUpdateEndpoint,
    extractedField,
    payloadBody,
    "POST",
    false
  );
  return tokenResponse;
}

async function createBuild(payload: any) {
  const tokenResponse: any = await fetchDataWithToken(
    buildCreateEndpoint,
    payload,
    "POST",
    false
  );
  return tokenResponse;
}

export { retrieveBuildList, updateBuild, createBuild };
