{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useModal/index.ts"],"sourcesContent":["import {\n  type Component,\n  type ComponentInternalInstance,\n  computed,\n  getCurrentInstance,\n  inject,\n  isReadonly,\n  isRef,\n  markRaw,\n  onScopeDispose,\n  type Ref,\n  shallowRef,\n  toValue,\n  watch,\n} from 'vue'\nimport {useSharedModalStack} from '../useModalManager'\n\nimport {orchestratorRegistryKey} from '../../utils/keys'\nimport type {\n  ControllerKey,\n  ModalOrchestratorArrayValue,\n  ModalOrchestratorCreateParam,\n  ModalOrchestratorParam,\n  OrchestratorCreateOptions,\n  PromiseWithComponent,\n} from '../../types/ComponentOrchestratorTypes'\nimport {buildPromise} from '../orchestratorShared'\nimport {BModal} from '../../components'\n\nexport const useModal = () => {\n  const orchestratorRegistry = inject(orchestratorRegistryKey, null)\n  if (!orchestratorRegistry) {\n    throw Error(\n      'useModal() must be called within setup(), and BApp, useRegistry or plugin must be installed/provided.'\n    )\n  }\n  const {store, _isOrchestratorInstalled} = orchestratorRegistry\n\n  /**\n   * @returns {PromiseWithComponent}  Returns a promise object with methods to control the modal (show, hide, toggle, get, set, destroy)\n   */\n  const create = <ComponentProps = Record<string, unknown>>(\n    obj: ModalOrchestratorCreateParam<ComponentProps> = {} as ModalOrchestratorCreateParam<ComponentProps>,\n    options: OrchestratorCreateOptions = {}\n  ): PromiseWithComponent<typeof BModal, ModalOrchestratorParam<ComponentProps>> => {\n    if (!_isOrchestratorInstalled.value) {\n      throw new Error('BApp component must be mounted to use the modal controller')\n    }\n\n    const resolvedProps = (isRef(obj) ? obj : shallowRef(obj)) as Ref<\n      ModalOrchestratorParam<ComponentProps>\n    >\n    const _self = resolvedProps.value?.id || Symbol('Modals controller')\n\n    const promise = buildPromise<\n      typeof BModal,\n      ModalOrchestratorParam<ComponentProps>,\n      ModalOrchestratorArrayValue\n    >(_self, store as Ref<ModalOrchestratorArrayValue[]>)\n\n    promise.stop = watch(\n      resolvedProps,\n      (_newValue) => {\n        const newValue = {...toValue(_newValue)} as Record<string, unknown>\n        const previousIndex = store.value.findIndex((el) => el._self === _self)\n        const previous =\n          previousIndex === -1 ? {_component: markRaw(BModal)} : store.value[previousIndex]\n        const v = {\n          type: 'modal',\n          _self,\n          position: 'modal',\n          ...previous,\n          options,\n          promise,\n        } as ModalOrchestratorArrayValue\n\n        for (const key in newValue) {\n          if (key.startsWith('on')) {\n            v[key as keyof ModalOrchestratorArrayValue] = newValue[key] as never\n          } else if (key === 'component' && newValue.component) {\n            v._component = markRaw(newValue.component as Component)\n          } else if (key === 'slots' && newValue.slots) {\n            v.slots = markRaw(newValue.slots) as never\n          } else {\n            v[key as keyof ModalOrchestratorArrayValue] = toValue(newValue[key]) as never\n          }\n        }\n        v.modelValue = v.modelValue ?? false\n        v['onUpdate:modelValue'] = (val: boolean) => {\n          const onUpdateModelValue = newValue['onUpdate:modelValue'] as\n            | ((val: boolean) => void)\n            | undefined\n          onUpdateModelValue?.(val)\n          const {modelValue} = toValue(obj)\n          if (isRef(obj) && !isRef(modelValue)) obj.value.modelValue = val\n          if (isRef(modelValue) && !isReadonly(modelValue)) {\n            ;(modelValue as Ref<ModalOrchestratorArrayValue['modelValue']>).value = val\n          }\n          const modal = store.value.find((el) => el._self === _self)\n          if (modal) {\n            modal.modelValue = val\n          }\n        }\n\n        if (previousIndex === -1) {\n          store.value.push(v)\n        } else {\n          store.value.splice(previousIndex, 1, v)\n        }\n      },\n      {\n        immediate: true,\n        deep: true,\n      }\n    )\n    onScopeDispose(() => {\n      const modal = store.value.find((el) => el._self === _self)\n      if (modal) {\n        modal.promise.value.destroy?.()\n      }\n    }, true)\n    return promise.value\n  }\n\n  const {lastStack, stack, registry} = useSharedModalStack()\n  /**\n   * Show a modal\n   * @param id - The id of the modal to show\n   */\n  const show = (id?: ControllerKey) => {\n    if (id === undefined) {\n      if (lastStack?.value) {\n        lastStack?.value.exposed?.show()\n      }\n    } else {\n      const stackModal = stack?.value.find((modal) => modal.exposed?.id === id)\n      if (stackModal) {\n        stackModal.exposed?.show()\n        return\n      }\n      const modal = store.value.find((el) => el._self === id)\n      if (modal) {\n        modal.modelValue = true\n        modal['onUpdate:modelValue']?.(true)\n      } else {\n        stack?.value.forEach((modal) => {\n          if (modal.exposed?.id === id) {\n            modal.exposed?.show()\n          }\n        })\n      }\n    }\n  }\n\n  /**\n   * Hide a modal\n   * @param trigger - The trigger to hide the modal\n   * @param id - The id of the modal to hide\n   */\n  const hide = (trigger?: string, id?: ControllerKey) => {\n    if (id === undefined) {\n      if (lastStack?.value) {\n        lastStack?.value.exposed?.hide(trigger)\n      }\n    } else {\n      const stackModal = stack?.value.find((modal) => modal.exposed?.id === id)\n      if (stackModal) {\n        stackModal.exposed?.hide(trigger)\n        return\n      }\n      const modal = store.value.find((el) => el._self === id)\n      if (modal) {\n        modal.modelValue = false\n        modal['onUpdate:modelValue']?.(false)\n      } else {\n        stack?.value.forEach((modal) => {\n          if (modal.exposed?.id === id) {\n            modal.exposed?.hide(trigger, true)\n          }\n        })\n      }\n    }\n  }\n\n  /**\n   * Hide all modals\n   * @param trigger - The trigger to hide all modals\n   */\n  const hideAll = (trigger?: string) => {\n    stack?.value.forEach((modal) => {\n      modal.exposed?.hide(trigger, true)\n    })\n  }\n\n  const get = (id: ControllerKey) => {\n    const modal = store.value.find((el) => el._self === id)\n    if (modal) {\n      return {\n        modal,\n        show() {\n          modal?.promise.value.show()\n        },\n        hide(trigger?: string) {\n          modal?.promise.value.hide(trigger)\n        },\n      }\n    }\n    if (registry?.value) {\n      for (const [, modal] of registry?.value.entries() ?? []) {\n        if (toValue(modal.exposed?.id) === id) {\n          return {\n            modal,\n            show() {\n              modal.exposed?.show()\n            },\n            hide(trigger?: string) {\n              modal.exposed?.hide(trigger, true)\n            },\n          }\n        }\n      }\n    }\n    return null\n  }\n\n  const instance = getCurrentInstance()\n  const current = () => {\n    const findBModal = (component: ComponentInternalInstance): ComponentInternalInstance | null => {\n      if (!component.parent) {\n        return null\n      }\n      if (component.parent.type === BModal) {\n        return component.parent\n      }\n      return findBModal(component.parent)\n    }\n    if (!instance) {\n      return null\n    }\n    const modalComponent = computed(() => findBModal(instance))\n    const modal = computed(() => modalComponent.value?.proxy)\n    return {\n      show() {\n        modalComponent.value?.exposed?.show()\n      },\n      hide(trigger?: string) {\n        modalComponent.value?.exposed?.hide(trigger, true)\n      },\n      modal,\n    }\n  }\n\n  return {\n    show,\n    hide,\n    hideAll,\n    get,\n    current,\n    create,\n    _isOrchestratorInstalled,\n    store,\n    // Todo: Supports listening events globally in the future\n  }\n}\n\n/**\n * @deprecated use useModal() instead.\n * @returns {ReturnType<typeof useModal>} The modal controller\n */\nexport const useModalController = useModal\n"],"mappings":";;;;;;AA6BA,IAAa,iBAAiB;CAC5B,MAAM,uBAAuB,OAAO,yBAAyB,KAAK;AAClE,KAAI,CAAC,qBACH,OAAM,MACJ,wGACD;CAEH,MAAM,EAAC,OAAO,6BAA4B;;;;CAK1C,MAAM,UACJ,MAAoD,EAAE,EACtD,UAAqC,EAAE,KACyC;AAChF,MAAI,CAAC,yBAAyB,MAC5B,OAAM,IAAI,MAAM,6DAA6D;EAG/E,MAAM,gBAAiB,MAAM,IAAI,GAAG,MAAM,WAAW,IAAI;EAGzD,MAAM,QAAQ,cAAc,OAAO,MAAM,OAAO,oBAAoB;EAEpE,MAAM,UAAU,aAId,OAAO,MAA4C;AAErD,UAAQ,OAAO,MACb,gBACC,cAAc;GACb,MAAM,WAAW,EAAC,GAAG,QAAQ,UAAU,EAAC;GACxC,MAAM,gBAAgB,MAAM,MAAM,WAAW,OAAO,GAAG,UAAU,MAAM;GAGvE,MAAM,IAAI;IACR,MAAM;IACN;IACA,UAAU;IACV,GALA,kBAAkB,KAAK,EAAC,YAAY,QAAQ,eAAO,EAAC,GAAG,MAAM,MAAM;IAMnE;IACA;IACD;AAED,QAAK,MAAM,OAAO,SAChB,KAAI,IAAI,WAAW,KAAK,CACtB,GAAE,OAA4C,SAAS;YAC9C,QAAQ,eAAe,SAAS,UACzC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