{"version":3,"file":"create_element-BUG6undN.cjs","sources":["../src/methods/wrap_element.ts","../src/components/fragment.ts","../src/methods/wrap_clone_element.ts","../src/jsx/runtime.ts","../src/methods/create_element.ts"],"sourcesContent":["import { SYMBOL_UNTRACKED_UNWRAPPED } from '../constants'\r\nimport { DEBUGGER, Stack } from 'soby'\r\nexport const SYMBOL_STACK = Symbol('STACK')\r\n\r\nexport interface StackTaggedFunction extends Function {\r\n  [SYMBOL_STACK]?: Stack\r\n}\r\n\r\n\r\nexport const wrapElement = <T extends Function>(element: T): T & StackTaggedFunction => {\r\n\r\n  element[SYMBOL_UNTRACKED_UNWRAPPED] = true\r\n  if (DEBUGGER.test)\r\n    element[SYMBOL_STACK] = new Stack(\"createElement\")\r\n\r\n  return element\r\n\r\n}","import type { Child } from '../types'\r\n\r\nexport const Fragment = ({ children, ...props }: { children?: Child }): Child => {\r\n\r\n    return children\r\n\r\n}\r\n","import { SYMBOL_CLONE } from '../constants'\r\nimport type { Component, Child, Refs } from '../types'\r\n\r\nexport interface CloneableType<P extends { children?: Child, ref?: Refs } = { children?: Child, ref?: Refs } /* extends Props */> {\r\n  Component: Component<P>\r\n  props?: P | null\r\n};\r\n\r\nexport const wrapCloneElement = <T, P /* extends Props */>(target: T, component: Component<P>, props?: P | null) => {\r\n  target[SYMBOL_CLONE] = { Component: component, props } as CloneableType\r\n  return target\r\n}\r\n","\n/* IMPORT */\n\nimport '../types'\nimport './types'\nexport { Fragment } from '../components/fragment'\n// import createElement from '../methods/create_element';\n// import { wrapCloneElement } from '../methods/wrap_clone_element';\nimport type { Child, Component, ComponentFunction, Element } from '../types'\nimport { wrapCloneElement, CloneableType } from '../methods/wrap_clone_element'\nimport { createElement } from '../index'\nimport { SYMBOL_CLONE, SYMBOL_DEFAULT, SYMBOL_JSX } from '../constants'\n\nconst wrapJsx = <P>(props: P) => {\n  if (props[SYMBOL_JSX]) return props\n  props[SYMBOL_JSX] = true\n  return props\n}\n\nexport const unwrapJsx = <P>(props: P) => {\n  delete props[SYMBOL_JSX]\n  return props\n}\n\nexport const isJsx = <P>(props: P) => !!props[SYMBOL_JSX]\n\n\nfunction getProps<P extends {} = { key?: string; children?: Child }>(component: string | Node | ComponentFunction<P>, props: P) {\n  if (typeof component === 'string') {\n    const ce = customElements.get(component)\n    if (!!ce) {\n      const defaultPropsFn = (ce as any).__component__?.[SYMBOL_DEFAULT]\n      if (!defaultPropsFn) {\n        console.error(`Component ${component} is missing default props. Please use the 'defaults' helper function to provide default props.`)\n      }\n      if (!props) props = defaultPropsFn() ?? {}\n    }\n  }\n  if (!props) props = {} as any\n\n  return wrapJsx(props)\n}\n\n\n/* MAIN */\n// const jsx = <P = {}>(component: Component<P>, props?: P | null, ...children: Child[]): Element => {\n//     return wrapCloneElement(createElement<P>(component as any, props, ...children), component, props);\n// };\n\n// React 16\nexport function jsx<P extends {} = {}>(component: Component<P>, props?: P, ...children: Child[]): Element\n//React 17\nexport function jsx<P extends {} = { key?: string; children?: Child }>(component: Component<P>, props?: P, key?: string): Element\nexport function jsx<P extends {} = { key?: string; children?: Child }>(component: Component<P>, props?: P, ...children: (string | Child)[]): Element {\n  if (typeof children === 'string') // React 16, key\n    return wrapCloneElement(createElement<P>(component as any, props ?? {} as P, children as string), component, props)\n\n  props = getProps<P>(component, props)\n\n  if (typeof children === 'string') // React 16, key\n    Object.assign(props as any, { children })\n\n  return wrapCloneElement(createElement<P>(component as any, props, (props as any)?.key as string), component, props)\n};\n\n//React 17 only\nexport const jsxDEV = <P extends {} = {}>(component: Component<P>, props: P | null, key: string, isStatic: boolean, source: { fileName: string, lineNumber: number, columnNumber: number }, self: any): Element => {\n  props = getProps<P>(component, props)\n\n  if (key)\n    Object.assign(props, { key })\n\n  return wrapCloneElement(createElement<P>(component as any, props), component, props)\n}\n\nexport const getMeta = (target: Element) => target?.[SYMBOL_CLONE] as CloneableType\n\n\nexport const jsxs = jsx ","/**\n * Element Creation API for Woby Framework\n * \n * This module provides the createElement function which is responsible for creating elements \n * in the Woby framework. It handles different types of components including functional components,\n * HTML elements, SVG elements, and custom elements.\n * \n * @module createElement\n */\n\nimport { untrack } from '../methods/soby'\nimport { wrapElement } from '../methods/wrap_element'\nimport { createComment, createHTMLNode, createSVGNode, createText } from '../utils/creators'\nimport { isClass, isFunction, isNode, isObject, isString, isSVGElement, isVoidChild } from '../utils/lang'\nimport { setChild, setProps } from '../utils/setters'\nimport type { Child, Component, Element } from '../types'\nimport { FragmentUtils } from '../utils/fragment'\nimport { customElement } from './custom_element'\nimport { Stack } from 'soby'\n\n/**\n * Creates an element from a component, props, and children\n * \n * This function is the core of the Woby rendering system. It handles the creation of elements\n * from various types of components:\n * 1. Functional components - Calls the function with props\n * 2. Class components - Instantiates the class with props\n * 3. String components - Creates HTML/SVG elements or custom elements\n * 4. Node components - Wraps existing DOM nodes\n * \n * @template P - The type of props for the component\n * @param component - The component to create (function, class, string tag name, or DOM node)\n * @param _props - The props to pass to the component\n * @param _children - The children elements\n * @returns A wrapped element that can be rendered\n * \n * @example\n * ```tsx\n * // Creating a functional component\n * const MyComponent = ({ name }: { name: string }) => <div>Hello, {name}!</div>\n * const element = createElement(MyComponent, { name: 'World' })\n * \n * // Creating an HTML element\n * const divElement = createElement('div', { className: 'container' }, 'Hello World')\n * \n * // Creating an SVG element\n * const svgElement = createElement('svg', { width: 100, height: 100 })\n * \n * // Creating a custom element\n * customElement('my-custom-element', ({ value }: { value: number }) => <div>Value: {value}</div>)\n * const customElementInstance = createElement('my-custom-element', { value: 42 })\n * ```\n */\nexport const createElement = <P = { children?: Child }>(component: Component<P>, _props?: P | null, ..._children: Child[]) => {\n\n    const children = _children.length > 1 ? _children : (_children.length > 0 ? _children[0] : undefined)\n    const hasChildren = !isVoidChild(children)\n    const { ...rest } = _props ?? {}\n\n    if (hasChildren && isObject(_props) && 'children' in _props) {\n\n        throw new Error('Providing \"children\" both as a prop and as rest arguments is forbidden')\n\n    }\n\n    if (isFunction(component)) {\n\n        const props = hasChildren ? Object.assign(_props, { children }) : _props\n\n        return wrapElement(() => {\n\n            return untrack(() => isClass(component) ? new (component as any)(props) : component.call(component, props as P)) //TSC\n\n        })\n\n    } else if (isString(component)) {\n\n        const isSVG = isSVGElement(component)\n        const isComment = component === 'comment'\n        const isText = component === 'text'\n\n        const createNode = isSVG ? createSVGNode : createHTMLNode\n        const create = isComment ? () => createComment((_props as any).data ?? '') : isText ? () => createText((_props as any).data ?? '') : createNode\n\n        return wrapElement((): Child => {\n            const ce = customElements.get(component) as ReturnType<typeof customElement>\n\n            const child = !!ce ? new ce(_props) : create(component) as HTMLElement\n\n            // if (!!ce)\n            //     (child as InstanceType<ReturnType<typeof customElement>>).props = { ..._props }\n\n            if (isSVG) child['isSVG'] = true\n\n            const stack = new Stack()\n\n            untrack(() => {\n\n                if (_props) {\n                    if (!!ce) {\n                        const { children, ...np } = _props as any //children already initialized in new ce(_props)\n                        setProps(child, np, stack)\n                    }\n                    else\n                        setProps(child, _props as any, stack)\n                }\n\n                // //already in prop\n                // if (hasChildren || ce?.__component__) {\n                //     // setChild(child, !!ce ? createElement(ce.__component__, (child as InstanceType<ReturnType<typeof customElement>>).props) : children, FragmentUtils.make(), stack)\n                //     setChild(child, children ?? _props.children, FragmentUtils.make(), stack)\n                // }\n\n            })\n\n            return child\n\n        })\n\n    } else if (isNode(component)) {\n\n        return wrapElement(() => component)\n\n    } else {\n\n        throw new Error('Invalid component')\n\n    }\n\n}"],"names":["SYMBOL_UNTRACKED_UNWRAPPED","DEBUGGER","Stack","SYMBOL_CLONE","SYMBOL_JSX","SYMBOL_DEFAULT","isVoidChild","isObject","isFunction","untrack","isClass","isString","isSVGElement","createSVGNode","createHTMLNode","createComment","createText","children","setProps","isNode"],"mappings":";;AAEO,MAAM,eAAe,OAAO,OAAO;AAOnC,MAAM,cAAc,CAAqB,YAAwC;AAEtF,UAAQA,QAAAA,0BAA0B,IAAI;AACtC,MAAIC,QAAAA,SAAS;AACX,YAAQ,YAAY,IAAI,IAAIC,QAAAA,MAAM,eAAe;A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