"use server";

import GetFileContent from "@/lib/cpanel/api3/Fileman/getfilecontent";
import { ServerResponse } from "@/lib/actions";
import { getCpanelVars } from "@/models/cpanel";
import { ServerAction } from "@/lib/actions";

export default async function fetchFileContent(formData: FormData) {
  const { error, con, dict } = await ServerAction();

  if (error) {
    return ServerResponse({
      operation: false,
      data: error,
    });
  }

  const dir = formData.get("dir") as string;
  const file = formData.get("file") as string;

  try {
    const result = await GetFileContent({
      dir: dir.slice(0, -file.length),
      file: file,
    });

    return ServerResponse({ operation: true, data: result.data });
  } catch (error) {
    return ServerResponse({
      operation: false,
      message: dict.alerts.general.unexpected_error,
      data: error,
    });
  } finally {
    con.release();
  }
}
