// ── Metadata badge ──────────────────────────────────────────────────────────
// Always-visible, compact wafer/lot identity overlay for `renderWaferMap`'s
// single-wafer canvas. Sits bottom-left, diagonally opposite the toolbar
// (top-right) — the toolbar has no responsive collapse and can span nearly
// the full width of a narrow canvas, so a badge sharing its corner would risk
// collision; vertical separation avoids that regardless of toolbar width.
//
// Deliberately an overlay, not a layout element: it never changes canvasWrap's
// box size, so the map never shrinks to make room for it. Collapsed by
// default to a single identifying line; click/Enter/Space expands in place
// (still an overlay, not a layout push) to the full compact field set.
//
// Mounted as a child of canvasWrap in renderWaferMap.ts, so it is
// automatically covered whenever the Insights tab's inset:0 overlay is shown
// — same coverage behaviour the toolbar already relies on, no extra wiring.
import { metadataEntries, buildCompactMetadataRows } from './summaryPanel.js';
import { CLR, Z_BASE, wireExpandToggle } from './toolbar.js';
function collapsedLabel(meta, lotStack) {
    if (lotStack) {
        return lotStack.aggrMethod
            ? `${lotStack.lotSize} wafers · ${lotStack.aggrMethod}`
            : `${lotStack.lotSize} wafers`;
    }
    if (meta.lot && meta.waferId !== undefined && meta.waferId !== '') {
        const lot = String(meta.lot);
        const waferId = String(meta.waferId);
        // Some hosts (e.g. tsmap, for multi-lot views) embed the lot ID inside
        // waferId itself (e.g. lot "CLUST-LOT-03", waferId "CLUST-LOT-03 · W02")
        // — joining unconditionally would repeat it. If waferId already contains
        // the lot string, it's already self-identifying; don't prefix it again.
        return waferId.includes(lot) ? waferId : `${lot} · ${waferId}`;
    }
    for (const key of ['lot', 'waferId', 'product', 'testProgram']) {
        const v = meta[key];
        if (v !== undefined && v !== null && v !== '')
            return String(v);
    }
    return undefined;
}
export function createMetadataBadge(metadata, opts = {}) {
    let meta = metadata ?? {};
    let lotStack = opts.lotStack;
    let expanded = false;
    const el = document.createElement('div');
    Object.assign(el.style, {
        position: 'absolute',
        bottom: '4px',
        left: '4px',
        zIndex: Z_BASE,
        background: CLR.menuBg,
        border: `1px solid ${CLR.menuBorder}`,
        borderRadius: '4px',
        boxShadow: '0 1px 4px rgba(0,0,0,0.12)',
        padding: '3px 8px',
        fontSize: '11px',
        color: CLR.value,
        cursor: 'pointer',
        maxWidth: '260px',
        pointerEvents: 'auto',
    });
    const summaryRow = document.createElement('div');
    Object.assign(summaryRow.style, {
        display: 'flex', alignItems: 'center', gap: '6px', whiteSpace: 'nowrap',
    });
    const summaryText = document.createElement('span');
    summaryText.style.overflow = 'hidden';
    summaryText.style.textOverflow = 'ellipsis';
    const chevron = document.createElement('span');
    Object.assign(chevron.style, { fontSize: '12px', lineHeight: '1', color: CLR.label, flexShrink: '0' });
    summaryRow.appendChild(summaryText);
    summaryRow.appendChild(chevron);
    const detailWrap = document.createElement('div');
    Object.assign(detailWrap.style, { marginTop: '5px', display: 'none' });
    el.appendChild(summaryRow);
    el.appendChild(detailWrap);
    function render() {
        const label = collapsedLabel(meta, lotStack) ?? 'Wafer info';
        summaryText.textContent = label;
        chevron.textContent = expanded ? '▴' : '▾';
        el.setAttribute('aria-expanded', String(expanded));
        el.setAttribute('aria-label', `Wafer info: ${label}. ${expanded ? 'Click to collapse.' : 'Click to expand.'}`);
        detailWrap.innerHTML = '';
        detailWrap.style.display = expanded ? 'block' : 'none';
        if (!expanded)
            return;
        if (lotStack) {
            const stackLine = document.createElement('div');
            Object.assign(stackLine.style, { fontWeight: '600', marginBottom: '3px' });
            stackLine.textContent = lotStack.aggrMethod
                ? `${lotStack.lotSize} wafers stacked · ${lotStack.aggrMethod}`
                : `${lotStack.lotSize} wafers stacked`;
            detailWrap.appendChild(stackLine);
        }
        const rows = buildCompactMetadataRows(meta);
        if (rows)
            detailWrap.appendChild(rows);
    }
    const expandToggle = wireExpandToggle(el, (open) => {
        expanded = open;
        render();
    });
    render();
    return {
        el,
        update(newMetadata, newLotStack) {
            meta = newMetadata ?? {};
            lotStack = newLotStack;
            render();
        },
        isEmpty() {
            return !lotStack && metadataEntries(meta).length === 0;
        },
        destroy() {
            expandToggle.destroy();
            el.remove();
        },
    };
}
//# sourceMappingURL=metadataBadge.js.map