import { existsSync, rmSync, statSync, readdirSync } from "node:fs";
import { join } from "node:path";
const COMPONENT_SUBPATHS = {
  cache: "wigolo.db",
  embeddings: "embeddings",
  models: "models",
  browser: "playwright-browsers",
  searxng: "searxng"
};
function measureBytes(target) {
  if (!existsSync(target)) return 0;
  const st = statSync(target);
  if (st.isFile()) return st.size;
  if (!st.isDirectory()) return 0;
  let total = 0;
  const entries = readdirSync(target, { withFileTypes: true });
  for (const entry of entries) {
    const child = join(target, entry.name);
    if (entry.isFile()) {
      total += statSync(child).size;
    } else if (entry.isDirectory()) {
      total += measureBytes(child);
    }
  }
  return total;
}
async function cleanupComponent(component, dataDir) {
  const subpath = COMPONENT_SUBPATHS[component];
  const target = join(dataDir, subpath);
  if (!existsSync(target)) {
    return { ok: true, freedBytes: 0 };
  }
  const freedBytes = measureBytes(target);
  try {
    rmSync(target, { recursive: true, force: true });
    return { ok: true, freedBytes };
  } catch (err) {
    return {
      ok: false,
      freedBytes: 0,
      error: err instanceof Error ? err.message : String(err)
    };
  }
}
export {
  cleanupComponent
};
//# sourceMappingURL=cleanup.js.map