"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var watchMode_exports = {};
__export(watchMode_exports, {
  runWatchModeLoop: () => runWatchModeLoop
});
module.exports = __toCommonJS(watchMode_exports);
var import_path = __toESM(require("path"));
var import_readline = __toESM(require("readline"));
var import_stream = require("stream");
var import_playwrightServer = require("playwright-core/lib/remote/playwrightServer");
var import_utils = require("playwright-core/lib/utils");
var import_utils2 = require("playwright-core/lib/utils");
var import_base = require("../reporters/base");
var import_utilsBundle = require("../utilsBundle");
var import_testServer = require("./testServer");
var import_configLoader = require("../common/configLoader");
var import_teleSuiteUpdater = require("../isomorphic/teleSuiteUpdater");
var import_testServerConnection = require("../isomorphic/testServerConnection");
class InMemoryTransport extends import_stream.EventEmitter {
  constructor(send) {
    super();
    this._send = send;
  }
  close() {
    this.emit("close");
  }
  onclose(listener) {
    this.on("close", listener);
  }
  onerror(listener) {
  }
  onmessage(listener) {
    this.on("message", listener);
  }
  onopen(listener) {
    this.on("open", listener);
  }
  send(data) {
    this._send(data);
  }
}
async function runWatchModeLoop(configLocation, initialOptions) {
  if ((0, import_configLoader.restartWithExperimentalTsEsm)(void 0, true))
    return "restarted";
  const options = { ...initialOptions };
  let bufferMode = false;
  const testServerDispatcher = new import_testServer.TestServerDispatcher(configLocation, {});
  const transport = new InMemoryTransport(
    async (data) => {
      const { id, method, params } = JSON.parse(data);
      try {
        const result2 = await testServerDispatcher.transport.dispatch(method, params);
        transport.emit("message", JSON.stringify({ id, result: result2 }));
      } catch (e) {
        transport.emit("message", JSON.stringify({ id, error: String(e) }));
      }
    }
  );
  testServerDispatcher.transport.sendEvent = (method, params) => {
    transport.emit("message", JSON.stringify({ method, params }));
  };
  const testServerConnection = new import_testServerConnection.TestServerConnection(transport);
  transport.emit("open");
  const teleSuiteUpdater = new import_teleSuiteUpdater.TeleSuiteUpdater({ pathSeparator: import_path.default.sep, onUpdate() {
  } });
  const dirtyTestFiles = /* @__PURE__ */ new Set();
  const dirtyTestIds = /* @__PURE__ */ new Set();
  let onDirtyTests = new import_utils.ManualPromise();
  let queue = Promise.resolve();
  const changedFiles = /* @__PURE__ */ new Set();
  testServerConnection.onTestFilesChanged(({ testFiles }) => {
    testFiles.forEach((file) => changedFiles.add(file));
    queue = queue.then(async () => {
      if (changedFiles.size === 0)
        return;
      const { report: report2 } = await testServerConnection.listTests({ locations: options.files, projects: options.projects, grep: options.grep });
      teleSuiteUpdater.processListReport(report2);
      for (const test of teleSuiteUpdater.rootSuite.allTests()) {
        if (changedFiles.has(test.location.file)) {
          dirtyTestFiles.add(test.location.file);
          dirtyTestIds.add(test.id);
        }
      }
      changedFiles.clear();
      if (dirtyTestIds.size > 0) {
        onDirtyTests.resolve("changed");
        onDirtyTests = new import_utils.ManualPromise();
      }
    });
  });
  testServerConnection.onReport((report2) => teleSuiteUpdater.processTestReportEvent(report2));
  await testServerConnection.initialize({ interceptStdio: false, watchTestDirs: true, populateDependenciesOnList: true });
  await testServerConnection.runGlobalSetup({});
  const { report } = await testServerConnection.listTests({});
  teleSuiteUpdater.processListReport(report);
  const projectNames = teleSuiteUpdater.rootSuite.suites.map((s) => s.title);
  let lastRun = { type: "regular" };
  let result = "passed";
  while (true) {
    if (bufferMode)
      printBufferPrompt(dirtyTestFiles, teleSuiteUpdater.config.rootDir);
    else
      printPrompt();
    const waitForCommand = readCommand();
    const command = await Promise.race([
      onDirtyTests,
      waitForCommand.result
    ]);
    if (command === "changed")
      waitForCommand.cancel();
    if (bufferMode && command === "changed")
      continue;
    const shouldRunChangedFiles = bufferMode ? command === "run" : command === "changed";
    if (shouldRunChangedFiles) {
      if (dirtyTestIds.size === 0)
        continue;
      const testIds = [...dirtyTestIds];
      dirtyTestIds.clear();
      dirtyTestFiles.clear();
      await runTests(options, testServerConnection, { testIds, title: "files changed" });
      lastRun = { type: "changed", dirtyTestIds: testIds };
      continue;
    }
    if (command === "run") {
      await runTests(options, testServerConnection);
      lastRun = { type: "regular" };
      continue;
    }
    if (command === "project") {
      const { selectedProjects } = await import_utilsBundle.enquirer.prompt({
        type: "multiselect",
        name: "selectedProjects",
        message: "Select projects",
        choices: projectNames
      }).catch(() => ({ selectedProjects: null }));
      if (!selectedProjects)
        continue;
      options.projects = selectedProjects.length ? selectedProjects : void 0;
      await runTests(options, testServerConnection);
      lastRun = { type: "regular" };
      continue;
    }
    if (command === "file") {
      const { filePattern } = await import_utilsBundle.enquirer.prompt({
        type: "text",
        name: "filePattern",
        message: "Input filename pattern (regex)"
      }).catch(() => ({ filePattern: null }));
      if (filePattern === null)
        continue;
      if (filePattern.trim())
        options.files = filePattern.split(" ");
      else
        options.files = void 0;
      await runTests(options, testServerConnection);
      lastRun = { type: "regular" };
      continue;
    }
    if (command === "grep") {
      const { testPattern } = await import_utilsBundle.enquirer.prompt({
        type: "text",
        name: "testPattern",
        message: "Input test name pattern (regex)"
      }).catch(() => ({ testPattern: null }));
      if (testPattern === null)
        continue;
      if (testPattern.trim())
        options.grep = testPattern;
      else
        options.grep = void 0;
      await runTests(options, testServerConnection);
      lastRun = { type: "regular" };
      continue;
    }
    if (command === "failed") {
      const failedTestIds = teleSuiteUpdater.rootSuite.allTests().filter((t) => !t.ok()).map((t) => t.id);
      await runTests({}, testServerConnection, { title: "running failed tests", testIds: failedTestIds });
      lastRun = { type: "failed", failedTestIds };
      continue;
    }
    if (command === "repeat") {
      if (lastRun.type === "regular") {
        await runTests(options, testServerConnection, { title: "re-running tests" });
        continue;
      } else if (lastRun.type === "changed") {
        await runTests(options, testServerConnection, { title: "re-running tests", testIds: lastRun.dirtyTestIds });
      } else if (lastRun.type === "failed") {
        await runTests({}, testServerConnection, { title: "re-running tests", testIds: lastRun.failedTestIds });
      }
      continue;
    }
    if (command === "toggle-show-browser") {
      await toggleShowBrowser();
      continue;
    }
    if (command === "toggle-buffer-mode") {
      bufferMode = !bufferMode;
      continue;
    }
    if (command === "exit")
      break;
    if (command === "interrupted") {
      result = "interrupted";
      break;
    }
  }
  const teardown = await testServerConnection.runGlobalTeardown({});
  return result === "passed" ? teardown.status : result;
}
function readKeyPress(handler) {
  const promise = new import_utils.ManualPromise();
  const rl = import_readline.default.createInterface({ input: process.stdin, escapeCodeTimeout: 50 });
  import_readline.default.emitKeypressEvents(process.stdin, rl);
  if (process.stdin.isTTY)
    process.stdin.setRawMode(true);
  const listener = import_utils.eventsHelper.addEventListener(process.stdin, "keypress", (text, key) => {
    const result = handler(text, key);
    if (result)
      promise.resolve(result);
  });
  const cancel = () => {
    import_utils.eventsHelper.removeEventListeners([listener]);
    rl.close();
    if (process.stdin.isTTY)
      process.stdin.setRawMode(false);
  };
  void promise.finally(cancel);
  return { result: promise, cancel };
}
const isInterrupt = (text, key) => text === "