import * as esbuild from 'esbuild';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const watch = process.argv.includes('--watch');
const libRoot = resolve(__dirname, '../../src/lib');

/** @type {import('esbuild').BuildOptions} */
const options = {
  entryPoints: [resolve(__dirname, 'src/extension.ts')],
  bundle: true,
  outfile: resolve(__dirname, 'out/extension.js'),
  external: ['vscode'],
  format: 'cjs',
  platform: 'node',
  target: 'node22',
  sourcemap: true,
  sourcesContent: false,
  logLevel: 'info',
  // Share the Chrome extension's portable core without relocating it.
  alias: {
    '@lib': libRoot,
  },
};

if (watch) {
  const ctx = await esbuild.context(options);
  await ctx.watch();
  console.log('[lexai-vscode] watching…');
} else {
  await esbuild.build(options);
  console.log('[lexai-vscode] compiled → out/extension.js');
}
