resopt-cli 0.9.0

Find, review and safely apply resource optimizations for Apple and Android projects.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Application state, data loading, filters and the resource list.
const boot = JSON.parse(document.getElementById('report-data').textContent);
const $ = id => document.getElementById(id);
const reducedMotion = () => window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function readStoredEarly(key) { try { return localStorage.getItem(key); } catch { return null; } }
const state = {
  token: boot.sessionToken || null,
  meta: boot.meta || null,
  records: [],            // sparse, indexed like the final report
  missing: new Set(), presenceStarted: false, presenceError: '',
  loaded: 0,              // rows received from the live feed
  phase: boot.sessionToken ? 'analyzing' : 'ready',
  progress: { completed: 0, total: 0 },
  error: null, disconnected: false,
  operations: {}, busy: false, message: '', capabilities: null,
  compareMode: readStoredEarly('resopt-compare'), differenceGain: 16,
  mode: 'candidates', page: 0, selected: null, selectedRecords: new Set(), selectionAnchor: null,
  chosen: null, background: 'checker', filtered: [],
};
const PAGE_SIZE = 50;

function readStored(key) { try { return localStorage.getItem(key); } catch { return null; } }
function store(key, value) { try { localStorage.setItem(key, value); } catch { /* private mode */ } }
let locale = pickLocale(readStored('resopt-language'), navigator.languages || [navigator.language]);
const t = (key, ...args) => translate(locale, key, args);
const size = value => formatSize(value, locale);
const count = value => formatNumber(value, locale);

function el(tag, cls, text) {
  const node = document.createElement(tag);
  if (cls) node.className = cls;
  if (text !== undefined) node.textContent = text;
  return node;
}
function sizeNode(value, cls) {
  const node = el('span', cls || '', size(value));
  if (typeof value === 'number') node.title = t('exactBytes', count(value));
  return node;
}
function candidateLabel(c) {
  const setting = !c.lossy ? t('lossless') : c.format === 'heic' && c.quality === 100 ? t('nearLossless') : t('quality', c.quality ?? '—');
  return `${String(c.format || '').toUpperCase()} · ${setting}`;
}
const pathText = r => String(r.resource?.path || '');
const indexOf = r => state.records.indexOf(r);
const operation = r => state.operations[indexOf(r)];
const isApplied = r => { const s = operation(r); return !!s && s.state !== 'original'; };
const isOptimized = r => operation(r)?.state === 'applied';
function operationBadge(r) {
  const status = operation(r)?.state;
  return status === 'partial' ? t('operationPartial') : status === 'conflict' ? t('operationConflict') : '';
}
function selectedAppliedResources() {
  return [...state.selectedRecords].filter(isApplied).map(indexOf);
}
function appliedSavings(r) {
  const s = operation(r);
  return s?.state === 'applied' ? (r.candidates?.[s.candidate]?.savings_bytes || 0) : 0;
}
function lowestScore(r) {
  const scores = (r.candidates || []).map(c => c.difference?.ssimulacra2).filter(v => typeof v === 'number');
  return scores.length ? Math.min(...scores) : Infinity;
}

function selectionAfterClick(current, anchor, clicked, ordered, shift, alt) {
  const next = alt ? new Set(current) : new Set();
  const start = ordered.indexOf(anchor), end = ordered.indexOf(clicked);
  if (shift && start >= 0 && end >= 0) {
    for (let i = Math.min(start, end); i <= Math.max(start, end); i++) next.add(ordered[i]);
    return { records: next, anchor };
  }
  if (alt && next.has(clicked)) next.delete(clicked); else next.add(clicked);
  return { records: next, anchor: clicked };
}

async function api(path, body) {
  const headers = { 'X-Resopt-Token': state.token };
  if (body !== undefined) headers['Content-Type'] = 'application/json';
  const response = await fetch(path, { method: body === undefined ? 'GET' : 'POST', headers, body: body === undefined ? undefined : JSON.stringify(body), cache: 'no-store' });
  const result = await response.json();
  if (result.states) state.operations = result.states;
  if (!response.ok) throw new Error(result.error || `HTTP ${response.status}`);
  return result;
}

