/**
 * Copies vendored @sandcode/* packages from src/vendor/ to node_modules/@sandcode/.
 * Runs automatically via npm postinstall so vendor packages survive npm install.
 *
 * CommonJS so it works reliably during npm install -g (no ESM loader issues).
 */
const { cpSync, mkdirSync, readdirSync } = require('fs');
const { join, resolve } = require('path');

const root = resolve(__dirname, '..');
const vendorDir = join(root, 'src', 'vendor');
const targetDir = join(root, 'node_modules', '@sandcode');

mkdirSync(targetDir, { recursive: true });

const entries = readdirSync(vendorDir).filter(e => e.startsWith('sandcode-'));
for (const entry of entries) {
  const pkgName = entry.replace('sandcode-', '');
  const src = join(vendorDir, entry);
  const dest = join(targetDir, pkgName);
  cpSync(src, dest, { recursive: true, force: true });
}

// Also copy color-diff-napi (native module stub)
const colorDiffSrc = join(vendorDir, 'sandcode-color-diff-napi');
const colorDiffDest = join(root, 'node_modules', 'color-diff-napi');
try {
  cpSync(colorDiffSrc, colorDiffDest, { recursive: true, force: true });
  console.log(`Copied ${entries.length + 1} vendor packages (including color-diff-napi)`);
} catch {
  console.log(`Copied ${entries.length} vendor packages to node_modules/@sandcode/`);
}
