/**
 * Bundles the Node-safe `./chat-driver` subpath into self-contained, Node-loadable dual output
 * (`dist/chat-driver.mjs` + `dist/chat-driver.cjs`).
 *
 * Why a bundler and not just tsc: `genx build -b ts` emits bundler-target ESM (extensionless
 * relative imports + a bare `tslib` import) that Node's own loader can't resolve. This step bundles
 * `src/chat-driver-node.ts` and its closure (including `@genesislcap/foundation-ai`, `tslib`, and
 * the logger) inline, so a plain Node/CommonJS host can `require`/`import` the driver stack with no
 * bundler and no jsdom. The FAST/DOM layer is never in this entry's closure, and `foundation-ai`'s
 * DI module (its only `@microsoft/fast-foundation` importer) tree-shakes out.
 *
 * Runs after `genx build -b ts` (which produces the matching `dist/dts/chat-driver-node.d.ts`).
 */
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';

const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const entry = resolve(packageRoot, 'src/chat-driver-node.ts');

/** @type {import('esbuild').BuildOptions} */
const shared = {
  entryPoints: [entry],
  bundle: true,
  platform: 'node',
  target: 'node22',
  // Self-contained: bundle third-party deps (foundation-ai, tslib, logger) inline so the artifact
  // is Node-loadable standalone. Node built-ins are external automatically under platform: 'node'.
  sourcemap: 'linked',
  logLevel: 'info',
};

await esbuild.build({
  ...shared,
  format: 'esm',
  outfile: resolve(packageRoot, 'dist/chat-driver.mjs'),
});
await esbuild.build({
  ...shared,
  format: 'cjs',
  outfile: resolve(packageRoot, 'dist/chat-driver.cjs'),
});