// ---- live feed -------------------------------------------------------------
async function poll() {
  try {
    let page;
    do {
      page = await api(`/api/results?after=${state.loaded}`);
      if (page.phase === 'ready' && state.phase !== 'ready') { state.records = []; state.loaded = 0; page = await api('/api/results?after=0'); state.phase = 'ready'; }
      for (const { index, row } of page.rows) state.records[index] = row;
      state.loaded = page.next;
      state.progress = { completed: page.completed, total: page.total };
      state.error = page.error || null;
      if (page.phase === 'failed') state.phase = 'failed';
    } while (page.rows.length && state.loaded < page.completed);
    state.disconnected = false;
    if (state.phase === 'ready') {
      const [meta, operations] = await Promise.all([api('/api/report'), api('/api/state')]);
      state.meta = meta; state.operations = operations;
      if (!state.presenceStarted) { state.presenceStarted = true; setTimeout(pollPresence, 0); }
    }
  } catch (error) {
    state.disconnected = true;
  }
  renderStatus(); refresh(false);
  if (state.phase === 'analyzing' || state.disconnected) setTimeout(poll, state.disconnected ? 2000 : 600);
}

// Once analysis settles, poll metadata only; unchanged ticks never re-render the
// inspector (which would reset scroll position, comparisons and animation playback).
async function pollPresence() {
  if (state.busy) { setTimeout(pollPresence, 2000); return; }
  let refreshNeeded = false;
  try {
    const result = await api('/api/presence');
    if (result.busy) { setTimeout(pollPresence, 2000); return; }
    if (!Array.isArray(result.missing) || result.missing.some(index => !Number.isInteger(index) || index < 0 || index >= state.records.length)) throw new Error('Invalid file status');
    const missing = new Set(result.missing);
    const changed = missing.size !== state.missing.size || [...missing].some(index => !state.missing.has(index));
    const hadError = !!state.presenceError; state.presenceError = '';
    if (changed) {
      state.missing = missing;
      // Refresh operation badges only on a presence change, not every tick.
      try { state.operations = await api('/api/state'); } catch { /* retry on the next change */ }
      refreshNeeded = true;
    }
    if (hadError || changed) renderStatus();
  } catch (error) {
    const message = String(error.message || error);
    if (state.presenceError !== message) { state.presenceError = message; renderStatus(); }
  }
  if (refreshNeeded) refresh(false);
  setTimeout(pollPresence, 2000);
}

function similarGroups() {
  const groups = state.meta?.similarGroups || [];
  if (state.visibleGroupSource !== groups || state.visibleMissing !== state.missing) {
    state.visibleGroupSource = groups; state.visibleMissing = state.missing;
    state.visibleGroups = remainingSimilarGroups(groups, state.records, state.missing);
  }
  return state.visibleGroups;
}

// ---- status, summary ---------------------------------------------------------
function renderStatus() {
  const banner = $('status'); banner.replaceChildren(); banner.className = 'status';
  const line = (text, cls) => banner.append(el('p', cls, text));
  if (state.disconnected) { banner.classList.add('error'); line(t('disconnected')); return; }
  if (state.phase === 'failed') { banner.classList.add('error'); line(t('failed', state.error)); line(t('failedAction'), 'hint'); return; }
  if (state.phase === 'analyzing') {
    const { completed, total } = state.progress;
    line(total ? t('analyzing', count(completed), count(total)) : t('scanning'));
    const bar = el('progress'); bar.max = total || 1; bar.value = completed; bar.setAttribute('aria-label', t('analyzing', completed, total)); banner.append(bar);
    const stop = el('button', '', state.cancelling ? t('cancelling') : t('cancel')); stop.type = 'button'; stop.disabled = !!state.cancelling;
    stop.addEventListener('click', async () => { state.cancelling = true; renderStatus(); try { await api('/api/cancel', {}); } catch { /* surfaced by polling */ } });
    banner.append(stop); line(t('localOnly'), 'hint'); return;
  }
  if (state.presenceError) { banner.classList.add('warn'); line(t('fileStatusUnavailable'), 'hint'); }
  if (state.meta?.cancelled) { banner.classList.add('warn'); line(t('cancelled')); }
  const p = state.meta?.performance;
  if (p) line(t('perf', p.wall_seconds.toFixed(1), p.workers, count(p.cache_hits), count(p.duplicate_reuses)), 'hint');
  for (const tool of state.capabilities?.tools || []) if (!tool.available) line(t('tool_missing', tool.name, tool.purpose, tool.install), 'hint');
  for (const feature of state.capabilities?.features || []) if (!feature.available) line(`${feature.id.toUpperCase()}: ${feature.note}`, 'hint');
}

