{"version":3,"file":"index.mjs","names":[],"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 {showHideRegistryKey} from '../../utils/keys'\nimport {getActiveShowHide} from '../../utils/registryAccess'\n\nexport const useToggle = (id: MaybeRefOrGetter<string | undefined> = undefined) => {\n  const instance = getCurrentInstance()\n  const storage = inject(showHideRegistryKey, null)\n\n  if (!storage) {\n    throw new Error(\n      'useToggle() must be called within setup(), and BApp, useRegistry or plugin must be installed/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 (resolvedId) {\n      return getActiveShowHide(registry, resolvedId)\n    }\n\n    if (!instance) {\n      return null\n    }\n\n    const component = findComponent(instance)\n    const componentId = toValue(component?.exposed?.id)\n    return getActiveShowHide(registry, componentId)\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"],"mappings":";;;;AAYA,IAAa,aAAa,KAA2C,KAAA,MAAc;CACjF,MAAM,WAAW,oBAAoB;CACrC,MAAM,UAAU,OAAO,qBAAqB,KAAK;AAEjD,KAAI,CAAC,QACH,OAAM,IAAI,MACR,yGACD;CAEH,MAAM,WAAW,QAAQ;CAEzB,MAAM,iBACJ,cACqC;AACrC,MAAI,CAAC,UAAU,OACb,QAAO;AAGT,MACE,UAAU,OAAO,UAAU,SAC3B,SAAS,MAAM,IAAI,QAAQ,UAAU,OAAO,QAAQ,GAAG,CAAC,CAExD,QAAO,UAAU;AAGnB,SAAO,cAAc,UAAU,OAAO;;CAGxC,MAAM,cAAc,eAAe;EACjC,MAAM,aAAa,QAAQ,GAAG;AAE9B,MAAI,WACF,QAAO,kBAAkB,UAAU,WAAW;AAGhD,MAAI,CAAC,SACH,QAAO;AAKT,SAAO,kBAAkB,UADL,QADF,cAAc,SAAS,EACF,SAAS,GAAG,CACJ;GAC/C;AAEF,QAAO;EACL,OAAO,eAAe,YAAY,OAAO,MAAM;EAC/C,OAAO,kBACL,YAAY,OAAO,KAAK,cAAc,IAAI,QAAQ,OAAO,YAAY;EACvE,OAAO,YACL,YAAY,OAAO,KAAK,SAAS,KAAK,IAAI,QAAQ,OAAO,YAAY;EACvE,SAAS,kBACP,YAAY,OAAO,OAAO,cAAc,IAAI,QAAQ,OAAO,YAAY;EACzE,WAAW,eAAe,YAAY,OAAO,UAAU;EACxD"}