// lookup is Schritt 7 (continued) of the module-system migration. Read-only:
// a lookup column's field_kind is never "physical", so isEditableColumn()
// in DataGrid.svelte already keeps it out of edit mode entirely -- no
// "lookup-edit" element, manifest.json's edit_tag is "". Reached only via
// the same "last resort" fallback position text/date/formula use (after
// label/tags/markdown/color/FK-badge/rollup-FK-badge have all had first
// refusal) -- a lookup value itself is never a foreign key TO something
// else, so those checks essentially never apply to it in practice, but the
// module still doesn't have to know that.
//
// Same genuinely-new visual treatment as formula-cell (a lookup value used
// to render byte-identical to plain text, only the column header got a
// "computed" badge): italic, muted, tagged with its field_kind in the
// tooltip so lookup/rollup/formula are each identifiable, not just
// generically "computed".
function formatValue(value) {
	if (value === null || value === undefined) return '';
	if (typeof value === 'boolean') return value ? 'true' : 'false';
	if (typeof value === 'object') return JSON.stringify(value);
	return String(value);
}

customElements.define(
	'lookup-cell',
	class extends HTMLElement {
		static get observedAttributes() {
			return ['value'];
		}
		connectedCallback() {
			this.render();
		}
		attributeChangedCallback() {
			this.render();
		}
		render() {
			let raw = null;
			try {
				raw = JSON.parse(this.getAttribute('value') ?? 'null');
			} catch {
				raw = null;
			}
			this.className = 'italic text-ink-500';
			this.title = 'lookup';
			this.textContent = formatValue(raw);
		}
	}
);