function renderSummary() {
  const rows = state.records.filter((r, index) => r && !state.missing.has(index));
  const opportunities = rows.filter(r => recommendedSavings(r) > 0);
  const setStat = (id, text, title) => { $(id).textContent = text; if (title) $(id).title = title; };
  const savings = opportunities.reduce((n, r) => n + recommendedSavings(r), 0);
  const applied = rows.reduce((n, r) => n + appliedSavings(r), 0);
  setStat('stat-resources', count(state.phase === 'analyzing' && state.progress.total ? state.progress.total : rows.length));
  setStat('stat-opportunities', count(opportunities.length));
  setStat('stat-savings', size(savings), t('exactBytes', count(savings)));
  setStat('stat-warnings', count(rows.filter(hasWarningCandidate).length));
  setStat('stat-applied', size(applied), t('exactBytes', count(applied)));
  const options = state.meta?.options;
  $('scope-note').textContent = options ? t('scope', size(rows.reduce((n, r) => n + (r.resource?.bytes || 0), 0)), (options.qualities || []).join(' / '), options.min_score ?? '—') : '';
  const modes = { candidates: opportunities.length, warnings: rows.filter(hasWarningCandidate).length, duplicates: similarGroups().length, applied: rows.filter(isApplied).length,
    images: rows.filter(r => r.resource?.kind === 'image').length, translations: rows.filter(r => r.localization).length, unsupported: rows.filter(r => ['unsupported', 'inventory_only'].includes(r.status)).length,
    failed: rows.filter(r => r.status === 'failed').length, all: rows.length };
  for (const [mode, value] of Object.entries(modes)) $(`mode-${mode}`).textContent = count(value);
  const formats = [...new Set(rows.map(r => r.resource?.format).filter(Boolean))].sort();
  const select = $('format-filter'), current = select.value;
  if (select.options.length !== formats.length + 1) {
    select.replaceChildren(Object.assign(el('option', '', t('allFormats')), { value: 'all' }), ...formats.map(f => Object.assign(el('option', '', f.toUpperCase()), { value: f })));
    select.value = formats.includes(current) ? current : 'all';
  }
  const live = !!state.token && state.phase === 'ready' && state.mode !== 'duplicates';
  const selectedApplied = selectedAppliedResources().length;
  $('batch-open').hidden = !live; $('restore-all-open').hidden = !live;
  $('restore-selected-open').hidden = !live || state.selectedRecords.size <= 1 || !selectedApplied;
  $('batch-open').textContent = state.selectedRecords.size > 1 ? t('batchSelected', count(state.selectedRecords.size)) : t('batch');
  $('restore-selected-open').textContent = t('restoreSelected', count(selectedApplied));
  $('batch-open').disabled = !opportunities.length && !modes.warnings; $('restore-all-open').disabled = !modes.applied;
}

