"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get animateScrollTo () {
        return animateScrollTo;
    },
    get buildTimeString () {
        return buildTimeString;
    },
    get clampTimeToMax () {
        return clampTimeToMax;
    },
    get clampTimeToMin () {
        return clampTimeToMin;
    },
    get delimiter () {
        return delimiter;
    },
    get getColumnsFromFormat () {
        return getColumnsFromFormat;
    },
    get isTimeDisabled () {
        return isTimeDisabled;
    },
    get parseTimeBoundary () {
        return parseTimeBoundary;
    },
    get parseTimeString () {
        return parseTimeString;
    },
    get range () {
        return range;
    },
    get roundToMultiplicity () {
        return roundToMultiplicity;
    },
    get toTotalSeconds () {
        return toTotalSeconds;
    }
});
const getColumnsFromFormat = (format, multiplicityMinutes, multiplicitySeconds)=>{
    const parts = format.split(':');
    const columns = [];
    parts.forEach((part)=>{
        switch(part){
            case 'HH':
                columns.push({
                    type: 'hours',
                    values: range(24),
                    format: 'HH'
                });
                break;
            case 'mm':
                columns.push({
                    type: 'minutes',
                    values: range(60, 2, multiplicityMinutes ?? 1),
                    format: 'mm'
                });
                break;
            case 'ss':
                columns.push({
                    type: 'seconds',
                    values: range(60, 2, multiplicitySeconds ?? 1),
                    format: 'ss'
                });
                break;
            default:
        }
    });
    return columns;
};
const parseTimeString = (timeString, format)=>{
    const timeParts = timeString.split(':');
    const formatParts = format.split(':');
    const result = {
        hh: null,
        mm: null,
        ss: null
    };
    formatParts.forEach((part, index)=>{
        const value = timeParts[index] ? parseInt(timeParts[index], 10) : null;
        switch(part){
            case 'HH':
                result.hh = value;
                break;
            case 'mm':
                result.mm = value;
                break;
            case 'ss':
                result.ss = value;
                break;
            default:
        }
    });
    return result;
};
const buildTimeString = (timeValues, format)=>{
    const formatParts = format.split(':');
    return formatParts.map((part)=>{
        switch(part){
            case 'HH':
                return timeValues.hh !== null ? timeValues.hh.toString().padStart(2, '0') : '00';
            case 'mm':
                return timeValues.mm !== null ? timeValues.mm.toString().padStart(2, '0') : '00';
            case 'ss':
                return timeValues.ss !== null ? timeValues.ss.toString().padStart(2, '0') : '00';
            default:
                return '00';
        }
    }).join(':');
};
const isTimeDisabled = (timeValues, min, max, format)=>{
    if (!min && !max) return false;
    if (timeValues.hh === null || timeValues.mm === null || timeValues.ss === null) {
        return false;
    }
    const totalSeconds = timeValues.hh * 3600 + timeValues.mm * 60 + timeValues.ss;
    let minSeconds = 0;
    let maxSeconds = 23 * 3600 + 59 * 60 + 59;
    if (min) {
        if (min instanceof Date) {
            minSeconds = min.getHours() * 3600 + min.getMinutes() * 60 + min.getSeconds();
        } else {
            const minTime = parseTimeString(min, format ?? 'HH:mm:ss');
            minSeconds = (minTime.hh || 0) * 3600 + (minTime.mm || 0) * 60 + (minTime.ss || 0);
        }
    }
    if (max) {
        if (max instanceof Date) {
            maxSeconds = max.getHours() * 3600 + max.getMinutes() * 60 + max.getSeconds();
        } else {
            const maxTime = parseTimeString(max, format ?? 'HH:mm:ss');
            maxSeconds = (maxTime.hh || 0) * 3600 + (maxTime.mm || 0) * 60 + (maxTime.ss || 0);
        }
    }
    return totalSeconds < minSeconds || totalSeconds > maxSeconds;
};
const delimiter = ':';
const range = (number, padLength = 2, step = 1)=>{
    if (step > number) step = number;
    return Array.from({
        length: Math.ceil(number / step)
    }, (_, i)=>(i * step).toString().padStart(padLength, '0'));
};
const roundToMultiplicity = (value, step)=>{
    if (!step || step <= 1) return value;
    return Math.round(value / step) * step;
};
const parseTimeBoundary = (val, format)=>{
    if (!val) {
        return null;
    }
    if (val instanceof Date) {
        return {
            hh: val.getHours(),
            mm: val.getMinutes(),
            ss: val.getSeconds()
        };
    }
    return parseTimeString(val, format);
};
const toTotalSeconds = (timeValues)=>(timeValues.hh ?? 0) * 3600 + (timeValues.mm ?? 0) * 60 + (timeValues.ss ?? 0);
const clampTimeToMin = (timeValues, minParsed, column, activeFormat, multiplicityMinutes = 1, multiplicitySeconds = 1)=>{
    const result = {
        ...timeValues
    };
    /**
     * Корректируем минуты (только при выборе часа)
     */ if (column === 'hours' && activeFormat.includes('mm')) {
        const clamped = Math.ceil((minParsed.mm ?? 0) / multiplicityMinutes) * multiplicityMinutes;
        if (clamped < 60) {
            result.mm = clamped;
        }
    }
    /**
     * Корректируем секунды
     */ if (!activeFormat.includes('ss') || toTotalSeconds(result) >= toTotalSeconds(minParsed)) {
        return result;
    }
    const clampedSS = Math.ceil((minParsed.ss ?? 0) / multiplicitySeconds) * multiplicitySeconds;
    if (clampedSS < 60) {
        result.ss = clampedSS;
        return result;
    }
    /**
     * Переполнение секунд: переходим на следующую кратную минуту, ss → 0
     */ const nextMM = (result.mm ?? 0) + multiplicityMinutes;
    if (nextMM < 60) {
        result.mm = nextMM;
        result.ss = 0;
    }
    return result;
};
const clampTimeToMax = (timeValues, maxParsed, column, activeFormat, multiplicityMinutes = 1, multiplicitySeconds = 1)=>{
    const result = {
        ...timeValues
    };
    /**
     * Корректируем минуты (только при выборе часа)
     */ if (column === 'hours' && activeFormat.includes('mm')) {
        result.mm = Math.floor((maxParsed.mm ?? 0) / multiplicityMinutes) * multiplicityMinutes;
    }
    /**
     * Корректируем секунды
     */ if (!activeFormat.includes('ss') || toTotalSeconds(result) <= toTotalSeconds(maxParsed)) {
        return result;
    }
    result.ss = Math.floor((maxParsed.ss ?? 0) / multiplicitySeconds) * multiplicitySeconds;
    return result;
};
const animateScrollTo = (element, targetScrollTop, duration = 300)=>{
    const startScrollTop = element.scrollTop;
    const distance = targetScrollTop - startScrollTop;
    let startTime = null;
    const animate = (currentTime)=>{
        if (!startTime) startTime = currentTime;
        const timeElapsed = currentTime - startTime;
        const progress = Math.min(timeElapsed / duration, 1);
        const easeOut = (t)=>1 - (1 - t) ** 3;
        const scrollPosition = startScrollTop + distance * easeOut(progress);
        element.scrollTop = scrollPosition;
        if (timeElapsed < duration) {
            requestAnimationFrame(animate);
        }
    };
    requestAnimationFrame(animate);
};
