"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIntervalData = exports.populateData = exports.expressionToData = void 0;
const interval_1 = require("./interval");
const interval_common_1 = require("./interval-common");
function expressionToData(expression) {
    const data = {};
    const inversed = expression.charAt(0) === '-';
    data.inversed = inversed;
    const invalid = interval_common_1.IntervalExpressionValidationRegex.test(inversed ? expression.substring(1) : expression);
    if (invalid) {
        throw new Error(`L'expression ${expression} n'est pas valide`);
    }
    let unit;
    let match;
    for (unit in interval_common_1.IntervalUnitRegexes) {
        match = expression.match(interval_common_1.IntervalUnitRegexes[unit]);
        data[unit] = match === null ? 0 : parseInt(match[1]);
    }
    return data;
}
exports.expressionToData = expressionToData;
function populateData(from, to) {
    if (!to) {
        to = {};
    }
    interval_common_1.IntervalUnitArray.forEach((unit) => {
        to[unit] = from[unit] || 0;
    });
    to.inversed = from.inversed === true;
    return to;
}
exports.populateData = populateData;
function buildIntervalData(input) {
    if (typeof (input) === 'string') {
        return expressionToData(input);
    }
    if (input !== null && (input instanceof interval_1.Interval || typeof (input) === 'object')) {
        return populateData(input);
    }
    throw new Error('Interval must be string, Interval or IntervalData');
}
exports.buildIntervalData = buildIntervalData;
