import chokidar from "chokidar";
import closeWithGrace from "close-with-grace";
let watcher = global.__change_tracker_watcher__;
function getWatcher() {
  if (process.env.KCDSHOP_DEPLOYED || process.env.KCDSHOP_DISABLE_WATCHER === "true") {
    return null;
  }
  if (watcher)
    return watcher;
  const workshopRoot = process.env.KCDSHOP_CONTEXT_CWD ?? process.cwd();
  watcher = chokidar.watch(workshopRoot, {
    ignoreInitial: true,
    ignored: [
      "**/node_modules/**",
      "**/build/**",
      "**/public/build/**",
      "**/playwright-report/**"
    ]
  });
  global.__change_tracker_watcher__ = watcher;
  return watcher;
}
function getOptionalWatcher() {
  return watcher;
}
global.__change_tracker_close_with_grace_return__?.uninstall();
global.__change_tracker_close_with_grace_return__ = closeWithGrace(
  () => watcher?.close()
);
export {
  getOptionalWatcher,
  getWatcher
};
