"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapRange = exports.prepareRanges = void 0;
const assert_js_1 = require("./assert.js");
function prepareRanges(rawRanges) {
    const ranges = rawRanges.map(rng => {
        (0, assert_js_1.assertScript)(rng.value, 'Range output value is required');
        return {
            value: rng.value,
            min: rng.min == null || String(rng.min) === '' ? null : parseInt(String(rng.min), 10),
            max: rng.max == null || String(rng.max) === '' ? null : parseInt(String(rng.max), 10),
        };
    }).sort((a, b) => ((a.min || -Infinity) < (b.min || -Infinity) ? -1 : 1));
    for (let i = 1; i < ranges.length; i += 1) {
        const rng = ranges[i];
        const prevRng = ranges[i - 1];
        const overlap = rng.min == null ||
            prevRng.max == null ||
            rng.min < prevRng.max;
        (0, assert_js_1.assertScript)(!overlap, 'Ranges may not overlap');
    }
    return ranges;
}
exports.prepareRanges = prepareRanges;
function mapRange(ranges, value) {
    for (const range of prepareRanges(ranges)) {
        const satMin = range.min == null || value >= range.min;
        const satMax = range.max == null || value < range.max;
        if (satMin && satMax) {
            return range.value;
        }
    }
    throw (0, assert_js_1.playbackError)(`Value ${value} is not covered by the ranges`);
}
exports.mapRange = mapRange;
//# sourceMappingURL=map-range.js.map