import { env } from "../config/env.js";
import { ApiError } from "./ApiError.js";

export const launchPuppeteerBrowser = async () => {
  if (env.disablePdfExports) {
    throw new ApiError(503, "PDF export is disabled on this hosting environment.");
  }

  const puppeteerModule = await import("puppeteer");
  const puppeteer = puppeteerModule.default || puppeteerModule;

  return puppeteer.launch({
    headless: true,
    executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || undefined,
    args: ["--no-sandbox", "--disable-setuid-sandbox"],
  });
};
