import type { StyleWire } from '@proulr/sdui-contract'; export type BdcElement = { type: string; props: Record; children: BdcElement[]; }; export type BdcComponent = (props: Record) => BdcElement; function flattenChild(child: unknown): BdcElement[] { if (child == null || typeof child === 'boolean') return []; if (Array.isArray(child)) return child.flatMap(flattenChild); if (typeof child === 'object' && 'type' in child) return [child as BdcElement]; if (typeof child === 'string' || typeof child === 'number') { return [{ type: '_text_node', props: { value: String(child) }, children: [] }]; } return []; } function createElement( type: string | BdcComponent, props: Record | null, ...childInputs: unknown[] ): BdcElement { const rawProps = props ?? {}; const { children: propsChildren, ...restProps } = rawProps as Record & { children?: unknown; }; const children = [ ...flattenChild(propsChildren), ...childInputs.flatMap(flattenChild), ]; if (typeof type === 'function') { return type({ ...restProps, children }); } return { type, props: restProps, children }; } export const jsx = createElement; export const jsxs = createElement; export const Fragment = '_fragment'; export function styleWire(style: StyleWire): { style: StyleWire } { return { style }; }