{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useToast/index.ts"],"sourcesContent":["import {\n  type ComponentPublicInstance,\n  computed,\n  type ComputedRef,\n  inject,\n  markRaw,\n  type MaybeRef,\n  onScopeDispose,\n  ref,\n  type Ref,\n} from 'vue'\nimport {\n  orchestratorRegistryKey,\n  type OrchestratorStoreObject,\n  showHideRegistryKey,\n} from '../../utils/keys'\nimport type {ContainerPosition} from '../../types/Alignment'\nimport type {\n  ComponentController,\n  ControllerKey,\n  ToastOrchestratorArrayValue,\n  ToastOrchestratorCreateParamBase,\n} from '../../types'\nimport {buildController, getOrchestratorControllerId} from '../orchestratorShared'\nimport {BToast} from '../../components'\n\nconst posDefault: ContainerPosition = 'top-end'\n\nexport const useToast = () => {\n  const orchestratorRegistry = inject(orchestratorRegistryKey, null)\n  if (!orchestratorRegistry)\n    throw new Error(\n      'useToast() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided.'\n    )\n  const {store, _isToastAppend, _isOrchestratorInstalled} = orchestratorRegistry\n  const showHideRegistry = inject(showHideRegistryKey, null)\n\n  /**\n   * @returns {ComponentController<typeof BToast, ToastOrchestratorParam>}\n   */\n  // Uses a `function` declaration (rather than a generic arrow function assigned to a const) so\n  // that TypeScript preserves per-call generic inference when `create` is returned as part of\n  // `useToast()`'s inferred return object.\n  function create<\n    ComponentProps extends Record<string, unknown> = Record<string, unknown>,\n    T extends ToastOrchestratorCreateParamBase<ComponentProps> =\n      ToastOrchestratorCreateParamBase<ComponentProps>,\n  >(\n    obj: MaybeRef<T> = {} as T\n  ): ComponentController<typeof BToast, Ref<ToastOrchestratorArrayValue>> {\n    if (!_isOrchestratorInstalled.value)\n      throw new Error('The BApp component must be mounted to use the toast composable')\n\n    const toastComp = markRaw(BToast)\n    const resolvedProps = ref(obj)\n    const toastStore = computed(() => store.value.toast)\n    const {htmlAttributeId, storeId} = getOrchestratorControllerId(resolvedProps.value.id)\n\n    const {resolve, controller} = buildController<\n      typeof BToast,\n      ComputedRef<OrchestratorStoreObject['toast']>\n    >(storeId, toastStore)\n\n    const value = computed<ToastOrchestratorArrayValue>({\n      get: () => {\n        const {component = toastComp, options, slots, ...props} = resolvedProps.value\n\n        return {\n          component,\n          options,\n          slots,\n          id: storeId,\n          fns: {\n            resolve,\n            setRef: (v: ComponentPublicInstance) => {\n              controller.ref = v\n            },\n            destroy: controller.destroy,\n          },\n          props: {\n            ...props,\n            id: htmlAttributeId,\n            position: props.position || posDefault,\n          },\n        }\n      },\n      set: (v) => {\n        resolvedProps.value = {\n          ...resolvedProps.value,\n          ...v.props,\n        }\n      },\n    })\n\n    toastStore.value.set(storeId, value)\n\n    onScopeDispose(async () => {\n      await controller[Symbol.asyncDispose]()\n    }, true)\n\n    return controller\n  }\n\n  /**\n   * Hides a mounted BToast that registered itself under `id`. Going through the component, rather\n   * than through the store, is what lets the trigger flow through the regular hide cycle\n   *\n   * @returns whether a toast was found\n   */\n  const hideRegisteredInstance = (id: ControllerKey, trigger?: string): boolean => {\n    // The show/hide registry is keyed by the html id, symbols never reach it\n    if (typeof id !== 'string') return false\n    const instances = showHideRegistry?.values.value.get(id)?.instances\n    if (!instances) return false\n    // Every show/hide component shares this registry, so an id may resolve to a BCollapse or a\n    // BModal. Only toasts are ours to hide. Newest first, the way the registry's getActive does\n    for (let i = instances.length - 1; i >= 0; i--) {\n      const instance = instances[i]\n      if (instance?.component.type !== BToast) continue\n      instance.hide(trigger, true)\n      return true\n    }\n    return false\n  }\n\n  /**\n   * Hides a toast that lives in the orchestrator store. Prefers the mounted instance, falling back\n   * to the store entry itself, which also covers toasts that the orchestrator has yet to render\n   * and toasts rendered through a custom `component`\n   */\n  const hideStoreItem = (item: Ref<ToastOrchestratorArrayValue>, trigger?: string): void => {\n    if (hideRegisteredInstance(item.value.props.id ?? item.value.id, trigger)) return\n    item.value = {\n      ...item.value,\n      props: {\n        ...item.value.props,\n        modelValue: false,\n      },\n    }\n  }\n\n  /**\n   * Hide all toasts\n   * @param trigger - The trigger to hide all toasts\n   */\n  const hideAll = (trigger?: string): void => {\n    for (const item of store.value.toast.values()) {\n      hideStoreItem(item, trigger)\n    }\n  }\n\n  /**\n   * Hide a toast\n   * @param trigger - The trigger to hide the toast\n   * @param id - The id of the toast to hide, when omitted every toast is hidden\n   */\n  const hide = (trigger?: string, id?: ControllerKey): void => {\n    if (id === undefined) {\n      hideAll(trigger)\n      return\n    }\n    const item = store.value.toast.get(id)\n    if (item) {\n      hideStoreItem(item, trigger)\n      return\n    }\n    // Not created through the composable, it could still be a BToast declared in a template\n    hideRegisteredInstance(id, trigger)\n  }\n\n  return {\n    _isToastAppend,\n    _isOrchestratorInstalled,\n    store,\n    create,\n    hide,\n    hideAll,\n  }\n}\n"],"mappings":";;;;;AA0BA,IAAM,aAAgC;AAEtC,IAAa,iBAAiB;CAC5B,MAAM,uBAAuB,OAAO,yBAAyB,IAAI;CACjE,IAAI,CAAC,sBACH,MAAM,IAAI,MACR,uGACF;CACF,MAAM,EAAC,OAAO,gBAAgB,6BAA4B;CAC1D,MAAM,mBAAmB,OAAO,qBAAqB,IAAI;;;;CAQzD,SAAS,OAKP,MAAmB,CAAC,GACkD;EACtE,IAAI,CAAC,yBAAyB,OAC5B,MAAM,IAAI,MAAM,gEAAgE;EAElF,MAAM,YAAY,QAAQ,cAAM;EAChC,MAAM,gBAAgB,IAAI,GAAG;EAC7B,MAAM,aAAa,eAAe,MAAM,MAAM,KAAK;EACnD,MAAM,EAAC,iBAAiB,YAAW,4BAA4B,cAAc,MAAM,EAAE;EAErF,MAAM,EAAC,SAAS,eAAc,gBAG5B,SAAS,UAAU;EAErB,MAAM,QAAQ,SAAsC;GAClD,WAAW;IACT,MAAM,EAAC,YAAY,WAAW,SAAS,OAAO,GAAG,UAAS,cAAc;IAExE,OAAO;KACL;KACA;KACA;KACA,IAAI;KACJ,KAAK;MACH;MACA,SAAS,MAA+B;OACtC,WAAW,MAAM;MACnB;MACA,SAAS,WAAW;KACtB;KACA,OAAO;MACL,GAAG;MACH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