import { ElMessage, ElMessageBox } from 'element-plus'
import type { ElMessageBoxOptions, MessageOptions } from 'element-plus'

import 'element-plus/theme-chalk/el-message.css'
import 'element-plus/theme-chalk/el-overlay.css'
import 'element-plus/theme-chalk/el-message-box.css'

const baseMessageOptions: Partial<MessageOptions> = {
  grouping: true,
}

const baseConfirmOptions: ElMessageBoxOptions = {
  type: 'warning',
  confirmButtonText: '继续执行',
  cancelButtonText: '取消',
  closeOnClickModal: false,
  closeOnPressEscape: false,
  autofocus: false,
  distinguishCancelAndClose: true,
}

const basePromptOptions: ElMessageBoxOptions = {
  confirmButtonText: '提交',
  cancelButtonText: '取消',
  closeOnClickModal: false,
  closeOnPressEscape: false,
  autofocus: false,
  distinguishCancelAndClose: true,
}

export function showSuccess(message: string, options: Partial<MessageOptions> = {}) {
  return ElMessage.success({
    ...baseMessageOptions,
    ...options,
    message,
  })
}

export function showError(message: string, options: Partial<MessageOptions> = {}) {
  return ElMessage.error({
    ...baseMessageOptions,
    ...options,
    message,
  })
}

export function showConfirm(
  message: string,
  title = '确认操作',
  options: ElMessageBoxOptions = {},
) {
  return ElMessageBox.confirm(message, title, {
    ...baseConfirmOptions,
    ...options,
  })
}

export function showPrompt(message: string, title: string, options: ElMessageBoxOptions = {}) {
  return ElMessageBox.prompt(message, title, {
    ...basePromptOptions,
    ...options,
  })
}

export function isFeedbackDismissed(error: unknown) {
  return error === 'cancel' || error === 'close'
}
