/**
 * Remove the bundled MCP docs after packing (#376, `postpack`). The bundle
 * (`resources/`) is generated by `copy-mcp-resources.js` at `prepack` and goes
 * INTO the published tarball, but it must NOT linger in the working tree: a
 * stale `resources/` would shadow the live repo-root `agent-docs/` in dev (and
 * in the in-repo apps that resolve `@webjsdev/cli` via the workspace symlink),
 * so dev would read a possibly-stale copy. Cleaning it on `postpack` keeps the
 * bundle transient, so `resolveDocsLocation` always uses the live source in dev.
 *
 * The reusable `cleanBundle(...)` is exported + unit-tested; the script body
 * runs it against the real package path. Dependency-free.
 *
 * @module clean-mcp-resources
 */

import { rmSync } from 'node:fs';
import { dirname, resolve, join } from 'node:path';
import { fileURLToPath } from 'node:url';

/**
 * Remove the bundle directory. PURE side effect on the given path.
 *
 * @param {string} destRoot  the `resources/` dir to remove
 * @returns {void}
 */
export function cleanBundle(destRoot) {
  rmSync(destRoot, { recursive: true, force: true });
}

function main() {
  const here = dirname(fileURLToPath(import.meta.url));
  const pkgRoot = resolve(here, '..'); // packages/mcp/scripts -> packages/mcp
  cleanBundle(join(pkgRoot, 'resources'));
  console.error('[webjs] cleaned the transient MCP docs bundle (resources/)');
}

if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
  main();
}
