"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 delimiter () {
        return delimiter;
    },
    get normalizeTimeSegment () {
        return normalizeTimeSegment;
    },
    get processTimeInput () {
        return processTimeInput;
    },
    get range () {
        return range;
    }
});
const delimiter = ':';
const range = (number, padLength = 2)=>{
    return Array.from({
        length: number
    }, (_, i)=>i.toString().padStart(padLength, '0'));
};
const normalizeTimeSegment = (type, input)=>{
    const max = type === 'HH' ? 23 : 59;
    const digits = input.replace(/\D/g, '');
    const num = parseInt(digits, 10);
    if (!digits) {
        return {
            text: '',
            value: null,
            carryover: ''
        };
    }
    if (Math.floor(max / 10) < num && digits.length === 1) {
        return {
            text: num.toString().padStart(2, '0'),
            value: num,
            carryover: ''
        };
    }
    if (num <= max && digits.length === 1) {
        return {
            text: digits,
            value: num,
            carryover: ''
        };
    }
    if (digits.length === 1) {
        return {
            text: max.toString().padStart(2, '0'),
            value: max,
            carryover: ''
        };
    }
    if (Number.isNaN(num)) {
        return {
            text: '',
            value: null,
            carryover: ''
        };
    }
    if (num <= max) {
        return {
            text: digits.padStart(2, '0'),
            value: num,
            carryover: ''
        };
    }
    const firstDigit = parseInt(digits[0], 10);
    const secondDigit = parseInt(digits[1], 10);
    if (firstDigit <= max) {
        return {
            text: firstDigit.toString().padStart(2, '0'),
            value: firstDigit,
            carryover: secondDigit.toString()
        };
    }
    return {
        text: max.toString().padStart(2, '0'),
        value: max,
        carryover: digits.slice(1)
    };
};
const processTimeInput = (input, format, cursorPosition)=>{
    const digits = input.replace(/\D/g, '');
    const tokens = format.split(delimiter);
    const values = {
        hh: null,
        mm: null,
        ss: null
    };
    const segments = [];
    let digitIndex = 0;
    let newCursorPosition = cursorPosition ?? 0;
    tokens.forEach((token)=>{
        const segmentDigits = digits.slice(digitIndex, digitIndex + 2);
        digitIndex += segmentDigits.length;
        const { text, value } = normalizeTimeSegment(token, segmentDigits);
        values[token.toLowerCase()] = value;
        segments.push(text);
    });
    let innerString = segments[0];
    if (segments.length > 1 && segments[1]) {
        innerString += `:${segments[1]}`;
    }
    if (format === 'HH:mm:ss' && segments.length > 2 && segments[2]) {
        innerString += `:${segments[2]}`;
    }
    if (input.length > innerString.length) {
        newCursorPosition = Math.min(newCursorPosition, innerString.length);
    } else if (innerString.length - 2 <= newCursorPosition) {
        newCursorPosition = innerString.length;
    }
    return {
        innerString,
        values,
        newCursorPosition
    };
};
