import {
  organizePackageFiles
} from "./chunk-4THN5TT4.mjs";

// src/watch.ts
import fs from "fs";
import path from "path";
var BASE_DIR = path.join(process.cwd(), "stacksync");
async function watch() {
  console.log(`\u{1F440} Watching ${BASE_DIR} for new package.json files...`);
  console.log("   (Drop files here and they will be automatically organized)");
  console.log("   Press Ctrl+C to stop.");
  if (!fs.existsSync(BASE_DIR)) {
    fs.mkdirSync(BASE_DIR, { recursive: true });
  }
  organizePackageFiles();
  let isProcessing = false;
  let timer = null;
  fs.watch(BASE_DIR, (eventType, filename) => {
    if (!filename) return;
    if (!filename.startsWith("package") || !filename.endsWith(".json")) return;
    if (timer) clearTimeout(timer);
    timer = setTimeout(async () => {
      if (isProcessing) return;
      isProcessing = true;
      try {
        if (fs.existsSync(path.join(BASE_DIR, filename))) {
          console.log(`
Detected ${filename}...`);
          await new Promise((resolve) => setTimeout(resolve, 200));
          organizePackageFiles();
        }
      } catch (err) {
        console.error("Error processing file:", err);
      } finally {
        isProcessing = false;
      }
    }, 500);
  });
}

export {
  watch
};
