/**
 * 系统弹框尺寸配置（大 / 中 / 小）
 * 修改此处即可全局统一弹框宽度与默认行为
 */
export interface DialogPreset {
    width: string
    maxWidth: string
    minWidth: string
    closeOnClickModal: boolean
    draggable: boolean
    class: string
}

export const dialogSmall: DialogPreset = {
    width: '26vw',
    maxWidth: '500px',
    minWidth: '300px',
    closeOnClickModal: false,
    draggable: true,
    class: 'dialog-small'
}

export const dialogMiddle: DialogPreset = {
    width: '42vw',
    maxWidth: '800px',
    minWidth: '600px',
    closeOnClickModal: false,
    draggable: true,
    class: 'dialog-middle'
}

export const dialogBig: DialogPreset = {
    width: '62vw',
    maxWidth: '1200px',
    minWidth: '800px',
    closeOnClickModal: false,
    draggable: true,
    class: 'dialog-big'
}

export const DIALOG_PRESETS = {
    small: dialogSmall,
    middle: dialogMiddle,
    big: dialogBig
} as const

export type DialogSize = keyof typeof DIALOG_PRESETS

export function getDialogPreset(size: DialogSize = 'small'): DialogPreset {
    return DIALOG_PRESETS[size]
}

export function getDialogWidth(size: DialogSize = 'small'): string {
    return DIALOG_PRESETS[size].width
}

/** 将配置同步到 CSS 变量，供全局样式使用 */
export function applyDialogCssVars(): void {
    ;(Object.keys(DIALOG_PRESETS) as DialogSize[]).forEach(size => {
        const preset = DIALOG_PRESETS[size]
        document.documentElement.style.setProperty(`--cn-dialog-width-${size}`, preset.width)
        document.documentElement.style.setProperty(`--cn-dialog-max-width-${size}`, preset.maxWidth)
        document.documentElement.style.setProperty(`--cn-dialog-min-width-${size}`, preset.minWidth)
    })
}

/**
 * 根据原有 width 推断弹框尺寸（兼容现有 el-dialog 的 width 写法）
 */
export function resolveDialogSize(width: string | number): DialogSize {
    const raw = String(width).trim().toLowerCase()

    if (raw.endsWith('%')) {
        const percent = parseFloat(raw)
        if (Number.isNaN(percent)) return 'small'
        if (percent >= 60) return 'big'
        if (percent >= 40) return 'middle'
        return 'small'
    }

    if (raw.endsWith('vw')) {
        const vw = parseFloat(raw)
        if (Number.isNaN(vw)) return 'middle'
        if (vw >= 55) return 'big'
        if (vw >= 35) return 'middle'
        return 'small'
    }

    const numeric = parseFloat(raw.replace(/px/g, ''))
    if (Number.isNaN(numeric)) return 'small'
    if (numeric >= 850) return 'big'
    if (numeric >= 550) return 'middle'
    return 'small'
}

/** 无 width 时根据 class 推断尺寸 */
export function resolveDialogSizeByClass(classValue: unknown): DialogSize {
    const classStr = normalizeClass(classValue)

    if (classStr.includes('dialog-big')) return 'big'
    if (classStr.includes('dialog-middle')) return 'middle'
    if (classStr.includes('dialog-small')) return 'small'

    if (classStr.includes('transient-filter-dialog')) return 'small'
    if (classStr.includes('cn-operate-dialog')) return 'small'

    if (classStr.includes('device-manage-popup') || classStr.includes('analysisList')) {
        return 'big'
    }

    return 'middle'
}

function normalizeClass(classValue: unknown): string {
    if (!classValue) return ''
    if (typeof classValue === 'string') return classValue
    if (Array.isArray(classValue)) return classValue.filter(Boolean).join(' ')
    if (typeof classValue === 'object') {
        return Object.entries(classValue as Record<string, boolean>)
            .filter(([, active]) => active)
            .map(([name]) => name)
            .join(' ')
    }
    return String(classValue)
}
