import type { l } from "@atproto/lex"; export const BRAND = Symbol.for("$"); export type Element = { type: l.NsidString; key?: string; props?: l.LexMap; }; export type LexiconComponent = { readonly $nsid: l.NsidString; }; export function createElement( type: l.NsidString, key?: string, props?: Record | null ): Element { const el: Record = { $: BRAND, type }; if (key != null) el.key = key; if (props != null) el.props = props; return el as Element; } export function isValidElement(value: unknown): value is Element { return ( typeof value === "object" && value !== null && (value as { $?: unknown }).$ === BRAND ); } export function keyStaticChildren(children: T): T { if (Array.isArray(children)) { return children.map((child, i) => isValidElement(child) && child.key == null ? { ...child, key: String(i) } : child ) as T; } if (isValidElement(children) && children.key == null) { return { ...children, key: "0" } as T; } return children; }