const AXIS_DECIMALS = 2
/** 与 formatAxisLabel 小数位一致，刻度步长不得小于此值，避免标签重复 */
const MIN_TICK_STEP = 10 ** -AXIS_DECIMALS

export interface YAxisRange {
    min: number
    max: number
    interval: number
}

export function roundAxisValue(val: number, decimals = AXIS_DECIMALS): number {
    if (!Number.isFinite(val)) return 0
    const factor = 10 ** decimals
    return Math.round(val * factor) / factor
}

/** Y 轴刻度：最多 2 位小数，末尾 0 去掉（如 1.00 → 1，0.10 → 0.1） */
export function formatAxisLabel(value: number): string {
    if (!Number.isFinite(value)) return '0'
    return String(Number(roundAxisValue(value).toFixed(AXIS_DECIMALS)))
}

/** 根据数据范围计算与显示精度对齐的刻度步长 */
function calcNiceTickInterval(range: number, maxTicks = 6): number {
    if (!Number.isFinite(range) || range <= 0) return MIN_TICK_STEP

    const raw = range / maxTicks
    const magnitude = 10 ** Math.floor(Math.log10(raw))
    const normalized = raw / magnitude

    let niceFactor: number
    if (normalized <= 1) niceFactor = 1
    else if (normalized <= 2) niceFactor = 2
    else if (normalized <= 5) niceFactor = 5
    else niceFactor = 10

    let interval = niceFactor * magnitude

    if (interval < MIN_TICK_STEP) {
        interval = MIN_TICK_STEP
    } else {
        interval = Math.ceil(interval / MIN_TICK_STEP) * MIN_TICK_STEP
    }

    return roundAxisValue(interval)
}

/** 将 min/max 对齐到刻度步长边界 */
function alignAxisToInterval(min: number, max: number, interval: number): { min: number; max: number } {
    const alignedMin = Math.floor(min / interval) * interval
    let alignedMax = Math.ceil(max / interval) * interval

    if (alignedMax <= alignedMin) {
        alignedMax = alignedMin + interval
    }

    return {
        min: roundAxisValue(alignedMin),
        max: roundAxisValue(alignedMax)
    }
}

function buildYAxisRange(axisMin: number, axisMax: number): YAxisRange {
    const interval = calcNiceTickInterval(axisMax - axisMin)
    const aligned = alignAxisToInterval(axisMin, axisMax, interval)
    return {
        ...aligned,
        interval
    }
}

/** 瞬间波形 Y 轴范围 */
export function calcShuYAxisRange(dataMin: number, dataMax: number): YAxisRange {
    const min = Number(dataMin)
    const max = Number(dataMax)
    if (!Number.isFinite(min) || !Number.isFinite(max)) {
        return buildYAxisRange(0, 1)
    }

    let axisMax = max * 1.1
    let axisMin = min > 0 ? min - min * 0.1 : min * 1.1

    if (axisMax <= axisMin) {
        const pad = Math.abs(max) * 0.1 || 0.01
        axisMax = max + pad
        axisMin = min - pad
    }

    return buildYAxisRange(axisMin, axisMax)
}

/** RMS 波形 Y 轴范围 */
export function calcRmsYAxisRange(dataMin: number, dataMax: number): YAxisRange {
    const min = Number(dataMin)
    const max = Number(dataMax)
    if (!Number.isFinite(min) || !Number.isFinite(max)) {
        return buildYAxisRange(0, 1)
    }

    let axisMax = max * 1.06 * 1.1
    let axisMin = min - min * 0.2

    if (axisMax <= axisMin) {
        const pad = Math.abs(max - min) * 0.1 || Math.abs(max) * 0.1 || 0.01
        axisMax = max + pad
        axisMin = min - pad
    }

    return buildYAxisRange(axisMin, axisMax)
}
