{"version":3,"file":"index.cjs","sources":["../../../components/notice/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\n\nimport Component from './notice.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient, isNull, isObject, 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  NoticeConfig,\n  NoticeInstance,\n  NoticeOptions,\n  NoticePlacement,\n  NoticeType,\n} from './symbol'\n\nexport type { NoticeType, NoticePlacement, NoticeOptions }\n\ntype FuzzyOptions = string | NoticeOptions\ntype ManagerOptions = { marker?: boolean, duration?: number, placement?: NoticePlacement } & Record<\n  string,\n  unknown\n>\n\ninterface AipMethod {\n  (options: NoticeOptions): () => void,\n  (title: string, duration?: number): () => void,\n  (title: string, content: string, duration?: number): () => void,\n  /** @internal */\n  (options: FuzzyOptions, duration?: number): () => void\n}\n\nconst placementWhiteList: NoticePlacement[] = [\n  'top-right',\n  'top-left',\n  'bottom-right',\n  'bottom-left',\n]\n\nlet count = 1\n\nfunction getKey() {\n  return `notice-${count++}`\n}\n\nexport class NoticeManager {\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: NoticeInstance | 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      marker: !!options.marker,\n      duration: options.duration ? toNumber(options.duration) : 4000,\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 = 'Notice'\n    this.defaults = {}\n\n    this.config(options)\n\n    this.open = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open(null, title, content, duration)\n    }\n\n    this.primary = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('primary', title, content, duration)\n    }\n\n    this.info = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('info', title, content, duration)\n    }\n\n    this.success = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('success', title, content, duration)\n    }\n\n    this.warning = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('warning', title, content, duration)\n    }\n\n    this.error = (title: FuzzyOptions, content?: string | number, duration?: number) => {\n      return this._open('error', title, content, duration)\n    }\n  }\n\n  judge(state: boolean, success: string, error: string, duration?: number): void\n  judge(state: boolean, success: NoticeOptions, error: string, duration?: number): void\n  judge(state: boolean, success: string, error: NoticeOptions, duration?: number): void\n  judge(state: boolean, success: NoticeOptions, error: NoticeOptions): void\n  judge(\n    state: boolean,\n    success: string | NoticeOptions,\n    error: string | NoticeOptions,\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 }: NoticeConfig & NoticeOptions) {\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 NoticeManager(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.$notice) {\n      app.config.globalProperties[property || '$notice'] = 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:Notice]: 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 NoticeInstance\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<NoticeInstance>(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(\n    type: null | NoticeType,\n    title: FuzzyOptions,\n    content?: string | number,\n    _duration?: number,\n  ) {\n    if (!isClient) {\n      return noop\n    }\n\n    let options: NoticeOptions\n\n    if (isObject(title)) {\n      options = title\n    } else {\n      if (typeof content === 'number') {\n        options = { title, duration: content }\n      } else if (!content) {\n        options = { title, duration: _duration }\n      } else {\n        options = { title, content, duration: _duration }\n      }\n    }\n\n    const key = options.key ?? getKey()\n    const notice = 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: NoticeOptions = {\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    notice.add(item)\n    setDelayClose()\n\n    function setDelayClose() {\n      const duration = typeof item.duration === 'number' ? item.duration : 4000\n\n      if (duration >= 500) {\n        timer = setTimeout(() => {\n          notice.remove(key)\n        }, duration)\n      }\n    }\n\n    return () => {\n      clearTimeout(timer)\n      notice.remove(key)\n    }\n  }\n}\n\nexport const Notice = new NoticeManager()\n"],"names":["placementWhiteList","count","getKey","NoticeManager","options","__publicField","toNumber","title","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","isObject","notice","timer","userCloseFn","onClose","userEnterFn","onEnter","userLeaveFn","onLeave","setDelayClose","item","markRaw","Notice"],"mappings":"sYAiCMA,EAAwC,CAC5C,YACA,WACA,eACA,aACF,EAEA,IAAIC,EAAQ,EAEZ,SAASC,GAAS,CAChB,MAAO,UAAUD,GAAO,EAC1B,CAEO,MAAME,CAAc,CAkBzB,YAAYC,EAA0B,GAAI,CAjB1CC,EAAA,aACAA,EAAA,iBAEAA,EAAA,aACAA,EAAA,gBACAA,EAAA,aACAA,EAAA,gBACAA,EAAA,gBACAA,EAAA,cAEQA,EAAA,oBACAA,EAAA,kBACAA,EAAA,kBACAA,EAAA,mBACAA,EAAA,iBACAA,EAAA,mBAGID,EAAA,CACR,GAAGA,EACH,OAAQ,CAAC,CAACA,EAAQ,OAClB,SAAUA,EAAQ,SAAWE,EAAS,SAAAF,EAAQ,QAAQ,EAAI,GAC5D,EAEA,KAAK,YAAc,KACnB,KAAK,UAAY,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