// formula is Schritt 7 (continued) of the module-system migration: unlike
// text/date, this is a genuinely new visual treatment, not parity with the
// old rendering -- a formula cell used to look byte-identical to a plain
// physical text cell (only the column header got a "computed" badge, see
// computedBadge() in DataGrid.svelte). Read-only: a formula column's
// field_kind is never "physical", so isEditableColumn() in DataGrid.svelte
// already keeps it out of edit mode entirely -- this module has no
// "formula-edit" element at all, and its manifest.json's edit_tag is "".
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(
	'formula-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 = 'formula';
			this.textContent = formatValue(raw);
		}
	}
);
