import { Interval } from './interval';
import { IntervalUnitRegexes, IntervalUnitArray, IntervalExpressionValidationRegex } from './interval-common';
export function expressionToData(expression) {
    const data = {};
    const inversed = expression.charAt(0) === '-';
    data.inversed = inversed;
    const invalid = 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 IntervalUnitRegexes) {
        match = expression.match(IntervalUnitRegexes[unit]);
        data[unit] = match === null ? 0 : parseInt(match[1]);
    }
    return data;
}
export function populateData(from, to) {
    if (!to) {
        to = {};
    }
    IntervalUnitArray.forEach((unit) => {
        to[unit] = from[unit] || 0;
    });
    to.inversed = from.inversed === true;
    return to;
}
export function buildIntervalData(input) {
    if (typeof (input) === 'string') {
        return expressionToData(input);
    }
    if (input !== null && (input instanceof Interval || typeof (input) === 'object')) {
        return populateData(input);
    }
    throw new Error('Interval must be string, Interval or IntervalData');
}
