// The Spotlight page: pick a packet and a quantity; closed with a clear note until YPBShop is deployed.
(async function () {
const $ = YPB.$, esc = YPB.esc;
const c = await YPB.getConfig().catch(() => ({}));
const d = await YPB.getJSON('/api/packets').catch(() => ({ items: [] }));
const want = Number(new URLSearchParams(location.search).get('p'));
$('pkPick').innerHTML = (d.items || []).map(x => '').join('') || '';
if (!c.shop || !c.shop.live) { $('notYet').hidden = false; return; }
$('buyBtn').disabled = false;
const q = async () => { const r = await YPBBuy.quote(Number($('qty').value) || 1); $('quote').textContent = r.error || ('About ' + YPBBuy.fmtPol(r.wei) + ' POL for $' + r.usd + ' (' + YPB.n(r.views) + ' views).'); };
$('qty').oninput = q; q();
$('buyForm').onsubmit = async e => {
e.preventDefault(); $('buyBtn').disabled = true;
try {
const packetId = Number($('pkPick').value); const before = (await YPB.getJSON('/api/my')).spotlights.length;
const r = await YPBBuy.run({ qty: Number($('qty').value) || 1, packetId, done: async () => (await YPB.getJSON('/api/my')).spotlights.length > before });
YPB.status(r.recorded ? 'Your Spotlight is live. Track its views on My packets.' : 'Paid and confirmed. It shows on My packets as soon as the watcher records it.' + (r.link ? ' ' + r.link : ''), 'ok');
} catch (err) { YPB.status(String(err && err.message || err).slice(0, 400), 'err'); }
$('buyBtn').disabled = false;
};
})();