import { cyan } from "kleur/colors";
import { performance } from "perf_hooks";
import { attachContentServerListeners } from "../../content/index.js";
import { info, warn } from "../logger/core.js";
import * as msg from "../messages.js";
import { printHelp } from "../messages.js";
import { startContainer } from "./container.js";
import { createContainerWithAutomaticRestart } from "./restart.js";
async function dev(settings, options) {
  var _a, _b, _c, _d, _e;
  if (((_a = options.flags) == null ? void 0 : _a.help) || ((_b = options.flags) == null ? void 0 : _b.h)) {
    printHelp({
      commandName: "astro dev",
      usage: "[...flags]",
      tables: {
        Flags: [
          ["--port", `Specify which port to run on. Defaults to 3000.`],
          ["--host", `Listen on all addresses, including LAN and public addresses.`],
          ["--host <custom-address>", `Expose on a network IP address at <custom-address>`],
          ["--help (-h)", "See all available flags."]
        ]
      },
      description: `Check ${cyan(
        "https://docs.astro.build/en/reference/cli-reference/#astro-dev"
      )} for more information.`
    });
    return;
  }
  const devStart = performance.now();
  await options.telemetry.record([]);
  const restart = await createContainerWithAutomaticRestart({
    flags: options.flags ?? {},
    handleConfigError: options.handleConfigError,
    beforeRestart: () => console.clear(),
    params: {
      settings,
      root: (_c = options.flags) == null ? void 0 : _c.root,
      logging: options.logging,
      isRestart: options.isRestart
    }
  });
  const devServerAddressInfo = await startContainer(restart.container);
  info(
    options.logging,
    null,
    msg.serverStart({
      startupTime: performance.now() - devStart,
      resolvedUrls: restart.container.viteServer.resolvedUrls || { local: [], network: [] },
      host: settings.config.server.host,
      base: settings.config.base,
      isRestart: options.isRestart
    })
  );
  const currentVersion = "0.0.0-vercel-json-20230313194145";
  if (currentVersion.includes("-")) {
    warn(options.logging, null, msg.prerelease({ currentVersion }));
  }
  if (((_e = (_d = restart.container.viteConfig.server) == null ? void 0 : _d.fs) == null ? void 0 : _e.strict) === false) {
    warn(options.logging, null, msg.fsStrictWarning());
  }
  await attachContentServerListeners(restart.container);
  return {
    address: devServerAddressInfo,
    get watcher() {
      return restart.container.viteServer.watcher;
    },
    handle(req, res) {
      return restart.container.handle(req, res);
    },
    async stop() {
      await restart.container.close();
    }
  };
}
export {
  dev as default
};
