{"version":3,"file":"IntlayerNode.mjs","names":[],"sources":["../../src/IntlayerNode.tsx"],"sourcesContent":["import type { NodeProps } from '@intlayer/core/interpreter';\nimport { getIntlayerNodePrototype } from '@intlayer/core/utils';\nimport type { ResolvedEditor } from '@intlayer/types/module_augmentation';\nimport { isValidElement, type PropsWithChildren, type ReactNode } from 'react';\n\n/**\n * Content node returned by `useIntlayer`.\n *\n * Without the editor it is typed as its value (`\"Hello\" & { value: \"Hello\" }`)\n * so it can go straight into string props (`aria-label={content.title}`).\n * With the editor, it is a React node wrapping the editable selector, so\n * string props must use `.value`.\n */\nexport type IntlayerNode<\n  T = NodeProps['children'],\n  AdditionalProps = unknown,\n> = ResolvedEditor<\n  T & { value: T } & AdditionalProps,\n  ReactNode & { value: T } & AdditionalProps\n>;\n\ntype RenderIntlayerNodeProps<T> = PropsWithChildren<{\n  value: T;\n  children: ReactNode;\n  additionalProps?: { [key: string]: any };\n}>;\n\n/**\n * Renders content as a React element that also behaves like its value\n * (`node.value`, `${node}`, `node.toUpperCase()`).\n *\n * The node is a copy of the element built on a shared prototype serving the\n * value's members, rather than a Proxy: React reads an element's fields many\n * times while reconciling, and a Proxy trap makes each read several times\n * slower.\n *\n * @param props - The value, the rendered children and extra node members.\n * @returns The renderable node.\n */\nexport const renderIntlayerNode = <\n  T, // Broadened to support arrays, numbers, objects, etc.\n>({\n  children,\n  value,\n  additionalProps,\n}: RenderIntlayerNodeProps<T>): IntlayerNode<T> =>\n  // Fields first, prototype last: set the other way round, each field would\n  // first look for a setter along the prototype chain, through its Proxy\n  Object.setPrototypeOf(\n    {\n      ...(isValidElement(children) ? children : <>{children}</>),\n      value,\n      ...additionalProps,\n    },\n    getIntlayerNodePrototype(value)\n  );\n"],"mappings":";;;;;;;;;;;;;;;;;AAuCA,MAAa,sBAEX,EACA,UACA,OACA,sBAIA,OAAO,eACL;CACE,GAAI,eAAe,QAAQ,IAAI,WAAW,kCAAG,SAAW;CACxD;CACA,GAAG;AACL,GACA,yBAAyB,KAAK,CAChC"}