'use strict';
/* FieldKit Scan & pack β photo β pieces β drag into a bag β inventory.
Engines: Gemini (free Google AI Studio key saved on this device) or the built-in finder
(automatic cut-outs + on-device COCO-SSD names, no key, photo stays on the phone). */
const SCAN_KEY_STORE = 'fieldkit_gemini_key_v1';
const SCAN_MODEL_STORE = 'fieldkit_gemini_model_v1';
const SCAN_CATEGORIES = ['Safety','Water & food','Light & power','Fire & fuel','Clothing & shelter','Medical & hygiene','Tools','Kitchen','Documents','Other'];
const SCAN_MAX_PIECES = 40;
const SCAN_CAT_EMOJI = {'Safety':'π§','Water & food':'π§','Light & power':'π¦','Fire & fuel':'π₯','Clothing & shelter':'π§₯','Medical & hygiene':'π©Ή','Tools':'π§','Kitchen':'π³','Documents':'π','Other':'π¦'};
const SCAN_WORD_EMOJI = [
[/head ?lamp|flash ?light|torch|lantern/i,'π¦'],[/batter/i,'π'],[/power ?bank|charger|cable/i,'π'],[/radio/i,'π»'],[/phone/i,'π±'],
[/bottle|water|filter|bladder|canteen/i,'π§'],[/snack|food|bar\b|meal|ration/i,'π«'],[/coffee|cup|mug/i,'β'],[/pot|pan|stove|cook/i,'π³'],[/bowl/i,'π₯£'],[/spoon|fork|spork|utensil/i,'π΄'],
[/knife/i,'πͺ'],[/scissor/i,'βοΈ'],[/multi.?tool|tool|plier|wrench|screwdriver|tape/i,'π§'],[/rope|cord|paracord/i,'πͺ’'],
[/tent|tarp|shelter/i,'βΊ'],[/sleeping|blanket|bivy/i,'π'],[/jacket|shell|coat|layer|fleece|hoodie/i,'π§₯'],[/sock/i,'π§¦'],[/glove/i,'π§€'],[/hat|cap\b|beanie/i,'π§’'],[/boot|shoe/i,'π₯Ύ'],[/sunglass/i,'πΆοΈ'],
[/first.?aid|bandage|blister|plaster/i,'π©Ή'],[/pill|medic|ibuprofen|aspirin/i,'π'],[/sanitiz|soap|wipe/i,'π§Ό'],[/sunscreen|lotion|lip/i,'π§΄'],[/tooth/i,'πͺ₯'],
[/compass/i,'π§'],[/\bmap\b/i,'πΊοΈ'],[/whistle|signal/i,'π’'],[/lighter|match|fire|tinder|ferro/i,'π₯'],[/propane|butane|fuel|canister/i,'π’οΈ'],
[/cash|money|wallet/i,'π΅'],[/\bid\b|passport|document|card|contact/i,'πͺͺ'],[/umbrella/i,'βοΈ'],[/backpack|pack\b/i,'π'],[/bag|pouch|sack/i,'π'],[/book|notebook/i,'π'],[/key/i,'π']
];
// COCO-SSD class β [name, category]. Background classes are ignored.
const SCAN_COCO = {bottle:['Water bottle','Water & food'],cup:['Cup','Kitchen'],bowl:['Bowl','Kitchen'],'wine glass':['Glass','Kitchen'],knife:['Knife','Tools'],fork:['Fork','Kitchen'],spoon:['Spoon','Kitchen'],scissors:['Scissors','Tools'],
'cell phone':['Phone','Safety'],backpack:['Backpack','Other'],handbag:['Bag','Other'],suitcase:['Duffel bag','Other'],umbrella:['Umbrella','Clothing & shelter'],book:['Book','Documents'],toothbrush:['Toothbrush','Medical & hygiene'],
banana:['Banana','Water & food'],apple:['Apple','Water & food'],orange:['Orange','Water & food'],sandwich:['Sandwich','Water & food'],remote:['Remote','Light & power'],laptop:['Laptop','Light & power'],mouse:['Mouse','Light & power'],keyboard:['Keyboard','Light & power'],
clock:['Clock','Tools'],tie:['Tie','Clothing & shelter'],'sports ball':['Ball','Other'],frisbee:['Frisbee','Other'],'teddy bear':['Teddy bear','Other'],'hair drier':['Hair dryer','Medical & hygiene'],vase:['Vase','Other'],kite:['Kite','Other'],'baseball bat':['Bat','Other'],'tennis racket':['Racket','Other'],skateboard:['Skateboard','Other']};
let scanState = null; // {kit, img, canvas, w, h, pieces:[], engine, note}
let scanCoco = null, scanCocoLoading = null;
const scanNorm = s => String(s || '').trim().toLowerCase().replace(/\s+/g, ' ');
const scanClampCount = n => Math.max(1, Math.min(999, Math.floor(Number(n)) || 1));
function scanEmoji(name, category) {
for (const [re, e] of SCAN_WORD_EMOJI) if (re.test(name || '')) return e;
return SCAN_CAT_EMOJI[category] || 'π¦';
}
function scanCategory(name, fallback) {
const g = typeof guessCategory === 'function' ? guessCategory(name) : 'Other';
return g !== 'Other' ? g : (SCAN_CATEGORIES.includes(fallback) ? fallback : 'Other');
}
function scanGetKey() { try { return localStorage.getItem(SCAN_KEY_STORE) || '' } catch { return '' } }
/* ---------- Built-in finder: automatic cut-outs on a plain background ---------- */
function scanSegment(rgba, w, h) {
const n = w * h, band = Math.max(2, Math.round(Math.min(w, h) * 0.04));
const R = [], G = [], B = [];
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
if (x >= band && x < w - band && y >= band && y < h - band) continue;
const k = (y * w + x) * 4; R.push(rgba[k]); G.push(rgba[k + 1]); B.push(rgba[k + 2]);
}
const med = a => { const s = a.slice().sort((p, q) => p - q); return s[s.length >> 1] || 0 };
const br = med(R), bg = med(G), bb = med(B);
const dist = new Float32Array(n), border = [];
for (let i = 0; i < n; i++) {
const k = i * 4, d = Math.abs(rgba[k] - br) + Math.abs(rgba[k + 1] - bg) + Math.abs(rgba[k + 2] - bb);
dist[i] = d;
const x = i % w, y = (i / w) | 0;
if (x < band || x >= w - band || y < band || y >= h - band) border.push(d);
}
border.sort((p, q) => p - q);
const thr = Math.max(60, (border[Math.floor(border.length * 0.9)] || 0) * 1.6);
let mask = new Uint8Array(n);
for (let i = 0; i < n; i++) mask[i] = dist[i] > thr ? 1 : 0;
const morph = (src, r, grow) => { // square dilate (grow) / erode
const out = new Uint8Array(n);
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
let v = grow ? 0 : 1;
for (let dy = -r; dy <= r && (grow ? !v : v); dy++) { const yy = y + dy; if (yy < 0 || yy >= h) { if (!grow) v = 0; continue }
for (let dx = -r; dx <= r; dx++) { const xx = x + dx; if (xx < 0 || xx >= w) { if (!grow) { v = 0; break } continue }
const m = src[yy * w + xx]; if (grow && m) { v = 1; break } if (!grow && !m) { v = 0; break } } }
out[y * w + x] = v;
}
return out;
};
mask = morph(morph(mask, 2, true), 1, false);
const seen = new Uint8Array(n), boxes = [], stack = [];
const minArea = n * 0.0015, maxArea = n * 0.5;
for (let s = 0; s < n; s++) {
if (!mask[s] || seen[s]) continue;
let x0 = w, y0 = h, x1 = 0, y1 = 0, area = 0;
stack.push(s); seen[s] = 1;
while (stack.length) {
const i = stack.pop(), x = i % w, y = (i / w) | 0; area++;
if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y;
if (x > 0 && mask[i - 1] && !seen[i - 1]) { seen[i - 1] = 1; stack.push(i - 1) }
if (x < w - 1 && mask[i + 1] && !seen[i + 1]) { seen[i + 1] = 1; stack.push(i + 1) }
if (y > 0 && mask[i - w] && !seen[i - w]) { seen[i - w] = 1; stack.push(i - w) }
if (y < h - 1 && mask[i + w] && !seen[i + w]) { seen[i + w] = 1; stack.push(i + w) }
}
const edges = (x0 === 0) + (y0 === 0) + (x1 === w - 1) + (y1 === h - 1);
if (area < minArea || area > maxArea || edges >= 3) continue;
boxes.push([x0, y0, x1 - x0 + 1, y1 - y0 + 1]);
}
return scanMergeBoxes(boxes, Math.round(w * 0.015));
}
function scanMergeBoxes(boxes, gap) {
let list = boxes.map(b => b.slice()), changed = true;
const near = (a, b) => a[0] - gap < b[0] + b[2] && b[0] - gap < a[0] + a[2] && a[1] - gap < b[1] + b[3] && b[1] - gap < a[1] + a[3];
while (changed) {
changed = false;
outer: for (let i = 0; i < list.length; i++) for (let j = i + 1; j < list.length; j++) if (near(list[i], list[j])) {
const a = list[i], b = list[j], x = Math.min(a[0], b[0]), y = Math.min(a[1], b[1]);
list[i] = [x, y, Math.max(a[0] + a[2], b[0] + b[2]) - x, Math.max(a[1] + a[3], b[1] + b[3]) - y];
list.splice(j, 1); changed = true; break outer;
}
}
const rowH = Math.max(1, Math.max(0, ...list.map(b => b[3])) * 0.6);
return list.sort((a, b) => Math.floor(a[1] / rowH) - Math.floor(b[1] / rowH) || a[0] - b[0]);
}
function scanIoU(a, b) {
const x = Math.max(a[0], b[0]), y = Math.max(a[1], b[1]), x2 = Math.min(a[0] + a[2], b[0] + b[2]), y2 = Math.min(a[1] + a[3], b[1] + b[3]);
const inter = Math.max(0, x2 - x) * Math.max(0, y2 - y);
return { iou: inter / (a[2] * a[3] + b[2] * b[3] - inter || 1), coverA: inter / (a[2] * a[3] || 1) };
}
// Combine cut-out boxes with named COCO detections (all in the same pixel space).
function scanCombine(segBoxes, cocoDets) {
const dets = (cocoDets || []).filter(d => SCAN_COCO[d.class] && d.score >= 0.35);
const used = new Set(), out = [];
for (const box of segBoxes) {
let best = null, bestScore = 0;
dets.forEach((d, k) => { const m = scanIoU(box, d.bbox); const s = Math.max(m.iou, m.coverA * 0.8); if (s > 0.3 && s > bestScore) { best = k; bestScore = s } });
if (best !== null) { used.add(best); const [name, category] = SCAN_COCO[dets[best].class]; out.push({ name, category, count: 1, box }) }
else out.push({ name: '', category: 'Other', count: 1, box });
}
dets.forEach((d, k) => { if (!used.has(k) && d.score >= 0.5) { const [name, category] = SCAN_COCO[d.class]; out.push({ name, category, count: 1, box: d.bbox.map(Math.round) }) } });
return out;
}
/* ---------- Gemini engine ---------- */
function scanPrompt() {
return 'You are cataloguing outdoor, backpacking and emergency gear laid out in this photo. ' +
'List every distinct physical item. Ignore the floor, table, sheet or surface, people, hands and shadows. ' +
'Group identical items and count them. Use short specific names (1 to 4 words), e.g. "Headlamp", "Water bottle", "First-aid kit", "AA batteries". ' +
'Return ONLY a JSON array, at most 30 entries, each: {"name": string, "category": one of ' + JSON.stringify(SCAN_CATEGORIES) +
', "count": integer, "box_2d": [ymin, xmin, ymax, xmax] normalized 0-1000 for one representative item}.';
}
function scanParseGemini(json, w, h) {
const text = (json?.candidates?.[0]?.content?.parts || []).map(p => p.text || '').join('').trim();
if (!text) throw Error(json?.promptFeedback?.blockReason ? 'Photo was blocked (' + json.promptFeedback.blockReason + ')' : 'No answer from Gemini');
let raw = text.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/, '');
const start = raw.search(/[\[{]/); if (start > 0) raw = raw.slice(start);
let data = JSON.parse(raw);
if (!Array.isArray(data)) data = data.items || data.boxes || data.objects || [];
const out = [];
for (const it of data.slice(0, SCAN_MAX_PIECES)) {
const name = String(it?.name || it?.label || '').trim().slice(0, 80);
if (!name) continue;
let box = null;
const b = it.box_2d || it.box;
if (Array.isArray(b) && b.length === 4 && b.every(v => Number.isFinite(Number(v)))) {
const [y0, x0, y1, x1] = b.map(v => Math.max(0, Math.min(1000, Number(v))));
if (x1 > x0 && y1 > y0) box = [Math.round(x0 / 1000 * w), Math.round(y0 / 1000 * h), Math.max(1, Math.round((x1 - x0) / 1000 * w)), Math.max(1, Math.round((y1 - y0) / 1000 * h))];
}
out.push({ name, category: SCAN_CATEGORIES.includes(it.category) ? it.category : scanCategory(name, 'Other'), count: scanClampCount(it.count ?? it.quantity ?? 1), box });
}
return out;
}
function scanPickModel(models) {
const ok = (models || []).filter(m => (m.supportedGenerationMethods || []).includes('generateContent'))
.map(m => String(m.name || '').replace(/^models\//, ''))
.filter(n => /flash/i.test(n) && !/(image|tts|live|audio|embed|thinking-exp|preview-tts)/i.test(n));
const lite = n => /lite/i.test(n) ? 1 : 0;
const ver = n => { const m = n.match(/gemini-(\d+(?:\.\d+)?)/); return m ? parseFloat(m[1]) : 0 };
const stable = n => /preview|exp/i.test(n) ? 1 : 0;
return ok.sort((a, b) => lite(a) - lite(b) || stable(a) - stable(b) || ver(b) - ver(a) || a.length - b.length)[0] || 'gemini-2.5-flash';
}
const SCAN_FALLBACK_MODELS = ['gemini-3.8-flash', 'gemini-3.5-flash', 'gemini-2.5-flash', 'gemini-flash-latest'];
const SCAN_API = 'https://generativelanguage.googleapis.com/v1beta/';
// Google's own explanation, with the key removed, so the person can see why a call failed.
async function scanGoogleError(r, key) {
let msg = '', code = '';
try { const j = await r.json(); msg = j?.error?.message || ''; code = j?.error?.status || '' } catch {}
msg = String(msg).split(key || '\u0000').join('[key]').replace(/\s+/g, ' ').trim().slice(0, 220);
return 'Google said ' + r.status + (code ? ' ' + code : '') + (msg ? ': ' + msg : '');
}
async function scanGeminiModels(key) {
let listed = null;
try { const m = localStorage.getItem(SCAN_MODEL_STORE); if (m) listed = m } catch {}
if (!listed) {
try {
const r = await fetch(SCAN_API + 'models?pageSize=200', { headers: { 'x-goog-api-key': key } });
if (r.ok) { const j = await r.json(); if ((j.models || []).length) listed = scanPickModel(j.models) }
} catch {}
}
return [...new Set([listed, ...SCAN_FALLBACK_MODELS].filter(Boolean))];
}
async function scanGeminiCall(key, body) {
let lastErr = null;
for (const model of await scanGeminiModels(key)) {
const r = await fetch(SCAN_API + 'models/' + encodeURIComponent(model) + ':generateContent', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-goog-api-key': key }, body });
if (r.ok) { try { localStorage.setItem(SCAN_MODEL_STORE, model) } catch {} return { json: await r.json(), model } }
const msg = await scanGoogleError(r, key);
if (r.status === 429) throw Error('Free Gemini limit reached for now (' + msg + ')');
if (r.status === 404 || (r.status === 400 && /model|not found|not supported/i.test(msg) && !/api.?key|credential|auth/i.test(msg))) { lastErr = msg; try { localStorage.removeItem(SCAN_MODEL_STORE) } catch {} continue }
throw Error(msg);
}
throw Error(lastErr || 'No Gemini model available for this key');
}
async function scanGemini(dataUrl, w, h, key) {
const body = JSON.stringify({ contents: [{ parts: [{ text: scanPrompt() }, { inline_data: { mime_type: 'image/jpeg', data: dataUrl.split(',')[1] } }] }],
generationConfig: { response_mime_type: 'application/json', temperature: 0.2 } });
return scanParseGemini((await scanGeminiCall(key, body)).json, w, h);
}
async function scanTestKey(statusEl) {
const key = scanGetKey();
if (!key) { statusEl.textContent = 'Save a key first.'; return false }
statusEl.textContent = 'Testing the key with Googleβ¦';
try {
const { model } = await scanGeminiCall(key, JSON.stringify({ contents: [{ parts: [{ text: 'Reply with the single word OK.' }] }] }));
statusEl.textContent = 'β Key works (' + model + '). Smart scan is ready.';
return true;
} catch (e) {
statusEl.textContent = 'β ' + (e.message || 'Could not reach Google') + (e instanceof TypeError ? ' β check the internet connection.' : '');
return false;
}
}
/* ---------- Built-in engine (on device) ---------- */
function scanLoadScript(src) {
return new Promise((ok, fail) => { const s = document.createElement('script'); s.src = src; s.onload = ok; s.onerror = () => fail(Error('Could not load ' + src)); document.head.appendChild(s) });
}
async function scanLoadCoco(onStatus) {
if (scanCoco) return scanCoco;
if (!scanCocoLoading) scanCocoLoading = (async () => {
onStatus?.('Getting the built-in item finder ready (first time only, about 18 MB)β¦');
if (!window.tf) await scanLoadScript('vendor/tf.min.js?v=1.17.0');
if (!window.cocoSsd) await scanLoadScript('vendor/coco-ssd.min.js?v=1.17.0');
scanCoco = await window.cocoSsd.load({ base: 'mobilenet_v2', modelUrl: 'vendor/coco-ssd/model.json' });
return scanCoco;
})().catch(e => { scanCocoLoading = null; throw e });
return scanCocoLoading;
}
async function scanBuiltIn(canvas, onStatus) {
const w = canvas.width, h = canvas.height, s = Math.min(1, 320 / Math.max(w, h));
const small = document.createElement('canvas'); small.width = Math.max(1, Math.round(w * s)); small.height = Math.max(1, Math.round(h * s));
const ctx = small.getContext('2d', { willReadFrequently: true }); ctx.drawImage(canvas, 0, 0, small.width, small.height);
const seg = scanSegment(ctx.getImageData(0, 0, small.width, small.height).data, small.width, small.height)
.map(b => [Math.round(b[0] / s), Math.round(b[1] / s), Math.round(b[2] / s), Math.round(b[3] / s)]);
let dets = [], note = '';
try { const model = await scanLoadCoco(onStatus); onStatus?.('Naming what it recognizesβ¦'); dets = await model.detect(canvas, 30, 0.3) }
catch (e) { note = 'Names unavailable offline β tap each name to type it.' }
return { found: scanCombine(seg, dets), note };
}
/* ---------- Pieces ---------- */
function scanCrop(canvas, box) {
if (!box) return '';
const side = Math.min(Math.max(box[2], box[3]) * 1.18, Math.max(canvas.width, canvas.height));
const cx = box[0] + box[2] / 2, cy = box[1] + box[3] / 2;
const sx = Math.max(0, Math.min(canvas.width - Math.min(side, canvas.width), cx - side / 2)), sy = Math.max(0, Math.min(canvas.height - Math.min(side, canvas.height), cy - side / 2));
const c = document.createElement('canvas'); c.width = c.height = 160;
const g = c.getContext('2d'); g.fillStyle = '#fff'; g.fillRect(0, 0, 160, 160);
g.drawImage(canvas, sx, sy, Math.min(side, canvas.width), Math.min(side, canvas.height), 0, 0, 160, 160);
return c.toDataURL('image/jpeg', 0.72);
}
function scanMakePieces(found, cropFn) {
const pieces = [], byName = new Map();
let unnamed = 0;
for (const f of found.slice(0, SCAN_MAX_PIECES)) {
const name = f.name || 'Item ' + (++unnamed);
const key = f.name ? scanNorm(name) : null;
if (key && byName.has(key)) { byName.get(key).count = Math.min(999, byName.get(key).count + scanClampCount(f.count)); continue }
const p = { id: 'p' + pieces.length + '_' + Math.random().toString(36).slice(2, 6), name, named: !!f.name, category: f.category || 'Other', count: scanClampCount(f.count), box: f.box || null, crop: cropFn ? cropFn(f.box) : '', inBag: false };
p.emoji = scanEmoji(p.name, p.category);
pieces.push(p); if (key) byName.set(key, p);
}
return pieces;
}
/* ---------- Save into the inventory ---------- */
function scanApply(kit, pieces) {
if (!Object.hasOwn(ZONES, kit)) throw Error('Unknown kit');
const groups = new Map();
for (const p of pieces) {
const name = String(p.name || '').trim().slice(0, 200), key = scanNorm(name);
if (!key) continue;
const g = groups.get(key);
if (g) { g.count = Math.min(999, g.count + scanClampCount(p.count)); g.inBag = g.inBag && !!p.inBag; if (!g.crop && p.crop) g.crop = p.crop }
else groups.set(key, { name, category: SCAN_CATEGORIES.includes(p.category) ? p.category : 'Other', count: scanClampCount(p.count), inBag: !!p.inBag, crop: /^data:image\/jpeg;base64,/.test(p.crop || '') && p.crop.length < 500000 ? p.crop : '' });
}
const added = [], updated = [];
for (const [key, g] of groups) {
const existing = items.find(i => !i.archived && i.place === kit && scanNorm(i.name) === key);
if (existing) {
existing.qty = g.count;
if (g.inBag) existing.status = 'packed'; else if (existing.status === 'packed') existing.status = 'stock';
if (!existing.photo && g.crop) existing.photo = g.crop;
updated.push(existing);
} else {
added.push({ id: 'scan' + Date.now().toString(36) + added.length + Math.random().toString(36).slice(2, 6), name: g.name, place: kit, category: g.category, status: g.inBag ? 'packed' : 'stock', qty: g.count,
spot: defaultSpot(kit, g.category, g.name), note: '', photo: g.crop, source: 'scan', archived: false });
}
}
if (added.length) items.unshift(...added);
return { added: added.length, updated: updated.length, packed: [...groups.values()].filter(g => g.inBag).length };
}
function scanSave() {
if (!scanState) return;
if (typeof storageBlocked !== 'undefined' && storageBlocked) { toast('Recover your saved data before scanning.'); return }
const kit = scanState.kit, pieces = scanState.pieces.filter(p => String(p.name || '').trim());
if (!pieces.length) { toast('Nothing to save yet'); return }
if (!protectSnapshot()) return;
const before = JSON.stringify(items);
let result;
try { result = scanApply(kit, pieces) } catch (e) { items = JSON.parse(before); toast('Could not save: ' + e.message); return }
if (!persist()) { items = JSON.parse(before); persist(); refresh(); toast('Not saved β storage is full. Remove some photos or export a backup.'); return }
scanClose(true);
refresh();
if (typeof zoneOpen === 'function') zoneOpen(kit);
const parts = [result.added ? result.added + ' added' : '', result.updated ? result.updated + ' updated' : ''].filter(Boolean).join(', ');
toast((parts || 'Saved') + ' Β· ' + result.packed + ' packed in ' + ZONES[kit].name, () => { items = JSON.parse(before); persist(); refresh(); toast('Scan undone') });
}
/* ---------- UI ---------- */
function scanEsc(s) { return typeof esc === 'function' ? esc(s) : String(s) }
function scanSetStep(step) {
for (const [id, s] of [['scanStart', 'start'], ['scanWorking', 'working'], ['scanBoard', 'board']]) $(id).classList.toggle('hidden', step !== s);
$('scanBag').classList.toggle('hidden', step !== 'board');
}
function scanKitOptions(selected) {
$('scanKit').innerHTML = Object.entries(ZONES).map(([id, z]) => ``).join('');
}
function scanOpen(kit) {
resetEntityGesture();setSelectMode(false);
const k = kit && Object.hasOwn(ZONES, kit) ? kit : (typeof openZone !== 'undefined' && openZone) || $('locationFilter').value || Object.keys(ZONES)[0];
scanState = { kit: k, pieces: [] };
scanKitOptions(k);
scanKeyUI();
$('scanKeyForm').classList.add('hidden');
scanSetStep('start');
$('scanLayer').classList.remove('hidden'); document.body.classList.add('scan-open');
document.body.style.overflow = 'hidden';
uiSyncLayers();$('scanLayer').querySelector('.scan-sheet')?.scrollTo?.({top:0});
setTimeout(() => {if(uiTopModal()===$('scanLayer'))$('scanClose').focus()}, 60);
}
function scanSaveKey(k) {
k = String(k || '').replace(/\s+/g, '').replace(/^["'`]+|["'`]+$/g, '');
if (!/^[\x21-\x7E]{20,200}$/.test(k)) return 'That does not look like a complete key. Copy the whole key from Google AI Studio and paste it again.';
try { localStorage.setItem(SCAN_KEY_STORE, k); localStorage.removeItem(SCAN_MODEL_STORE) } catch { return 'Could not save the key on this device.' }
return '';
}
function scanKeyUI() {
const k = scanGetKey();
$('scanEngine').textContent = k ? 'Smart scan' : 'Built-in';
$('scanKeyHint').textContent = k ? 'Smart scan is on: Google Gemini names your gear automatically.' : 'Without Smart scan, items are cut out automatically and you name them.';
$('scanKeyToggle').textContent = k ? 'Smart scan is on Β· change key' : 'β¨ Turn on Smart scan (free)';
$('geminiKeyStatus').textContent = k ? 'Key saved on this device (ends β¦' + k.slice(-4) + ').' : 'No key saved. Scan works on this device; AI is optional.';
$('geminiKeyRemove').classList.toggle('hidden', !k);
}
function scanClose(force) {
if ($('scanLayer').classList.contains('hidden')) return;
if (!force && scanState?.pieces?.length && !confirm('Leave without saving these items?')) return;
$('scanLayer').classList.add('hidden'); document.body.classList.remove('scan-open');
scanState = null;
uiSyncLayers();
if($('detail').classList.contains('show'))$('detailClose').focus();else (current==='inventory'?$('invScan'):$('scanOpen')).focus();
}
async function scanFile(file) {
if (!file || !scanState) return;
scanSetStep('working');
const status = t => { $('scanWorkingText').textContent = t };
status('Reading your photoβ¦');
try {
const url = URL.createObjectURL(file), img = new Image();
img.src = url; await img.decode();
const s = Math.min(1, 1280 / Math.max(img.naturalWidth, img.naturalHeight));
const canvas = document.createElement('canvas'); canvas.width = Math.round(img.naturalWidth * s); canvas.height = Math.round(img.naturalHeight * s);
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height); URL.revokeObjectURL(url);
const dataUrl = canvas.toDataURL('image/jpeg', 0.8);
let found = null, note = '', engine = 'Built-in';
const key = scanGetKey();
if (key) {
status('Smart scan is naming your gearβ¦');
try { found = await scanGemini(dataUrl, canvas.width, canvas.height, key); engine = 'Smart scan' }
catch (e) { note = e.message + ' β used the built-in finder instead.' }
}
if (!found) { status('Finding each itemβ¦'); const r = await scanBuiltIn(canvas, status); found = r.found; note = note || r.note }
if (!scanState) return;
Object.assign(scanState, { canvas, dataUrl, w: canvas.width, h: canvas.height, engine, note });
scanState.pieces = scanMakePieces(found, b => scanCrop(canvas, b));
$('scanEngine').textContent = engine;
scanRender(true);
} catch (e) {
status('');
scanSetStep('start');
toast('Could not read that photo: ' + e.message);
}
}
function scanAddAt(x, y) {
if (!scanState?.canvas) return;
const side = Math.round(Math.min(scanState.w, scanState.h) * 0.22), box = [Math.max(0, Math.round(x - side / 2)), Math.max(0, Math.round(y - side / 2)), side, side];
const n = scanState.pieces.reduce((m, p) => { const k = /^Item (\d+)$/.exec(String(p.name).trim()); return k ? Math.max(m, +k[1]) : m }, 0) + 1;
const p = scanMakePieces([{ name: '', category: 'Other', count: 1, box }], b => scanCrop(scanState.canvas, b))[0];
p.name = 'Item ' + n; p.named = false; p.id = 'p' + Date.now().toString(36);
scanState.pieces.push(p);
scanRender();
const input = document.querySelector(`[data-piece-name="${p.id}"]`); if (input) { input.focus(); input.select() }
}
function scanPieceHtml(p, i) {
const pic = p.crop ? `` : `${p.emoji}`;
return `