import type { FabricComponent, FabricElement, FabricNode } from './Fabric.ts' import { renderIntrinsic } from './intrinsic.ts' type MakeChildrenOptional = T extends { children?: any } ? Omit & Partial> : T export type ComponentBuilder = { (...args: unknown extends T ? [] : {} extends Omit ? [props?: MakeChildrenOptional] : [props: MakeChildrenOptional]): FabricComponent displayName?: string | undefined } export function createComponent(type: string, Component: (props: TProps) => FabricNode): ComponentBuilder { return (...args) => { const fn: FabricComponent = (() => renderIntrinsic(Component(args[0] as TProps) as FabricNode)) as any fn.component = Component fn.props = args[0]! as TProps fn.type = type fn.children = (...children: Array) => { const propsWithChildren = { ...(args[0] ?? {}), children() { return renderIntrinsic(children) }, } as unknown as TProps const fnChild = (() => renderIntrinsic(Component(propsWithChildren) as FabricNode)) as FabricElement fnChild.component = Component fnChild.props = args[0]! as TProps fnChild.type = type return fnChild } return fn } }