// ---- list ---------------------------------------------------------------------
// Report index -> position of its duplicate group (groups are sorted by redundant bytes).
function duplicateGroups() {
  const groups = similarGroups();
  if (state.groupSource !== groups) { state.groupSource = groups; state.groupOf = new Map(); groups.forEach((g, rank) => g.members.forEach(i => state.groupOf.set(i, rank))); }
  return state.groupOf;
}
function similarityGroup(r) { return similarGroups()[duplicateGroups().get(indexOf(r))]; }
function groupScoreRange(group) {
  const comparisons = group.comparisons?.slice(1);
  if (!comparisons?.length || comparisons.length !== group.members.length - 1 || comparisons.some(c => !Number.isFinite(c.score))) return null;
  const scores = comparisons.map(c => c.identical ? 100 : Math.min(99.9, Math.max(0, c.score)));
  return [Math.min(...scores), Math.max(...scores)];
}
function groupBytes(group) { return group.members.reduce((total, index) => total + (state.records[index]?.resource?.bytes || 0), 0); }
function groupScoreText(group) {
  const range = groupScoreRange(group);
  return range ? `${range[0].toFixed(1)}${range[0].toFixed(1) === range[1].toFixed(1) ? '' : `–${range[1].toFixed(1)}`}` : '—';
}
const MODE_FILTERS = {
  candidates: r => recommendedSavings(r) > 0, warnings: hasWarningCandidate, duplicates: r => duplicateGroups().has(indexOf(r)), applied: isApplied,
  images: r => r.resource?.kind === 'image', translations: r => !!r.localization, unsupported: r => ['unsupported', 'inventory_only'].includes(r.status),
  failed: r => r.status === 'failed', all: () => true,
};
function applyFilters() {
  const query = $('search').value.trim().toLocaleLowerCase().split(/\s+/).filter(Boolean);
  const format = $('format-filter').value, sort = $('sort').value;
  state.filtered = state.records.filter((r, index) => r && !state.missing.has(index) && MODE_FILTERS[state.mode](r) && (format === 'all' || r.resource?.format === format)
    && query.every(q => `${pathText(r)} ${r.resource?.format} ${r.resource?.kind}`.toLocaleLowerCase().includes(q)));
  if (state.mode === 'duplicates') {
    state.filtered = similarGroupRows(state.records, similarGroups(), new Set(state.filtered.map(indexOf)));
    const byGroup = {
      savings: (a, b) => similarityGroup(b).redundant_bytes - similarityGroup(a).redundant_bytes,
      size: (a, b) => groupBytes(similarityGroup(b)) - groupBytes(similarityGroup(a)),
      name: (a, b) => basename(pathText(a)).localeCompare(basename(pathText(b))),
      score: (a, b) => (groupScoreRange(similarityGroup(a))?.[0] ?? -1) - (groupScoreRange(similarityGroup(b))?.[0] ?? -1),
    }[sort];
    state.filtered.sort((a, b) => byGroup(a, b) || duplicateGroups().get(indexOf(a)) - duplicateGroups().get(indexOf(b)));
    return;
  }
  // In the translations view the savings order ranks tables by issues instead.
  const by = { savings: state.mode === 'translations' ? (a, b) => localizationIssueTotal(b) - localizationIssueTotal(a) : (a, b) => recommendedSavings(b) - recommendedSavings(a), size: (a, b) => (b.resource?.bytes || 0) - (a.resource?.bytes || 0),
    name: (a, b) => basename(pathText(a)).localeCompare(basename(pathText(b))), score: (a, b) => lowestScore(a) - lowestScore(b) }[sort];
  state.filtered.sort((a, b) => by(a, b) || pathText(a).localeCompare(pathText(b)));
}
// `reset` moves to the first page and selection; live updates keep the user's place.
function refresh(reset) {
  renderSummary(); applyFilters();
  state.selectedRecords = new Set([...state.selectedRecords].filter(r => state.filtered.includes(r)));
  if (reset) state.page = 0;
  state.page = Math.min(state.page, Math.max(0, Math.ceil(state.filtered.length / PAGE_SIZE) - 1));
  const selectionChanged = !state.selected || !state.filtered.includes(state.selected);
  if (selectionChanged) { state.selected = state.filtered[state.page * PAGE_SIZE] || null; state.chosen = preferredCandidate(state.selected); }
  if (state.selected && !state.selectedRecords.size) state.selectedRecords.add(state.selected);
  else if (state.selected && !state.selectedRecords.has(state.selected)) {
    state.selected = state.filtered.find(r => state.selectedRecords.has(r)) || state.selected;
    state.chosen = preferredCandidate(state.selected);
  }
  if (state.mode === 'duplicates') state.selectedRecords = new Set(state.selected ? [state.selected] : []);
  if (reset && state.selected) state.chosen = preferredCandidate(state.selected);
  if (!state.filtered.includes(state.selectionAnchor)) state.selectionAnchor = state.selected;
  document.querySelectorAll('.mode').forEach(b => { const on = b.dataset.mode === state.mode; b.classList.toggle('active', on); b.setAttribute('aria-pressed', String(on)); });
  renderList();
  if (selectionChanged || reset || state.phase === 'ready') renderDetail();
}
function preferredCandidate(r) {
  if (!r) return null;
  const s = operation(r);
  if (s?.state === 'applied') return s.candidate;
  // In the warnings view the point is to review the warning candidate itself.
  if (state.mode === 'warnings') {
    const warnings = (r.candidates || []).map((c, i) => [c, i]).filter(([c]) => warningKind(c)).sort((a, b) => a[0].bytes - b[0].bytes);
    if (warnings.length) return warnings[0][1];
  }
  if (Number.isInteger(r.smallest_candidate)) return r.smallest_candidate;
  const warning = (r.candidates || []).findIndex(c => warningKind(c));
  return warning >= 0 ? warning : (r.candidates?.length ? 0 : null);
}
function renderList() {
  document.querySelector('.list-head [data-i18n="colResource"]').textContent = t(state.mode === 'duplicates' ? 'dupGroups' : 'colResource');
  document.querySelector('.list-head [data-i18n="colSize"]').textContent = t(state.mode === 'duplicates' ? 'dupTotalSize' : 'colSize');
  document.querySelector('.list-head [data-i18n="colSavings"]').textContent = t(state.mode === 'duplicates' ? 'dupSimilarity' : state.mode === 'translations' ? 'locColIssues' : 'colSavings');
  $('sort').querySelector('[value="savings"]').textContent = t(state.mode === 'duplicates' ? 'dupSortGroup' : state.mode === 'translations' ? 'locSortIssues' : 'sortSavings');
  const list = $('results'); const focused = document.activeElement?.dataset?.index; list.replaceChildren();
  const start = state.page * PAGE_SIZE, items = state.filtered.slice(start, start + PAGE_SIZE);
  if (!items.length) {
    const empty = el('div', 'empty');
    if (state.phase === 'analyzing' && !state.records.some(Boolean)) empty.append(el('strong', '', t('waiting')));
    else {
      empty.append(el('strong', '', t('noMatch')), el('span', '', t('noMatchHint')));
      const reset = el('button', '', t('clearFilters')); reset.type = 'button';
      reset.addEventListener('click', () => { $('search').value = ''; $('format-filter').value = 'all'; state.mode = 'all'; refresh(true); });
      empty.append(reset);
    }
    list.append(empty);
  }
  for (const r of items) {
    const row = el('button', 'resource-row'); row.type = 'button'; row.dataset.index = String(indexOf(r));
    row.dataset.active = String(r === state.selected); row.dataset.optimized = String(isOptimized(r));
    row.setAttribute('role', 'option'); row.setAttribute('aria-selected', String(state.selectedRecords.has(r))); row.title = pathText(r);
    const identity = el('span', 'identity'), thumb = el('span', 'thumb'), src = assetUrl(r.original_preview);
    if (src) { const img = el('img'); img.src = src; img.alt = ''; img.loading = 'lazy'; img.decoding = 'async'; thumb.append(img); }
    else thumb.textContent = String(r.resource?.format || 'file').toUpperCase().slice(0, 5);
    const copy = el('span', 'file-copy');
    copy.append(el('span', 'filename', basename(pathText(r))), el('span', 'file-context', `${String(r.resource?.format || '').toUpperCase()} · ${pathText(r).replace(/[\\/]?[^\\/]+$/, '')}`));
    identity.append(thumb, copy);
    const saved = recommendedSavings(r), cell = el('span', saved ? 'number saving' : 'number status-muted');
    if (isOptimized(r)) cell.append(operationStatus('success', '✓', t('modeApplied')));
    else if (isApplied(r)) cell.append(operationStatus('warning', '!', operationBadge(r)));
    else if (saved) cell.append(sizeNode(saved), el('small', '', `−${formatPercent(saved / (r.resource.bytes || 1))}`));
    else if (hasWarningCandidate(r)) cell.append(el('span', 'badge warn', t('modeWarnings')));
    else if (localizationIssueTotal(r)) cell.append(el('span', 'badge warn', t('locIssueCount', count(localizationIssueTotal(r)))));
    else cell.append(el('span', '', '—'));
    if (state.mode === 'duplicates') {
      const group = similarityGroup(r), rank = duplicateGroups().get(indexOf(r));
      row.classList.add('similar-group-row'); row.dataset.optimized = 'false';
      row.title = group.members.map(index => pathText(state.records[index])).join('\n');
      const summary = el('span', 'group-summary');
      summary.append(el('strong', '', `${t('dupGroup', rank + 1)} · ${t('dupImageCount', count(group.members.length))}`), el('span', 'hint', t(`dup_${group.kind}`)));
      const previews = el('span', 'group-thumbs');
      for (const index of group.members.slice(0, 4)) {
        const member = state.records[index], src = assetUrl(member?.original_preview), tile = el('span', 'thumb');
        if (src) { const img = el('img'); img.src = src; img.alt = ''; img.loading = 'lazy'; img.decoding = 'async'; tile.append(img); }
        else tile.textContent = String(member?.resource?.format || '—').toUpperCase();
        previews.append(tile);
      }
      if (group.members.length > 4) previews.append(el('span', 'hint', `+${count(group.members.length - 4)}`));
      summary.append(previews, el('span', 'file-context', t('dupReferenceName', basename(pathText(r)))));
      const score = el('span', 'number group-score', groupScoreText(group)); score.append(el('small', '', '/ 100')); score.title = t('dupRangeHint');
      row.append(summary, sizeNode(groupBytes(group), 'number'), score);
    } else row.append(identity, sizeNode(r.resource?.bytes, 'number'), cell);
    row.addEventListener('click', event => { selectRecord(r, event); if (window.matchMedia('(max-width: 760px)').matches) $('inspector').scrollIntoView({ block: 'start', behavior: reducedMotion() ? 'instant' : 'smooth' }); });
    row.addEventListener('keydown', event => {
      const move = { ArrowDown: 1, ArrowUp: -1 }[event.key]; if (!move) return;
      event.preventDefault(); const next = items[items.indexOf(r) + move];
      if (next) { selectRecord(next); list.querySelector(`[data-index="${indexOf(next)}"]`)?.focus(); }
    });
    list.append(row);
  }
  if (focused !== undefined) list.querySelector(`[data-index="${focused}"]`)?.focus();
  const total = state.filtered.length;
  renderListRange();
  $('page-number').textContent = `${total ? state.page + 1 : 0} / ${Math.ceil(total / PAGE_SIZE)}`;
  $('previous').disabled = state.page === 0; $('next').disabled = start + PAGE_SIZE >= total;
}
function renderListRange() {
  const total = state.filtered.length, start = state.page * PAGE_SIZE;
  if (state.mode === 'duplicates') { $('range').textContent = total ? t('dupGroupRange', count(start + 1), count(Math.min(start + PAGE_SIZE, total)), count(total)) : t('none'); return; }
  $('range').textContent = `${total ? t('range', count(start + 1), count(Math.min(start + PAGE_SIZE, total)), count(total)) : t('none')} · ${t('selectedCount', count(state.selectedRecords.size))}`;
}
function operationStatus(kind, symbol, label) {
  const status = el('span', `operation-status ${kind}`), icon = el('span', 'operation-icon', symbol);
  icon.setAttribute('aria-hidden', 'true'); status.append(icon, el('span', '', label)); return status;
}
function selectRecord(r, event = {}) {
  if (state.mode === 'duplicates') { event = {}; state.groupMember = null; }
  const result = selectionAfterClick(state.selectedRecords, state.selectionAnchor, r, state.filtered, !!event.shiftKey, !!event.altKey);
  state.selectedRecords = result.records; state.selectionAnchor = result.anchor;
  state.selected = state.selectedRecords.has(r) ? r : state.filtered.find(item => state.selectedRecords.has(item)) || r;
  state.chosen = preferredCandidate(state.selected); state.message = '';
  for (const row of $('results').children) {
    const item = state.records[Number(row.dataset?.index)];
    row.setAttribute?.('aria-selected', String(state.selectedRecords.has(item)));
    row.dataset.active = String(item === state.selected);
  }
  renderListRange();
  renderSummary();
  renderDetail();
}
function changePage(delta) { state.page += delta; const items = state.filtered.slice(state.page * PAGE_SIZE, (state.page + 1) * PAGE_SIZE); state.selected = items.find(r => state.selectedRecords.has(r)) || items[0] || null; if (state.selected && !state.selectedRecords.size) state.selectedRecords.add(state.selected); state.chosen = preferredCandidate(state.selected); renderList(); renderDetail(); $('results').scrollTop = 0; }

