{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/orchestratorShared/index.ts"],"sourcesContent":["import {\n  type ComponentPublicInstance,\n  inject,\n  nextTick,\n  provide,\n  type Ref,\n  ref,\n  toValue,\n  type WatchHandle,\n} from 'vue'\nimport type {\n  ControllerKey,\n  OrchestratorArrayValue,\n  PromiseWithComponent,\n  PromiseWithComponentInternal,\n  ShowPromiseWithComponent,\n} from '../../types/ComponentOrchestratorTypes'\nimport type {BvTriggerableEvent} from '../../utils'\nimport {orchestratorRegistryKey} from '../../utils/keys'\n\nexport function buildPromise<TComponent, TParam, TArrayValue extends OrchestratorArrayValue>(\n  _id: ControllerKey,\n  store: Ref<TArrayValue[]>\n): {\n  value: PromiseWithComponent<TComponent, TParam>\n  resolve: (value: BvTriggerableEvent) => void\n  stop: WatchHandle | undefined\n} {\n  let resolveFunc: (value: BvTriggerableEvent) => void = () => {}\n\n  const promise = new Promise<BvTriggerableEvent>((resolve) => {\n    resolveFunc = resolve\n  }) as PromiseWithComponent<TComponent, TParam>\n\n  type RefWithMethods = ComponentPublicInstance<TComponent> & {\n    show?: () => void\n    hide?: (trigger?: string, noEmit?: boolean) => void\n    toggle?: () => void\n  }\n  Object.assign(promise, {\n    id: _id,\n    ref: null as ComponentPublicInstance<TComponent> | null,\n    show() {\n      const refWithMethods = this.ref as RefWithMethods | null\n      if (refWithMethods?.show) {\n        refWithMethods.show()\n      } else {\n        this.set({modelValue: true} as unknown as Partial<TParam>)\n      }\n      const self = this as PromiseWithComponentInternal<TComponent, TParam>\n      const asyncDispose = async () => {\n        await self.destroy()\n      }\n      const showPromise = promise.then((event) =>\n        Object.assign(Object.create(event), {\n          [Symbol.asyncDispose]: asyncDispose,\n        })\n      )\n      return Object.assign(showPromise, {\n        id: self.id,\n        ref: self.ref,\n        show: () => self.show(),\n        hide: (trigger?: string) => self.hide(trigger),\n        toggle: () => self.toggle(),\n        set: (val: Partial<TParam>) => self.set(val),\n        get: () => self.get(),\n        destroy: () => self.destroy(),\n        [Symbol.asyncDispose]: asyncDispose,\n      }) as ShowPromiseWithComponent<TComponent, TParam>\n    },\n    hide(trigger?: string) {\n      const refWithMethods = this.ref as RefWithMethods | null\n      if (refWithMethods?.hide) {\n        refWithMethods.hide(trigger, true)\n      } else return this.set({modelValue: false} as unknown as Partial<TParam>)\n      return promise\n    },\n    toggle() {\n      const currentItem = this.get() as TParam & {modelValue?: unknown}\n      const refWithMethods = this.ref as RefWithMethods | null\n      if (refWithMethods?.toggle) {\n        refWithMethods.toggle()\n      } else return this.set({modelValue: !currentItem?.modelValue} as unknown as Partial<TParam>)\n      return promise\n    },\n    get(): TParam | undefined {\n      return store.value.find((el) => el._self === _id) as TParam | undefined\n    },\n    set(val: Partial<TParam>) {\n      const itemIndex = store.value.findIndex((el) => el._self === _id)\n      const item = store.value[itemIndex]\n      if (item) {\n        const v = {...toValue(item), ...toValue(val)}\n        if (item.modelValue !== v.modelValue) {\n          item['onUpdate:modelValue']?.(v.modelValue as boolean)\n        }\n        const updatedItem = {\n          ...v,\n          modelValue: toValue(v.modelValue),\n        } as TArrayValue\n        // Only set title and body if they are defined in v\n        const vRecord = v as Record<string, unknown>\n        if ('title' in vRecord && vRecord.title !== undefined) {\n          ;(updatedItem as Record<string, unknown>).title = toValue(vRecord.title)\n        }\n        if ('body' in vRecord && vRecord.body !== undefined) {\n          ;(updatedItem as Record<string, unknown>).body = toValue(vRecord.body)\n        }\n        store.value.splice(itemIndex, 1, updatedItem)\n      }\n      return promise\n    },\n    async destroy() {\n      const item = store.value.find((el) => el._self === _id)\n      if (!item) return\n      item.promise.stop?.()\n      if (item.modelValue) {\n        await new Promise((resolve) => {\n          const prev = item['onHidden']\n          item['onHidden'] = (e) => {\n            prev?.(e)\n            resolve(e)\n          }\n          nextTick(() => {\n            this.hide('destroy')\n          })\n        })\n      }\n      store.value.splice(\n        store.value.findIndex((el) => el._self === _id),\n        1\n      )\n    },\n    async [Symbol.asyncDispose]() {\n      await this.destroy()\n    },\n  } as PromiseWithComponentInternal<TComponent, TParam>)\n\n  return {\n    value: promise,\n    resolve: resolveFunc,\n    stop: undefined as WatchHandle | undefined,\n  }\n}\n\nexport const _newOrchestratorRegistry = (): {\n  store: Ref<OrchestratorArrayValue[]>\n  _isOrchestratorInstalled: Ref<boolean>\n  _isToastAppend: Ref<boolean>\n} => ({\n  store: ref<OrchestratorArrayValue[]>([]),\n  _isOrchestratorInstalled: ref(false),\n  _isToastAppend: ref(false),\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"],"mappings":";;;AAoBA,SAAgB,aACd,KACA,OAKA;CACA,IAAI,oBAAyD;CAE7D,MAAM,UAAU,IAAI,SAA6B,YAAY;AAC3D,gBAAc;GACd;AAOF,QAAO,OAAO,SAAS;EACrB,IAAI;EACJ,KAAK;EACL,OAAO;GACL,MAAM,iBAAiB,KAAK;AAC5B,OAAI,gBAAgB,KAClB,gBAAe,MAAM;OAErB,MAAK,IAAI,EAAC,YAAY,MAAK,CAA+B;GAE5D,MAAM,OAAO;GACb,MAAM,eAAe,YAAY;AAC/B,UAAM,KAAK,SAAS;;GAEtB,MAAM,cAAc,QAAQ,MAAM,UAChC,OAAO,OAAO,OAAO,OAAO,MAAM,EAAE,GACjC,OAAO,eAAe,cACxB,CAAC,CACH;AACD,UAAO,OAAO,OAAO,aAAa;IAChC,IAAI,KAAK;IACT,KAAK,KAAK;IACV,YAAY,KAAK,MAAM;IACvB,OAAO,YAAqB,KAAK,KAAK,QAAQ;IAC9C,cAAc,KAAK,QAAQ;IAC3B,MAAM,QAAyB,KAAK,IAAI,IAAI;IAC5C,WAAW,KAAK,KAAK;IACrB,eAAe,KAAK,SAAS;KAC5B,OAAO,eAAe;IACxB,CAAC;;EAEJ,K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