import { SSLToggleSSLRedirect } from "@/lib/cpanel/api2/types";
import { cpanelFetch } from "@/lib/utils";
import { api3BaseCommand } from "../..";

export default async function ToggleSSLRedirect(props: SSLToggleSSLRedirect) {
  const command = api3BaseCommand({
    command: "SSL",
    subcommand: "toggle_ssl_redirect_for_domains",
  });

  const params: { [key: string]: any } = props;

  const paramsString = Object.keys(params)
    .filter((e) => e !== undefined)
    .map((e) => `${e}=${params[e]}`)
    .join("&");

  const fetchString = `${command}${paramsString}`;

  const response = await cpanelFetch(fetchString);

  if (!response.ok) {
    throw new Error();
  }

  return await response.json();
}
