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

const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
const doc = await io.read(process.argv[2]);
const skin = doc.getRoot().listSkins()[0];
const joints = skin.listJoints();

const tailRoots = joints.filter((n) => /^Tail_Fork_(Top|Bottom)$/.test(n.getName()));
const tailSet = new Set();
const mark = (n) => { tailSet.add(n); n.listChildren().forEach(mark); };
tailRoots.forEach(mark);
const tailJoints = joints.filter((n) => tailSet.has(n));
console.log(`tail roots: ${tailRoots.map((n) => n.getName()).join(', ')}`);
console.log(`tail subtree joints (${tailJoints.length}): ${tailJoints.map((n) => n.getName()).join(', ')}`);

// classify vertices into body / tail / otherfin and count triangles per group
const isFin = joints.map((n) => !n.getName().startsWith('Spine'));
const isTail = joints.map((n) => tailSet.has(n));
const mesh = doc.getRoot().listMeshes()[0];
const prim = mesh.listPrimitives().reduce((a, b) => (b.getIndices().getCount() > a.getIndices().getCount() ? b : a));
const jA = prim.getAttribute('JOINTS_0'), wA = prim.getAttribute('WEIGHTS_0');
const n = prim.getAttribute('POSITION').getCount();
const vFin = new Uint8Array(n), vTail = new Uint8Array(n);
const j = [0, 0, 0, 0], w = [0, 0, 0, 0];
for (let i = 0; i < n; i++) {
  jA.getElement(i, j); wA.getElement(i, w);
  let f = 0, t = 0, tot = 0;
  for (let k = 0; k < 4; k++) { tot += w[k]; if (isFin[j[k]]) f += w[k]; if (isTail[j[k]]) t += w[k]; }
  vFin[i] = tot > 0 && f / tot >= 0.5 ? 1 : 0;
  vTail[i] = tot > 0 && t / tot >= 0.5 ? 1 : 0;
}
console.log(`tail vertices: ${vTail.reduce((a, b) => a + b, 0)} / ${n}`);
