// Measure the two tail lobes separately. For each of Tail_Fork_Top and
// Tail_Fork_Bottom (subtree), report the vertex centroid + bbox. If the lobes are
// separated in Y → a normal vertical fork (upper/lower). If separated in X →
// a genuine LEFT/RIGHT double tail (two tails side by side), which is what reads
// as "two tails" and is a geometry problem, not a render one.
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 root = doc.getRoot();
const joints = root.listSkins()[0].listJoints();
const idxOf = new Map(joints.map((n, i) => [n, i]));

const subtree = (rootName) => {
  const set = new Set();
  const start = joints.find((n) => n.getName() === rootName);
  if (!start) return set;
  const mark = (n) => { if (idxOf.has(n)) set.add(idxOf.get(n)); n.listChildren().forEach(mark); };
  mark(start);
  return set;
};
const topSet = subtree('Tail_Fork_Top');
const botSet = subtree('Tail_Fork_Bottom');
console.log(`Top subtree joints: ${topSet.size}, Bottom subtree joints: ${botSet.size}`);

const mesh = root.listMeshes()[0];
const prim = mesh.listPrimitives().reduce((a, b) => (b.getIndices().getCount() > a.getIndices().getCount() ? b : a));
const pos = prim.getAttribute('POSITION');
const jA = prim.getAttribute('JOINTS_0'), wA = prim.getAttribute('WEIGHTS_0');
const n = pos.getCount();

const acc = { top: mk(), bot: mk() };
function mk() { return { c: 0, sum: [0, 0, 0], min: [1e9, 1e9, 1e9], max: [-1e9, -1e9, -1e9] }; }
function add(e, p) { e.c++; for (let d = 0; d < 3; d++) { e.sum[d] += p[d]; e.min[d] = Math.min(e.min[d], p[d]); e.max[d] = Math.max(e.max[d], p[d]); } }

const p = [0, 0, 0], 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 top = 0, bot = 0;
  for (let k = 0; k < 4; k++) { if (topSet.has(j[k])) top += w[k]; if (botSet.has(j[k])) bot += w[k]; }
  if (top < 0.5 && bot < 0.5) continue;
  pos.getElement(i, p);
  add(top >= bot ? acc.top : acc.bot, p);
}
const f = (x) => x.toFixed(3).padStart(8);
for (const [k, e] of Object.entries(acc)) {
  if (!e.c) { console.log(`${k}: (no verts)`); continue; }
  const ctr = e.sum.map((s) => s / e.c);
  console.log(`${k.padEnd(4)} n=${String(e.c).padStart(4)}  centroid[${ctr.map(f).join(',')}]  X:[${f(e.min[0])},${f(e.max[0])}] Y:[${f(e.min[1])},${f(e.max[1])}] Z:[${f(e.min[2])},${f(e.max[2])}]`);
}
