import { Modal } from '@arco-design/web-vue'

const MOBILE_BREAKPOINT = 768
const MOBILE_HORIZONTAL_GAP = 24
const MOBILE_MIN_WIDTH = 280

function getResponsiveModalWidth() {
  if (typeof window === 'undefined') return undefined
  if (window.innerWidth > MOBILE_BREAKPOINT) return undefined
  return Math.max(MOBILE_MIN_WIDTH, window.innerWidth - MOBILE_HORIZONTAL_GAP)
}

function withResponsiveWidth(options = {}) {
  const width = getResponsiveModalWidth()
  if (width === undefined) return options
  return { ...options, width }
}

const MODAL_METHODS = ['confirm', 'info', 'success', 'warning', 'error', 'open']

/** 移动端为 Modal 静态方法注入适配宽度，避免 confirm 超出屏幕 */
export function setupResponsiveModal() {
  MODAL_METHODS.forEach((method) => {
    const original = Modal[method]
    if (typeof original !== 'function') return
    Modal[method] = (options) => original.call(Modal, withResponsiveWidth(options))
  })
}
