import { cpanelFetch } from "@/lib/utils";
import { api3BaseCommand } from "../..";
import { FilemanUploadFilesProps } from "../../types";

export default async function UploadFiles({
  dir,
  body,
  method,
}: FilemanUploadFilesProps): Promise<any> {
  const command = api3BaseCommand({
    command: "Fileman",
    subcommand: "upload_files",
  });

  const params: { [key: string]: any } = { dir };

  const paramsString = Object.keys(params)
    .filter((e) => e !== undefined)
    .map((e) => `${e}=${params[e]}`)
    .join("&");

  const fetchString = `${command}${paramsString}`;

  const response = await cpanelFetch(fetchString, method, body);

  if (!response.ok) {
    throw new Error();
  }

  return await response.json();
}
