import createProbe from './dist/freecad-private-naming-source-probe.js'
import { inspectWasmStaticArchive } from '../../scripts/freecad-naming-sdk-lib.mjs'

const archive = await inspectWasmStaticArchive(new URL('./dist/libFreeCADPrivateNamingProbe.a', import.meta.url))
if (archive.memberCount !== 7 || archive.wasmObjectCount !== 7 || archive.llvmBitcodeCount !== 0) {
  throw new Error(`Unexpected FreeCAD private naming source archive: ${JSON.stringify(archive)}`)
}

const probe = await createProbe()
const result = probe.freecadPrivateNamingSourceProbe()
if (result !== 'Edge12;:M;CUT;:H2a:7,E') throw new Error(`Unexpected FreeCAD private naming source probe result: ${result}`)
const stringHasherResult = probe.freecadPrivateStringHasherSourceProbe()
if (stringHasherResult !== 'plain=1;hashed=2;mapped=5;index=12;related=2') {
  throw new Error(`Unexpected FreeCAD StringHasher source probe result: ${stringHasherResult}`)
}
const elementMapResult = probe.freecadPrivateElementMapSourceProbe()
if (elementMapResult !== 'stored=#1;:H2a,F;tag=42;original=Edge12;:M;CUT;serialized=115;restored=1') {
  throw new Error(`Unexpected FreeCAD ElementMap source probe result: ${elementMapResult}`)
}
for (const forbidden of ['freecadNamingAbiVersion', 'freecadNamingCapabilitiesJson', 'freecadNamingEvidenceJson']) {
  if (typeof probe[forbidden] === 'function') throw new Error(`Source prerequisite probe must not export production ABI callback ${forbidden}.`)
}
console.log(JSON.stringify({
  status: 'source-prerequisite-pass',
  freecadVersion: '1.1.1',
  sourceCommit: '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d',
  linkedSources: [
    'App/IndexedName.cpp',
    'App/MappedName.cpp',
    'App/StringHasher.cpp',
    'App/MappedElement.cpp',
    'App/ElementNamingUtils.cpp',
    'App/ElementMap.cpp',
    'Base/Handle.cpp',
  ],
  sourceArchive: {
    name: 'libFreeCADPrivateNamingProbe.a',
    ...archive,
    productionEligible: false,
  },
  result,
  stringHasherResult,
  elementMapResult,
  productionWorkerLinked: false,
}, null, 2))
