import chalk from "chalk";
import { spawn } from "child_process";
import pick from "lodash.pick";
import npmRunPath from "npm-run-path";
import { Checker } from "./Checker.js";
import {
  composePreambleCode,
  RUNTIME_CLIENT_ENTRY_PATH,
  RUNTIME_CLIENT_RUNTIME_PATH,
  runtimeCode,
  wrapVirtualPrefix,
  WS_CHECKER_RECONNECT_EVENT
} from "./client/index.js";
import {
  ACTION_TYPES
} from "./types.js";
const sharedConfigKeys = ["enableBuild", "overlay"];
const buildInCheckerKeys = [
  "typescript",
  "vueTsc",
  "vls",
  "eslint",
  "stylelint"
];
async function createCheckers(userConfig, env) {
  const serveAndBuildCheckers = [];
  const sharedConfig = pick(userConfig, sharedConfigKeys);
  for (const name of buildInCheckerKeys) {
    if (!userConfig[name])
      continue;
    const { createServeAndBuild } = await import(`./checkers/${name}/main.js`);
    serveAndBuildCheckers.push(
      createServeAndBuild({ [name]: userConfig[name], ...sharedConfig }, env)
    );
  }
  return serveAndBuildCheckers;
}
function checker(userConfig) {
  const enableBuild = (userConfig == null ? void 0 : userConfig.enableBuild) ?? true;
  const enableOverlay = (userConfig == null ? void 0 : userConfig.overlay) !== false;
  const enableTerminal = (userConfig == null ? void 0 : userConfig.terminal) !== false;
  const overlayConfig = typeof (userConfig == null ? void 0 : userConfig.overlay) === "object" ? userConfig == null ? void 0 : userConfig.overlay : {};
  let initialized = false;
  let initializeCounter = 0;
  let checkers = [];
  let isProduction = false;
  let baseWithOrigin;
  let viteMode;
  let buildWatch = false;
  let logger = null;
  return {
    name: "vite-plugin-checker",
    enforce: "pre",
    // @ts-ignore
    __internal__checker: Checker,
    config: async (config, env) => {
      viteMode = env.command;
      if (initializeCounter === 0) {
        initializeCounter++;
      } else {
        initialized = true;
        return;
      }
      checkers = await createCheckers(userConfig || {}, env);
      if (viteMode !== "serve")
        return;
      checkers.forEach((checker2) => {
        const workerConfig = checker2.serve.config;
        workerConfig({
          enableOverlay,
          enableTerminal,
          env
        });
      });
    },
    configResolved(config) {
      logger = config.logger;
      baseWithOrigin = config.server.origin ? config.server.origin + config.base : config.base;
      isProduction || (isProduction = config.isProduction || config.command === "build");
      buildWatch = !!config.build.watch;
    },
    buildEnd() {
      if (initialized)
        return;
      if (viteMode === "serve") {
        checkers.forEach((checker2) => {
          const { worker } = checker2.serve;
          worker.terminate();
        });
      }
    },
    resolveId(id) {
      if (id === RUNTIME_CLIENT_RUNTIME_PATH || id === RUNTIME_CLIENT_ENTRY_PATH) {
        return wrapVirtualPrefix(id);
      }
      return;
    },
    load(id) {
      if (id === wrapVirtualPrefix(RUNTIME_CLIENT_RUNTIME_PATH)) {
        return runtimeCode;
      }
      if (id === wrapVirtualPrefix(RUNTIME_CLIENT_ENTRY_PATH)) {
        return composePreambleCode({ baseWithOrigin, overlayConfig });
      }
      return;
    },
    transformIndexHtml() {
      if (initialized)
        return;
      if (isProduction)
        return;
      return [
        {
          tag: "script",
          attrs: { type: "module" },
          children: composePreambleCode({ baseWithOrigin, overlayConfig })
        }
      ];
    },
    buildStart: () => {
      if (initialized)
        return;
      if (!isProduction || !enableBuild)
        return;
      const localEnv = npmRunPath.env({
        env: process.env,
        cwd: process.cwd(),
        execPath: process.execPath
      });
      (async () => {
        const exitCodes = await Promise.all(
          checkers.map((checker2) => spawnChecker(checker2, userConfig, localEnv))
        );
        const exitCode = exitCodes.find((code) => code !== 0) ?? 0;
        if (exitCode !== 0 && !buildWatch) {
          process.exit(exitCode);
        }
      })();
    },
    configureServer(server) {
      if (initialized)
        return;
      let latestOverlayErrors = new Array(checkers.length);
      checkers.forEach((checker2, index) => {
        const { worker, configureServer: workerConfigureServer } = checker2.serve;
        workerConfigureServer({ root: server.config.root });
        worker.on("message", (action) => {
          if (action.type === ACTION_TYPES.overlayError) {
            latestOverlayErrors[index] = action.payload;
            if (action.payload) {
              server.ws.send("vite-plugin-checker", action.payload);
            }
          } else if (action.type === ACTION_TYPES.console) {
            if (Checker.logger.length) {
              Checker.log(action);
            } else {
              logger.error(action.payload);
            }
          }
        });
      });
      if (server.ws.on) {
        server.watcher.on("change", () => {
          logger.clearScreen("error");
        });
        server.ws.on("vite-plugin-checker", (data) => {
          if (data.event === "runtime-loaded") {
            server.ws.send("vite-plugin-checker", {
              event: WS_CHECKER_RECONNECT_EVENT,
              data: latestOverlayErrors.filter(Boolean)
            });
          }
        });
      } else {
        setTimeout(() => {
          logger.warn(
            chalk.yellow(
              "[vite-plugin-checker]: `server.ws.on` is introduced to Vite in 2.6.8, see [PR](https://github.com/vitejs/vite/pull/5273) and [changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md#268-2021-10-18). \nvite-plugin-checker relies on `server.ws.on` to send overlay message to client. Support for Vite < 2.6.8 will be removed in the next major version release."
            )
          );
        }, 5e3);
      }
    }
  };
}
function spawnChecker(checker2, userConfig, localEnv) {
  return new Promise((resolve) => {
    const buildBin = checker2.build.buildBin;
    const finalBin = typeof buildBin === "function" ? buildBin(userConfig) : buildBin;
    const proc = spawn(...finalBin, {
      cwd: process.cwd(),
      stdio: "inherit",
      env: localEnv,
      // shell is necessary on windows to get the process to even start.
      // Command line args constructed by checkers therefore need to escape double quotes
      // to have them not striped out by cmd.exe. Using shell on all platforms lets us avoid
      // having to perform platform-specific logic around escaping quotes since all platform
      // shells will strip out unescaped double quotes. E.g. shell=false on linux only would
      // result in escaped quotes not being unescaped.
      shell: true
    });
    proc.on("exit", (code) => {
      if (code !== null && code !== 0) {
        resolve(code);
      } else {
        resolve(0);
      }
    });
  });
}
var main_default = checker;
export {
  checker,
  main_default as default
};
//# sourceMappingURL=main.js.map