/* /who-gets-paid (Marty, 2026-09-26): enter any position, read it and its matrix uplines live
from /api/public/payment-path, and walk the contract's _payUpline rule one step at a time from
the buyer upward. "What if" lets a viewer give a passed-over upline the level or direct they
are missing and see where the payment would land instead. The rule here is the contract's:
skip levelIndex uplines; pay the first with level > levelIndex and directCount >= 2;
stop at the root (#1) or after 16 steps and send it to the fee wallet. */
(function () {
var NAMES = ['Scintilla', 'Ascensus', 'Fabrica', 'Culmen', 'Apex', 'Fastigium', 'Vertex', 'Corona'];
var $ = function (id) { return document.getElementById(id); };
var form = $('wgpForm'), idIn = $('wgpId'), sel = $('wgpLvl'), buyWrap = $('wgpBuyWrap');
var runBtn = $('wgpRun'), resetBtn = $('wgpReset'), buyerEl = $('wgpBuyer'), sumEl = $('wgpSum');
var chainEl = $('wgpChain'), legend = $('wgpLegend');
var data = null, overrides = {}, timer = null;
function esc(s) { return String(s).replace(/[&<>"]/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"' }[c]; }); }
function fmt(n) { return Number(n || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }); }
function lvName(l) { return NAMES[l - 1] || ('level ' + l); }
function eff(u) { var o = overrides[u.id] || {}; return { level: o.level || u.level, directCount: o.directCount != null ? o.directCount : u.directCount, what: !!(o.level || o.directCount != null) }; }
function price(li) {
var t = data.buyer.tier === 2 ? 2 : 1;
return data.costs && data.costs.up && data.costs.up[t] ? data.costs.up[t][li] : null;
}
function evaluate(li) {
var steps = [], paid = null, U = data.uplines;
for (var i = 0; i < U.length; i++) {
var e = eff(U[i]);
if (i < li) { steps.push({ s: 'skip', why: 'Skipped: ' + (i + 1) + ' of ' + li + ' skipped without checking.' }); continue; }
var lvOk = e.level > li, dOk = e.directCount >= 2;
if (lvOk && dOk) { steps.push({ s: 'paid', why: 'Holds ' + lvName(e.level) + ', needs ' + lvName(li + 1) + ' or higher, and has ' + e.directCount + ' directs. Paid in full; the search ends.' }); paid = i; break; }
var need = [];
if (!lvOk) need.push('needs ' + lvName(li + 1) + ' or higher (holds ' + lvName(e.level) + ')');
if (!dOk) need.push('needs 2 directs (has ' + e.directCount + ')');
steps.push({ s: 'fail', why: 'Passed over: ' + need.join(' and ') + '.', fix: { id: U[i].id, level: lvOk ? null : li + 1, directs: dOk ? null : 2 } });
}
return { steps: steps, paid: paid };
}
function draw(li, upto) {
var r = evaluate(li), U = data.uplines, b = data.buyer;
var html = '
';
}
var done = upto >= r.steps.length, amt = price(li);
var amtTxt = amt != null ? fmt(amt) + ' POL' : 'the upgrade';
if (done && r.paid === null) {
var reason = data.stopped === 'root' && U.length < 16 ? 'The next upline is #1, the root. The search stops there.' : 'The search used all 16 steps.';
html += '
stopfee' + (data.stopped === 'root' && U.length < 16 ? '#1 root reached' : '16 steps used') + 'fee wallet' + reason + ' Nobody qualified, so the full ' + amtTxt + ' goes to the fee wallet.
';
}
chainEl.innerHTML = html;
if (done) {
var wi = Object.keys(overrides).length ? ' (with your what-if changes)' : '';
sumEl.innerHTML = r.paid !== null
? '#' + U[r.paid].id + ' would be paid ' + amtTxt + '' + wi + '. ' + li + ' upline' + (li === 1 ? '' : 's') + ' skipped, ' + (r.paid - li) + ' passed over.'
: 'Nobody in reach qualifies, so the full ' + amtTxt + ' would go to the fee wallet' + wi + '. Skipped ' + Math.min(li, U.length) + ', checked ' + Math.max(0, U.length - li) + '.';
} else sumEl.textContent = 'Checking ' + (upto + 1) + ' up…';
}
function run(animate) {
if (!data) return;
if (timer) { clearInterval(timer); timer = null; }
var li = Number(sel.value), total = evaluate(li).steps.length;
var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!animate || reduce) { draw(li, total); return; }
var k = 0; draw(li, 0);
timer = setInterval(function () { k++; draw(li, k); if (k >= total) { clearInterval(timer); timer = null; } }, 380);
}
function showBuyer() {
var b = data.buyer, t = b.tier === 2 ? 'Premium' : 'Standard';
buyerEl.innerHTML =
'