/**
 * SANDCODE Loader Shim
 *
 * Loaded via --import before the CLI entry point when running under tsx/Node.
 * Registers:
 *   1. bun:bundle module shim (feature() → false)
 *   2. MACRO global (VERSION, BUILD_TIME)
 *   3. Module resolution hook for bun:bundle imports
 */
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { register } from 'node:module'

// Read version from package.json
const __shimDir = dirname(fileURLToPath(import.meta.url))
const pkg = JSON.parse(readFileSync(join(__shimDir, '..', 'package.json'), 'utf8'))

// Define MACRO as a global (used throughout the codebase for build-time constants)
globalThis.MACRO = {
  VERSION: process.env.SANDCODE_VERSION || pkg.version,
  BUILD_TIME: new Date().toISOString(),
}

// Register the custom loader that resolves bun:bundle to our shim
register('./bun-bundle-hook.js', import.meta.url)
