import type { App, Plugin } from 'vue'

import type { ModalFunc, ModalFuncProps } from './Modal'
import Modal from './Modal'
import confirm, { withConfirm, withError, withInfo, withSuccess, withWarn } from './confirm'
import destroyFns from './destroyFns'
import useModal from './useModal'

export type { ActionButtonProps } from '../_util/ActionButton'
export type { ModalFuncProps, ModalProps } from './Modal'

function modalWarn(props: ModalFuncProps) {
  return confirm(withWarn(props))
}
Modal.useModal = useModal
Modal.info = function infoFn(props: ModalFuncProps) {
  return confirm(withInfo(props))
}

Modal.success = function successFn(props: ModalFuncProps) {
  return confirm(withSuccess(props))
}

Modal.error = function errorFn(props: ModalFuncProps) {
  return confirm(withError(props))
}

Modal.warning = modalWarn

Modal.warn = modalWarn

Modal.confirm = function confirmFn(props: ModalFuncProps) {
  return confirm(withConfirm(props))
}

Modal.addDestroyFn = (fn: () => void) => {
  destroyFns.push(fn)
}

Modal.delDestroyFn = (fn: () => void) => {
  for (let i = destroyFns.length - 1; i >= 0; i--) {
    if (destroyFns[i] === fn) {
      destroyFns.splice(i, 1)
    }
  }
}

Modal.destroyAll = function destroyAllFn() {
  while (destroyFns.length) {
    const close = destroyFns.pop()
    if (close) {
      close()
    }
  }
}

/* istanbul ignore next */
Modal.install = function (app: App) {
  app.component(Modal.name, Modal)
  return app
}

export default Modal as typeof Modal &
  Plugin & {
    readonly info: ModalFunc

    readonly success: ModalFunc

    readonly error: ModalFunc

    readonly warn: ModalFunc

    readonly warning: ModalFunc

    readonly confirm: ModalFunc

    readonly addDestroyFn: (fn: () => void) => void

    readonly delDestroyFn: (fn: () => void) => void

    readonly destroyAll: () => void

    readonly useModal: typeof useModal
  }
