{"version":3,"file":"directiveComponent.mjs","names":["h","mergeProps","render","resolveComponent","consoleError","isObject","useDirectiveComponent","component","props","concreteComponent","hook","mountComponent","mounted","updated","unmounted","el","binding","vnode","_props","text","value","children","textContent","provides","ctx","instance","$","findComponentParent","node","appContext","Object","assign","create","root","stack","Set","walk","child","add","result","suspense","ssContent","Array","isArray","subTree","delete","from","reverse"],"sources":["../../src/composables/directiveComponent.ts"],"sourcesContent":["// Utilities\nimport { h, mergeProps, render, resolveComponent } from 'vue'\nimport { consoleError, isObject } from '@/util'\n\n// Types\nimport type {\n Component,\n ComponentInternalInstance,\n ComponentPublicInstance,\n ConcreteComponent,\n DirectiveBinding,\n ObjectDirective,\n VNode,\n} from 'vue'\nimport type { ComponentInstance } from '@/util'\n\ntype ExcludeProps =\n | 'v-slots'\n | `v-slot:${string}`\n | `on${Uppercase}${string}`\n | 'key'\n | 'ref'\n | 'ref_for'\n | 'ref_key'\n | '$children'\n\ndeclare const CustomDirectiveSymbol: unique symbol\ntype DirectiveHook = (el: any, binding: B, vnode: VNode, prevVNode: VNode) => void\nexport interface CustomDirective {\n created?: DirectiveHook\n beforeMount?: DirectiveHook\n mounted?: DirectiveHook\n beforeUpdate?: DirectiveHook\n updated?: DirectiveHook\n beforeUnmount?: DirectiveHook\n unmounted?: DirectiveHook\n [CustomDirectiveSymbol]: true\n}\n\nexport function useDirectiveComponent <\n Binding extends DirectiveBinding,\n> (component: string | Component, props?: (binding: Binding) => Record): CustomDirective\nexport function useDirectiveComponent <\n C extends Component,\n Props = Omit['$props'], ExcludeProps>\n> (component: string | C, props?: Record): ObjectDirective\nexport function useDirectiveComponent (\n component: string | Component,\n props?: Record | ((binding: DirectiveBinding) => Record)\n): ObjectDirective | CustomDirective {\n const concreteComponent = (typeof component === 'string'\n ? resolveComponent(component)\n : component) as ConcreteComponent\n\n const hook = mountComponent(concreteComponent, props)\n\n return {\n mounted: hook,\n updated: hook,\n unmounted (el: HTMLElement) {\n render(null, el)\n },\n }\n}\n\nfunction mountComponent (component: ConcreteComponent, props?: Record | ((binding: DirectiveBinding) => Record)) {\n return function (el: HTMLElement, binding: DirectiveBinding, vnode: VNode) {\n const _props = typeof props === 'function' ? props(binding) : props\n const text = binding.value?.text ?? binding.value ?? _props?.text\n const value = isObject(binding.value) ? binding.value : {}\n\n // Get the children from the props or directive value, or the element's children\n const children = () => text ?? el.textContent\n\n // If vnode.ctx is the same as the instance, then we're bound to a plain element\n // and need to find the nearest parent component instance to inherit provides from\n const provides = (vnode.ctx === binding.instance!.$\n ? findComponentParent(vnode, binding.instance!.$)?.provides\n : vnode.ctx?.provides) ?? binding.instance!.$.provides\n\n const node = h(component, mergeProps(_props, value), children)\n node.appContext = Object.assign(\n Object.create(null),\n (binding.instance as ComponentPublicInstance).$.appContext,\n { provides }\n )\n\n render(node, el)\n }\n}\n\nfunction findComponentParent (vnode: VNode, root: ComponentInternalInstance): ComponentInternalInstance | null {\n // Walk the tree from root until we find the child vnode\n const stack = new Set()\n const walk = (children: VNode[]): boolean => {\n for (const child of children) {\n if (!child) continue\n\n if (child === vnode || (child.el && vnode.el && child.el === vnode.el)) {\n return true\n }\n\n stack.add(child)\n let result\n if (child.suspense) {\n result = walk([child.ssContent!])\n } else if (Array.isArray(child.children)) {\n result = walk(child.children as VNode[])\n } else if (child.component?.vnode) {\n result = walk([child.component?.subTree])\n }\n if (result) {\n return result\n }\n stack.delete(child)\n }\n\n return false\n }\n if (!walk([root.subTree])) {\n consoleError('Could not find original vnode, component will not inherit provides')\n return root\n }\n\n // Return the first component parent\n const result = Array.from(stack).reverse()\n for (const child of result) {\n if (child.component) {\n return child.component\n }\n }\n return root\n}\n"],"mappings":"AAAA;AACA,SAASA,CAAC,EAAEC,UAAU,EAAEC,MAAM,EAAEC,gBAAgB,QAAQ,KAAK;AAAA,SACpDC,YAAY,EAAEC,QAAQ,6BAE/B;AA0CA,OAAO,SAASC,qBAAqBA,CACnCC,SAA6B,EAC7BC,KAAkF,EAC/C;EACnC,MAAMC,iB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