{"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 {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"],"names":[],"mappings":";;;AAYO,MAAM,YAAY,CAAC,KAA2C,WAAc;AACjF,QAAM,WAAW,mBAAA;AACjB,QAAM,UAAU,OAAO,qBAAqB,IAAI;AAEhD,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AACA,QAAM,WAAW,QAAQ;AAEzB,QAAM,gBAAgB,CACpB,cACqC;AACrC,QAAI,CAAC,UAAU,QAAQ;AACrB,aAAO;AAAA,IACT;AAEA,QACE,UAAU,OAAO,UAAU,IAAI,KAC/B,SAAS,MAAM,IAAI,QAAQ,UAAU,OAAO,QAAQ,EAAE,CAAC,GACvD;AACA,aAAO,UAAU;AAAA,IACnB;AAEA,WAAO,cAAc,UAAU,MAAM;AAAA,EACvC;AAEA,QAAM,cAAc,SAAS,MAAM;AACjC,UAAM,aAAa,QAAQ,EAAE;AAE7B,QAAI,YAAY;AACd,aAAO,kBAAkB,UAAU,UAAU;AAAA,IAC/C;AAEA,QAAI,CAAC,UAAU;AACb,aAAO;AAAA,IACT;AAEA,UAAM,YAAY,cAAc,QAAQ;AACxC,UAAM,cAAc,QAAQ,WAAW,SAAS,EAAE;AAClD,WAAO,kBAAkB,UAAU,WAAW;AAAA,EAChD,CAAC;AAED,SAAO;AAAA,IACL,OAAO,SAAS,MAAM,YAAY,OAAO,KAAK;AAAA,IAC9C,MAAM,CAAC,kBACL,YAAY,OAAO,KAAK,aAAa,KAAK,QAAQ,OAAO,WAAW;AAAA,IACtE,MAAM,CAAC,YACL,YAAY,OAAO,KAAK,SAAS,IAAI,KAAK,QAAQ,OAAO,WAAW;AAAA,IACtE,QAAQ,CAAC,kBACP,YAAY,OAAO,OAAO,aAAa,KAAK,QAAQ,OAAO,WAAW;AAAA,IACxE,WAAW,SAAS,MAAM,YAAY,OAAO,SAAS;AAAA,EAAA;AAE1D;"}