import fs from 'node:fs'
import path from 'node:path'

const root=process.cwd()
const src=path.join(root,'packages/kbx-ui/src/registry/componentManifest.ts')
const out=path.join(root,'generated/component-manifest.json')
const text=fs.readFileSync(src,'utf8')
const entries=[...text.matchAll(/\{\s*name:\s*'([^']+)'\s*,\s*category:\s*'([^']+)'\s*,\s*purpose:\s*'([^']+)'[\s\S]*?version:\s*'([^']+)'\s*\}/g)]
  .map(m=>({name:m[1],category:m[2],purpose:m[3],version:m[4]}))
const duplicate=entries.filter((x,i,a)=>a.findIndex(y=>y.name===x.name)!==i)
if(duplicate.length){console.error(`duplicate component manifest entries: ${duplicate.map(x=>x.name).join(', ')}`);process.exit(1)}
fs.writeFileSync(out,JSON.stringify(entries,null,2)+'\n')
console.log(`generated ${entries.length} component definitions -> ${path.relative(root,out)}`)
