{"version":3,"file":"index.mjs","sources":["../../../components/message/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\n\nimport Component from './message.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient, isNull, noop, toNumber } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type {\n  Key,\n  MessageConfig,\n  MessageInstance,\n  MessageOptions,\n  MessagePlacement,\n  MessageType\n} from './symbol'\n\nexport type { MessageConfig, MessageType, MessagePlacement, MessageOptions }\n\ntype FuzzyOptions = string | MessageOptions\ntype ManagerOptions = { duration?: number, placement?: MessagePlacement } & Record<string, unknown>\n\ninterface AipMethod {\n  (options: MessageOptions): () => void,\n  (content: string, duration?: number): () => void,\n  /** @internal */\n  (options: FuzzyOptions, duration?: number): () => void\n}\n\nconst placementWhiteList: MessagePlacement[] = ['top', 'bottom']\n\nlet count = 1\n\nfunction getKey() {\n  return `message-${count++}`\n}\n\nexport class MessageManager {\n  name: string\n  defaults: Record<string, unknown>\n\n  open: AipMethod\n  primary: AipMethod\n  info: AipMethod\n  success: AipMethod\n  warning: AipMethod\n  error: AipMethod\n\n  private _mountedApp: App<unknown> | null\n  private _instance: MessageInstance | null\n  private _innerApp: App<unknown> | null\n  private _container: HTMLElement | null\n  private _wrapper: HTMLElement | SVGElement | null\n  private _mountedEl: HTMLElement | null\n\n  constructor(options: ManagerOptions = {}) {\n    options = {\n      ...options,\n      duration: options.duration ? toNumber(options.duration) : 3000\n    }\n\n    this._mountedApp = null\n    this._instance = null\n    this._innerApp = null\n    this._container = null\n    this._wrapper = null\n    this._mountedEl = null\n    this.name = 'Message'\n    this.defaults = {}\n\n    this.config(options)\n\n    this.open = (content: FuzzyOptions, duration?: number) => {\n      return this._open(null, content, duration)\n    }\n\n    this.primary = (content: FuzzyOptions, duration?: number) => {\n      return this._open('primary', content, duration)\n    }\n\n    this.info = (content: FuzzyOptions, duration?: number) => {\n      return this._open('info', content, duration)\n    }\n\n    this.success = (content: FuzzyOptions, duration?: number) => {\n      return this._open('success', content, duration)\n    }\n\n    this.warning = (content: FuzzyOptions, duration?: number) => {\n      return this._open('warning', content, duration)\n    }\n\n    this.error = (content: FuzzyOptions, duration?: number) => {\n      return this._open('error', content, duration)\n    }\n  }\n\n  judge(state: boolean, success: string, error: string, duration?: number): void\n  judge(state: boolean, success: MessageOptions, error: string, duration?: number): void\n  judge(state: boolean, success: string, error: MessageOptions, duration?: number): void\n  judge(state: boolean, success: MessageOptions, error: MessageOptions): void\n  judge(\n    state: boolean,\n    success: string | MessageOptions,\n    error: string | MessageOptions,\n    duration?: number\n  ) {\n    if (state) {\n      this.success(success, duration)\n    } else {\n      this.error(error, duration)\n    }\n  }\n\n  close(key: Key) {\n    if (isNull(key)) {\n      this.clear()\n    } else {\n      this._getInstance()?.remove(key)\n    }\n  }\n\n  config({ placement, ...others }: MessageConfig & MessageOptions) {\n    if (placement) {\n      this._getInstance()?.config({\n        placement: placementWhiteList.includes(placement) ? placement : placementWhiteList[0]\n      })\n    }\n\n    this.defaults = { ...this.defaults, ...others }\n  }\n\n  clone() {\n    const manager = new MessageManager(this.defaults)\n\n    manager._mountedApp = this._mountedApp\n\n    return manager\n  }\n\n  clear() {\n    this._getInstance()?.clear()\n  }\n\n  destroy() {\n    this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\n    this._innerApp?.unmount()\n    this._container && render(null, this._container)\n    destroyObject(this)\n  }\n\n  isDestroyed() {\n    return false\n  }\n\n  install(app: App, options: ManagerOptions & { property?: string } = {}) {\n    const { property, ...others } = options\n\n    this.config(others)\n    this._mountedApp = app\n\n    if (property || !app.config.globalProperties.$message) {\n      app.config.globalProperties[property || '$message'] = this\n    }\n  }\n\n  transferTo(target: MaybeRef<string | MaybeInstance>) {\n    if (!isClient) return\n\n    const el = unrefElement(target)\n\n    if (el) {\n      this._wrapper = el\n\n      if (this._instance) {\n        this._mountedEl && this._wrapper.appendChild(this._mountedEl)\n      } else {\n        this._getInstance()\n      }\n    }\n  }\n\n  private _getInstance() {\n    if (!this._instance && isClient) {\n      if (!this._mountedApp) {\n        console.warn('[vexip-ui:Message]: App missing, the plugin maybe not installed.')\n\n        this._container = document.createElement('div')\n        this._innerApp = createApp(Component)\n        this._instance = this._innerApp.mount(this._container) as MessageInstance\n      } else {\n        const vnode = createVNode(Component, null, null)\n\n        this._container = document.createElement('div')\n        vnode.appContext = this._mountedApp._context\n\n        render(vnode, this._container)\n\n        this._instance = proxyExposed<MessageInstance>(vnode)\n      }\n\n      this._mountedEl = this._container.firstElementChild as HTMLElement\n      ;(this._wrapper || document.body).appendChild(this._mountedEl)\n    }\n\n    return this._instance\n  }\n\n  private _open(type: null | MessageType, content: FuzzyOptions, _duration?: number) {\n    if (!isClient) {\n      return noop\n    }\n\n    const options = typeof content === 'string' ? { content, duration: _duration } : content\n\n    const key = options.key ?? getKey()\n    const message = this._getInstance()!\n\n    let timer: ReturnType<typeof setTimeout>\n\n    const userCloseFn = options.onClose\n    const onClose = () => {\n      clearTimeout(timer)\n\n      if (typeof userCloseFn === 'function') {\n        return userCloseFn()\n      }\n    }\n\n    const userEnterFn = options.onEnter\n    const onEnter = () => {\n      if (options.liveOnEnter) {\n        clearTimeout(timer)\n      }\n\n      if (typeof userEnterFn === 'function') {\n        return userEnterFn()\n      }\n    }\n\n    const userLeaveFn = options.onLeave\n    const onLeave = () => {\n      if (options.liveOnEnter) {\n        clearTimeout(timer)\n        setDelayClose()\n      }\n\n      if (typeof userLeaveFn === 'function') {\n        return userLeaveFn()\n      }\n    }\n\n    const item: MessageOptions = {\n      ...this.defaults,\n      ...options,\n      key,\n      type: type ?? options.type,\n      onClose,\n      onEnter,\n      onLeave\n    }\n\n    if (item.icon && typeof item.icon !== 'function') {\n      item.icon = markRaw(item.icon)\n    }\n\n    message.add(item)\n    setDelayClose()\n\n    function setDelayClose() {\n      const duration = typeof item.duration === 'number' ? item.duration : 3000\n\n      if (duration >= 500) {\n        timer = setTimeout(() => {\n          message.remove(key)\n        }, duration)\n      }\n    }\n\n    return () => {\n      clearTimeout(timer)\n      message.remove(key)\n    }\n  }\n}\n\nexport const Message = new MessageManager()\n"],"names":["placementWhiteList","count","getKey","MessageManager","options","__publicField","toNumber","content","duration","state","success","error","key","isNull","_a","placement","others","manager","_b","render","destroyObject","app","property","target","isClient","el","unrefElement","createApp","Component","vnode","createVNode","proxyExposed","type","_duration","noop","message","timer","userCloseFn","onClose","userEnterFn","onEnter","userLeaveFn","onLeave","setDelayClose","item","markRaw","Message"],"mappings":";;;;;;;;AA6BA,MAAMA,IAAyC,CAAC,OAAO,QAAQ;AAE/D,IAAIC,IAAQ;AAEZ,SAASC,IAAS;AAChB,SAAO,WAAWD,GAAO;AAC3B;AAEO,MAAME,EAAe;AAAA,EAkB1B,YAAYC,IAA0B,IAAI;AAjB1C,IAAAC,EAAA;AACA,IAAAA,EAAA;AAEA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAEQ,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAGI,IAAAD,IAAA;AAAA,MACR,GAAGA;AAAA,MACH,UAAUA,EAAQ,WAAWE,EAASF,EAAQ,QAAQ,IAAI;AAAA,IAC5D,GAEA,KAAK,cAAc,MACnB,KAAK,YAAY,MACjB,KAAK,YAAY,MACjB,KAAK,aAAa,MAClB,KAAK,WAAW,MAChB,KAAK,aAAa,MAClB,KAAK,OAAO,WACZ,KAAK,WAAW,CAAC,GAEjB,KAAK,OAAOA,CAAO,GAEd,KAAA,OAAO,CAACG,GAAuBC,MAC3B,KAAK,MAAM,MAAMD,GAASC,CAAQ,GAGtC,KAAA,UAAU,CAACD,GAAuBC,MAC9B,KAAK,MAAM,WAAWD,GAASC,CAAQ,GAG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