{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/orchestratorShared/index.ts"],"sourcesContent":["import {type ComponentPublicInstance, inject, nextTick, provide, readonly, type Ref, ref} from 'vue'\nimport type {ComponentController, ControllerKey, PromiseWithController} from '../../types'\nimport type {BvTriggerableEvent} from '../../utils'\nimport {orchestratorRegistryKey, type OrchestratorStoreObject} from '../../utils/keys'\n\n/* oxlint-disable-next-line no-explicit-any */\n/* eslint-disable-next-line @typescript-eslint/no-explicit-any */\nexport type ValueInMapRecord<MapRecord> = MapRecord extends Map<any, infer I> ? I : never\n\nexport const buildController = <\n  TComponent,\n  TStore extends Readonly<Ref<OrchestratorStoreObject[keyof OrchestratorStoreObject]>>,\n>(\n  id: ControllerKey,\n  store: TStore\n): PromiseWithController<TComponent, ValueInMapRecord<TStore['value']>> => {\n  let resolveFunc: (value: BvTriggerableEvent) => void = () => {}\n\n  const basePromise = new Promise<BvTriggerableEvent>((resolve) => {\n    resolveFunc = resolve\n  })\n\n  type RefWithMethods = ComponentPublicInstance<TComponent> & {\n    show?: () => void\n    hide?: (trigger?: string, noEmit?: boolean) => void\n    toggle?: () => void\n  }\n  const findItem = () => store.value.get(id)\n\n  const controller = {\n    id,\n    ref: null as RefWithMethods | null,\n    async show() {\n      const currentModelValue = controller.get()?.value.props.modelValue\n      if (currentModelValue) {\n        const event = await basePromise\n        return Object.assign(Object.create(event), {\n          [Symbol.asyncDispose]: controller.destroy,\n        })\n      }\n      const refWithMethods = controller.ref\n      if (\n        refWithMethods !== null &&\n        'show' in refWithMethods &&\n        typeof refWithMethods.show === 'function'\n      ) {\n        refWithMethods.show()\n      } else {\n        controller.set({modelValue: true})\n      }\n      const event = await basePromise\n      return Object.assign(Object.create(event), {\n        [Symbol.asyncDispose]: controller.destroy,\n      })\n    },\n    hide(trigger?: string) {\n      const refWithMethods = controller.ref\n      if (\n        refWithMethods !== null &&\n        'hide' in refWithMethods &&\n        typeof refWithMethods.hide === 'function'\n      ) {\n        refWithMethods.hide(trigger, true)\n      } else {\n        controller.set({modelValue: false})\n      }\n    },\n    toggle() {\n      const currentItem = controller.get()\n      const refWithMethods = controller.ref\n      if (\n        refWithMethods !== null &&\n        'toggle' in refWithMethods &&\n        typeof refWithMethods.toggle === 'function'\n      ) {\n        refWithMethods.toggle()\n      } else {\n        controller.set({modelValue: !currentItem?.value.props.modelValue})\n      }\n    },\n    get: findItem,\n    set(val: Partial<ValueInMapRecord<TStore['value']>['value']['props']>) {\n      const current = findItem()\n      if (current?.value) {\n        current.value = {\n          ...current.value,\n          props: {\n            ...current.value.props,\n            ...val,\n            /* oxlint-disable no-explicit-any */\n            /* eslint-disable @typescript-eslint/no-explicit-any */\n          } as any,\n        }\n      }\n    },\n    async destroy() {\n      const item = findItem()\n      if (!item) return\n      try {\n        if (item.value.props.modelValue) {\n          await basePromise\n          await nextTick()\n          controller.hide('destroy')\n        }\n      } finally {\n        store.value.delete(id)\n      }\n    },\n    [Symbol.asyncDispose]() {\n      return controller.destroy()\n    },\n  } as ComponentController<TComponent, ValueInMapRecord<TStore['value']>>\n\n  return {\n    controller,\n    resolve: resolveFunc,\n  }\n}\n\nexport const _newOrchestratorRegistry = (): {\n  store: Readonly<Ref<OrchestratorStoreObject>>\n  _isToastAppend: Readonly<Ref<boolean>>\n  _setToastAppend: (value: boolean) => void\n  _isOrchestratorInstalled: Readonly<Ref<boolean>>\n  _setOrchestratorInstalled: (value: boolean) => void\n} => {\n  const _isOrchestratorInstalled = ref(false)\n  const _isToastAppend = ref(false)\n\n  return {\n    store: ref({\n      modal: new Map(),\n      tooltip: new Map(),\n      popover: new Map(),\n      toast: new Map(),\n    }),\n    _isOrchestratorInstalled: readonly(_isOrchestratorInstalled),\n    _setOrchestratorInstalled: (v) => {\n      _isOrchestratorInstalled.value = v\n    },\n    _isToastAppend: readonly(_isToastAppend),\n    _setToastAppend: (v) => {\n      _isToastAppend.value = v\n    },\n  }\n}\n\nexport const useOrchestratorRegistry = () => {\n  const orchestratorRegistry = inject(orchestratorRegistryKey, null)\n\n  if (orchestratorRegistry) {\n    return orchestratorRegistry\n  }\n  const newOrchestratorRegistry = _newOrchestratorRegistry()\n  provide(orchestratorRegistryKey, newOrchestratorRegistry)\n  return newOrchestratorRegistry\n}\n\nexport const getOrchestratorControllerId = (propId: string | undefined) => {\n  const id: ControllerKey = propId || Symbol('OrchestratorItem')\n\n  return {\n    /**\n     * This id is used for stability in the store, using the createIdWatcher will update the store if the id ever changes\n     */\n    storeId: id,\n    /**\n     * Do not pass in the symbol as a prop. Html attribute cannot be a symbol. Thus it's internal\n     */\n    htmlAttributeId: typeof id === 'string' ? id : undefined,\n  }\n}\n"],"mappings":";;;AASA,IAAa,mBAIX,IACA,UACyE;CACzE,IAAI,oBAAyD,CAAC;CAE9D,MAAM,cAAc,IAAI,SAA6B,YAAY;EAC/D,cAAc;CAChB,CAAC;CAOD,MAAM,iBAAiB,MAAM,MAAM,IAAI,EAAE;CAEzC,MAAM,aAAa;EACjB;EACA,KAAK;EACL,MAAM,OAAO;GAEX,IAD0B,WAAW,IAAI,CAAC,EAAE,MAAM,MAAM,YACjC;IACrB,MAAM,QAAQ,MAAM;IACpB,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG,GACxC,OAAO,eAAe,WAAW,QACpC,CAAC;GACH;GACA,MAAM,iBAAiB,WAAW;GAClC,IACE,mBAAmB,QACnB,UAAU,kBACV,OAAO,eAAe,SAAS,YAE/B,eAAe,KAAK;QAEpB,WAAW,IAAI,EAAC,YAAY,KAAI,CAAC;GAEnC,MAAM,QAAQ,MAAM;GACpB,OAAO,OAAO,OAAO,OAAO,OAAO,KAAK,GAAG,GACxC,OAAO,eAAe,WAAW,QACpC,CAAC;EACH;EACA,KAAK,SAAkB;GACrB,MAAM,iBAAiB,WAAW;GAClC,IACE,mBAAmB,QACnB,UAAU,kBACV,OAAO,eAAe,SAAS,YAE/B,eAAe,KAAK,SAAS,IAAI;QAEjC,WAAW,IAAI,EAAC,YAAY,MAAK,CAAC;EAE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