import { existsSync, promises } from 'node:fs';
import { dirname, relative } from 'node:path';
import process from 'node:process';
import { setupDotenv } from 'c12';
import { defineCommand } from 'citty';
import { box, colors } from 'consola/utils';
import { resolve } from 'pathe';
import { x } from 'tinyexec';
import { l as loadKit } from '../shared/cli.DlcAx0De.mjs';
import { c as cwdArgs, l as logLevelArgs, e as envNameArgs, b as legacyRootDirArgs, d as dotEnvArgs, a as logger } from '../shared/cli.TH4BiEd5.mjs';
import 'jiti';
import 'std-env';
import 'consola';
import 'node:url';

const preview = defineCommand({
  meta: {
    name: "preview",
    description: "Launches Nitro server for local testing after `nuxi build`."
  },
  args: {
    ...cwdArgs,
    ...logLevelArgs,
    ...envNameArgs,
    ...legacyRootDirArgs,
    ...dotEnvArgs
  },
  async run(ctx) {
    process.env.NODE_ENV = process.env.NODE_ENV || "production";
    const cwd = resolve(ctx.args.cwd || ctx.args.rootDir);
    const { loadNuxtConfig } = await loadKit(cwd);
    const config = await loadNuxtConfig({
      cwd,
      envName: ctx.args.envName,
      // c12 will fall back to NODE_ENV
      overrides: (
        /* ctx.options?.overrides || */
        {}
      )
    });
    const resolvedOutputDir = resolve(
      config.srcDir || cwd,
      config.nitro.output?.dir || ".output",
      "nitro.json"
    );
    const defaultOutput = resolve(cwd, ".output", "nitro.json");
    const nitroJSONPaths = [resolvedOutputDir, defaultOutput];
    const nitroJSONPath = nitroJSONPaths.find((p) => existsSync(p));
    if (!nitroJSONPath) {
      logger.error(
        "Cannot find `nitro.json`. Did you run `nuxi build` first? Search path:\n",
        nitroJSONPaths
      );
      process.exit(1);
    }
    const outputPath = dirname(nitroJSONPath);
    const nitroJSON = JSON.parse(await promises.readFile(nitroJSONPath, "utf-8"));
    if (!nitroJSON.commands.preview) {
      logger.error("Preview is not supported for this build.");
      process.exit(1);
    }
    const info = [
      ["Node.js:", `v${process.versions.node}`],
      ["Nitro Preset:", nitroJSON.preset],
      ["Working directory:", relative(process.cwd(), outputPath)]
    ];
    const _infoKeyLen = Math.max(...info.map(([label]) => label.length));
    logger.log(
      box(
        [
          "You are running Nuxt production build in preview mode.",
          `For production deployments, please directly use ${colors.cyan(
            nitroJSON.commands.preview
          )} command.`,
          "",
          ...info.map(
            ([label, value]) => `${label.padEnd(_infoKeyLen, " ")} ${colors.cyan(value)}`
          )
        ].join("\n"),
        {
          title: colors.yellow("Preview Mode"),
          style: {
            borderColor: "yellow"
          }
        }
      )
    );
    const envExists = ctx.args.dotenv ? existsSync(resolve(cwd, ctx.args.dotenv)) : existsSync(cwd);
    if (envExists) {
      logger.info(
        `Loading \`${ctx.args.dotenv || ".env"}\`. This will not be loaded when running the server in production.`
      );
      await setupDotenv({ cwd, fileName: ctx.args.dotenv });
    }
    logger.info(`Starting preview command: \`${nitroJSON.commands.preview}\``);
    const [command, ...commandArgs] = nitroJSON.commands.preview.split(" ");
    logger.log("");
    await x(command, commandArgs, { nodeOptions: { stdio: "inherit", cwd: outputPath } });
  }
});

export { preview as default };
