/**
 * Node resolve hook so the repo's extensionless TS imports (`./manifest`) work
 * under `node --experimental-strip-types`, which otherwise demands a real file
 * extension. Vite resolves these at build time; node does not.
 *
 *   node --import ./scripts/ts-resolve.mjs --experimental-strip-types src/assets/manifest.test.ts
 */
import { registerHooks } from 'node:module'
import { existsSync } from 'node:fs'
import { fileURLToPath } from 'node:url'

registerHooks({
  resolve(specifier, context, next) {
    try {
      return next(specifier, context)
    } catch (err) {
      if (!specifier.startsWith('.') || !context.parentURL) throw err
      for (const ext of ['.ts', '/index.ts']) {
        const url = new URL(specifier + ext, context.parentURL)
        if (existsSync(fileURLToPath(url))) return next(specifier + ext, context)
      }
      throw err
    }
  },
})
