#!/usr/bin/env node
import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { clearImportedProjectTargets } from './src/server/routes/files.js';

function makeTempProject() {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-import-clear-'));
  for (const subdir of ['Apps', 'Theme', 'Config', '.vl-code']) {
    fs.mkdirSync(path.join(dir, subdir), { recursive: true });
  }
  fs.writeFileSync(path.join(dir, 'Apps', 'Old.vx'), '// old');
  fs.writeFileSync(path.join(dir, 'Theme', 'Theme.vth'), '// theme');
  fs.writeFileSync(path.join(dir, 'Config', 'ProjectConfig'), '1234');
  fs.writeFileSync(path.join(dir, 'VL.md'), '# old');
  fs.writeFileSync(path.join(dir, '.vl-code', 'project.json'), '{}');
  return dir;
}

async function main() {
  const dir = makeTempProject();
  await clearImportedProjectTargets(dir);

  assert.equal(fs.existsSync(path.join(dir, 'Apps')), false);
  assert.equal(fs.existsSync(path.join(dir, 'Theme')), false);
  assert.equal(fs.existsSync(path.join(dir, 'Config')), false);
  assert.equal(fs.existsSync(path.join(dir, 'VL.md')), false);
  assert.equal(fs.existsSync(path.join(dir, '.vl-code', 'project.json')), true);

  fs.rmSync(dir, { recursive: true, force: true });
  console.log('\n── Import Clears Project Targets ──');
  console.log('PASS test-import-clears-project-targets.js');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
