#!/usr/bin/env node
/**
 * Build tree-sitter-dart native binding in node_modules/ after materialize-vendor-grammars.cjs.
 * Vendored source lives in vendor/ only; see #836 and #1728.
 */
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// Opt-out: skip the native rebuild entirely. Dart parsing becomes
// unavailable but `npm install gitnexus` finishes much faster on machines
// without a C++ toolchain. Strict `=== '1'` only — '=true', '=yes', '=0'
// (read as a string), and any other value all fall through to the rebuild.
if (process.env.GITNEXUS_SKIP_OPTIONAL_GRAMMARS === '1') {
  console.warn(
    '[tree-sitter-dart] Skipping build (GITNEXUS_SKIP_OPTIONAL_GRAMMARS=1). Dart parsing will be unavailable until reinstalled without the env var.',
  );
  process.exit(0);
}

const dartDir = path.join(__dirname, '..', 'node_modules', 'tree-sitter-dart');
const bindingGyp = path.join(dartDir, 'binding.gyp');
const bindingNode = path.join(dartDir, 'build', 'Release', 'tree_sitter_dart_binding.node');

try {
  if (!fs.existsSync(bindingGyp) || fs.existsSync(bindingNode)) {
    process.exit(0);
  }

  try {
    require.resolve('node-addon-api');
    require.resolve('node-gyp-build');
  } catch (resolveErr) {
    console.warn(
      '[tree-sitter-dart] Skipping build: hoisted build deps not resolvable (%s).',
      resolveErr.message,
    );
    console.warn(
      '[tree-sitter-dart] Dart parsing will be unavailable. Install without --no-optional and with scripts enabled to build.',
    );
    process.exit(0);
  }

  console.log('[tree-sitter-dart] Building native binding...');
  execSync('npx node-gyp rebuild', {
    cwd: dartDir,
    stdio: 'pipe',
    timeout: 180000,
  });
  console.log('[tree-sitter-dart] Native binding built successfully');
} catch (err) {
  console.warn('[tree-sitter-dart] Could not build native binding:', err.message);
  console.warn(
    '[tree-sitter-dart] Dart parsing will be unavailable. Non-Dart functionality is unaffected.',
  );
  process.exit(0);
}
