{"version":3,"file":"index.mjs","sources":["../../../../src/composables/useToggle/index.ts"],"sourcesContent":["import {\n  type ComponentInternalInstance,\n  computed,\n  getCurrentInstance,\n  inject,\n  type MaybeRefOrGetter,\n  toValue,\n} from 'vue'\n\nimport {globalShowHideStorageInjectionKey} from '../../utils/keys'\n\nexport const useToggle = (id: MaybeRefOrGetter<string | undefined> = undefined) => {\n  const instance = getCurrentInstance()\n  const storage = inject(globalShowHideStorageInjectionKey, null)\n\n  if (!storage) {\n    throw new Error(\n      'useToggle() was called outside of the setup() function or the showHide plugin is not provided.'\n    )\n  }\n  const registry = storage.values\n  // const registry = toRef(() => appRegistry.value)\n  const findComponent = (\n    component: ComponentInternalInstance\n  ): ComponentInternalInstance | null => {\n    if (!component.parent) {\n      return null\n    }\n\n    if (\n      component.parent.exposed?.['id'] &&\n      registry.value.get(toValue(component.parent.exposed.id))\n    ) {\n      return component.parent\n    }\n\n    return findComponent(component.parent)\n  }\n\n  const myComponent = computed(() => {\n    const resolvedId = toValue(id)\n\n    if (!registry) return null\n    if (resolvedId) {\n      const value = registry.value.get(resolvedId)\n      return toValue(value) || null\n    }\n\n    if (!instance) {\n      return null\n    }\n\n    const component = findComponent(instance)\n    return toValue(registry.value.get(toValue(component?.exposed?.id))) || null\n  })\n\n  return {\n    value: computed(() => myComponent.value?.value),\n    show: (resolveOnHide?: boolean) =>\n      myComponent.value?.show(resolveOnHide) || Promise.reject('not found'),\n    hide: (trigger?: string) =>\n      myComponent.value?.hide(trigger, true) || Promise.reject('not found'),\n    toggle: (resolveOnHide?: boolean) =>\n      myComponent.value?.toggle(resolveOnHide) || Promise.reject('not found'),\n    component: computed(() => myComponent.value?.component),\n  }\n}\n"],"names":[],"mappings":";;AAWa,MAAA,YAAY,CAAC,KAA2C,WAAc;AACjF,QAAM,WAAW,mBAAmB;AAC9B,QAAA,UAAU,OAAO,mCAAmC,IAAI;AAE9D,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,IACF;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