"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTimeText = exports.padTwo = void 0;
const padTwo = (n) => n.toString().padStart(2, "0");
exports.padTwo = padTwo;
const clampDigits = (digits, withSeconds) => {
    if (digits.length < 2) {
        return digits;
    }
    let hh = parseInt(digits.slice(0, 2), 10);
    if (hh > 23) {
        hh = 23;
    }
    let out = padTwo(hh);
    if (digits.length < 4) {
        return out + digits.slice(2);
    }
    let mm = parseInt(digits.slice(2, 4), 10);
    if (mm > 59) {
        mm = 59;
    }
    out += padTwo(mm);
    if (!withSeconds || digits.length < 6) {
        return out + digits.slice(4);
    }
    let ss = parseInt(digits.slice(4, 6), 10);
    if (ss > 59) {
        ss = 59;
    }
    out += padTwo(ss);
    return out;
};
const parseTimeText = (text, withSeconds) => {
    var _a;
    if (!text) {
        return "";
    }
    const max = withSeconds ? 6 : 4;
    const segments = text.split(":");
    let raw;
    if (segments.length === 1) {
        raw = ((_a = segments[0]) !== null && _a !== void 0 ? _a : "").replace(/\D/gu, "");
        if (raw.length === 1) {
            raw = "0" + raw;
        }
    }
    else {
        raw = segments.map((s) => {
            const d = s.replace(/\D/gu, "");
            if (d.length === 0) {
                return "";
            }
            if (d.length === 1) {
                return "0" + d;
            }
            return d.slice(0, 2);
        }).join("");
    }
    return clampDigits(raw.slice(0, max), withSeconds);
};
exports.parseTimeText = parseTimeText;
//# sourceMappingURL=utils.js.map