// ---- static chrome ------------------------------------------------------------
function applyStaticText() {
  document.documentElement.lang = locale; document.title = `resopt · ${t('title')}`;
  document.querySelectorAll('[data-i18n]').forEach(node => { node.textContent = t(node.dataset.i18n); });
  document.querySelectorAll('[data-i18n-label]').forEach(node => { node.setAttribute('aria-label', t(node.dataset.i18nLabel)); });
  document.querySelectorAll('[data-i18n-placeholder]').forEach(node => { node.placeholder = t(node.dataset.i18nPlaceholder); });
  $('session-mode').textContent = state.token ? t('live') : t('offline');
  $('format-filter').replaceChildren();
}
function applyTheme() {
  const value = $('theme').value;
  document.documentElement.dataset.theme = value === 'system' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : value;
}
function start() {
  $('theme').value = ['light', 'dark', 'system'].includes(readStored('resopt-theme')) ? readStored('resopt-theme') : 'system';
  $('theme').addEventListener('change', () => { applyTheme(); store('resopt-theme', $('theme').value); });
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', applyTheme); applyTheme();
  const browserLanguages = navigator.languages?.length ? navigator.languages : [navigator.language];
  $('language').value = ['en', 'zh-CN'].includes(readStored('resopt-language')) ? readStored('resopt-language') : 'auto';
  $('language').addEventListener('change', () => { const choice = $('language').value; store('resopt-language', choice); locale = pickLocale(choice, browserLanguages); applyStaticText(); renderStatus(); refresh(false); renderDetail(); });
  applyStaticText();
  for (const id of ['search', 'format-filter', 'sort']) $(id).addEventListener(id === 'search' ? 'input' : 'change', () => refresh(true));
  document.querySelectorAll('.mode').forEach(b => b.addEventListener('click', () => { state.mode = b.dataset.mode; refresh(true); }));
  $('previous').addEventListener('click', () => changePage(-1)); $('next').addEventListener('click', () => changePage(1));
  document.addEventListener('keydown', event => { if (event.key === '/' && !/INPUT|SELECT|TEXTAREA/.test(document.activeElement?.tagName || '')) { event.preventDefault(); $('search').focus(); } });
  setupDialogs();
  // The launch key has done its job once the page is loaded; keep it out of the address bar and history.
  if (location.search) history.replaceState(null, '', location.pathname);
  if (state.token) {
    api('/api/capabilities').then(c => { state.capabilities = c; renderStatus(); }).catch(() => {});
    poll();
  } else {
    state.records = Array.isArray(boot.resources) ? boot.resources : [];
    if (!state.records.some(r => recommendedSavings(r) > 0)) state.mode = 'all';
    renderStatus(); refresh(true);
  }
}