import { isDeepStrictEqual } from 'node:util'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const report = JSON.parse(await readFile(resolve(root, 'config/freecad-property-integerset-success.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyIntegerSet success check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-integerset-success' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyIntegerSet') fail('baseline is invalid')
if (report.caseCount !== 2 || report.cases?.length !== 2 || report.bindingBoundary?.neverAssignedEmptyRead !== 'native-process-termination' || report.bindingBoundary.safeEquivalent !== 'assign-empty-list-before-read' || report.bindingBoundary.oracleDefault !== 'set()') fail('case inventory or native binding boundary changed')
const expected = new Map([
  ['Fem::FemSetNodesObject', { name: 'Nodes', group: 'Node indexes', deduplicated: [1, 2, 3] }],
  ['Fem::FemSetElementNodesObject', { name: 'Elements', group: 'Element indexes', deduplicated: [4, 10] }],
])
for (const entry of report.cases) {
  const contract = expected.get(entry.objectTypeId)
  if (!contract || entry.propertyName !== contract.name || entry.group !== contract.group || entry.mode !== 'direct-native-property-setter') fail(`${entry.objectTypeId} identity changed`)
  const values = { explicitEmpty: [], deduplicated: contract.deduplicated, negativeBoundary: [-2, 0, 5], scalar: [7], largeSafeInteger: [9007199254740991], restored: [] }
  for (const [phase, expectedValue] of Object.entries(values)) {
    const snapshot = entry.phases?.[phase]
    if (!isDeepStrictEqual(snapshot?.value, expectedValue) || snapshot.propertyTypeId !== 'App::PropertyIntegerSet' || snapshot.group !== contract.group || !isDeepStrictEqual(snapshot.propertyStatus, []) || !isDeepStrictEqual(snapshot.editorMode, []) || snapshot.shape?.applicable !== false || snapshot.objectSet?.length !== 1 || snapshot.objectSet[0].typeId !== entry.objectTypeId || !Array.isArray(snapshot.objectState)) fail(`${entry.objectTypeId} ${phase} semantics changed`)
  }
}
console.log(JSON.stringify({ status: 'freecad-property-integerset-success-pass', cases: report.caseCount, values: Object.fromEntries(report.cases.map((entry) => [entry.propertyName, Object.fromEntries(Object.entries(entry.phases).map(([phase, snapshot]) => [phase, snapshot.value]))])), bindingBoundary: report.bindingBoundary }, null, 2))

