import { NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';

const input = process.argv[2];
const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
const doc = await io.read(input);
const root = doc.getRoot();

const skin = root.listSkins()[0];
const joints = skin.listJoints();
const isFinJoint = joints.map((n) => !n.getName().startsWith('Spine'));
console.log(`skin joints: ${joints.length}  (fin joints: ${isFinJoint.filter(Boolean).length}, body/Spine: ${isFinJoint.filter((x) => !x).length})`);

const mesh = root.listMeshes()[0];
const prims = mesh.listPrimitives();
console.log(`meshes: ${root.listMeshes().length}, primitives: ${prims.length}`);
const prim = prims[0];
const pos = prim.getAttribute('POSITION');
const jA = prim.getAttribute('JOINTS_0');
const wA = prim.getAttribute('WEIGHTS_0');
const idx = prim.getIndices();
console.log(`semantics: ${prim.listSemantics().join(', ')}`);
console.log(`vertices: ${pos.getCount()}, indices: ${idx.getCount()} (${idx.getCount() / 3} tris)`);

const vcount = pos.getCount();
const vIsFin = new Uint8Array(vcount);
const j = [0, 0, 0, 0], w = [0, 0, 0, 0];
for (let i = 0; i < vcount; i++) {
  jA.getElement(i, j);
  wA.getElement(i, w);
  let fin = 0, tot = 0;
  for (let k = 0; k < 4; k++) { tot += w[k]; if (isFinJoint[j[k]]) fin += w[k]; }
  vIsFin[i] = tot > 0 && fin / tot >= 0.5 ? 1 : 0;
}
let finV = 0;
for (let i = 0; i < vcount; i++) finV += vIsFin[i];

const arr = idx.getArray();
let bodyT = 0, finT = 0, mixed = 0;
for (let t = 0; t < arr.length; t += 3) {
  const votes = vIsFin[arr[t]] + vIsFin[arr[t + 1]] + vIsFin[arr[t + 2]];
  if (votes >= 2) finT++; else bodyT++;
  if (votes === 1 || votes === 2) mixed++;
}
console.log(`fin vertices: ${finV}/${vcount} (${(100 * finV / vcount).toFixed(1)}%)`);
console.log(`triangles → body: ${bodyT}, fin: ${finT}, (boundary/mixed tris: ${mixed})`);
