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

const partsListEndpoint = `${baseUrl}/building/parts/list`;
const partsCreateEndpoint = `${baseUrl}/building/parts/create`;
const partsUpdateEndpoint = `${baseUrl}/building/parts/update`;

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

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

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

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

export { retrievePartsList, updateParts, createParts };
