const test = require('node:test')
const assert = require('node:assert')
const fs = require('fs')
const path = require('path')
const os = require('os')

const { analyzeLibraryDeps } = require('../src/lib/library-deps-hint')

test('analyzeLibraryDeps tolerates modern/TS-ish syntax in .jsx templates', () => {
  const site = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-hints-parse-'))
  fs.writeFileSync(path.join(site, 'package.json'), '{"name":"hint-site"}\n', 'utf8')

  const component = path.join(site, 'Weird.jsx')
  fs.writeFileSync(
    component,
    [
      "import React from 'react'",
      "import pkg, { thing } from 'some-pkg'",
      '',
      // TS-ish syntax inside .jsx: historically seen in templates migrated from TS.
      'const n = (1 as unknown as number)',
      'const obj = { a: 1, ...(true ? { b: 2 } : {}) }',
      'export default function X() {',
      '  return <div data-x={obj?.b ?? 0}>{thing ?? pkg}</div>',
      '}',
      '',
    ].join('\n'),
    'utf8',
  )

  assert.doesNotThrow(() => {
    const analysis = analyzeLibraryDeps(component, {
      // Do not rely on ~/.bild.json during tests
      config: { sitePath: null, cmsDependenciesFile: null, cmsDependenciesImportAlias: null },
    })
    assert.ok(analysis, 'expected analysis result for .jsx file')
  })
})

