"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.intervalDump = exports.intervalToSeconds = exports.intervalSum = exports.Interval = void 0;
const interval_forge_1 = require("./interval-forge");
const interval_common_1 = require("./interval-common");
class Interval {
    seconds;
    static from(input) {
        return new Interval(input);
    }
    constructor(input) {
        const data = (0, interval_forge_1.buildIntervalData)(input);
        (0, interval_forge_1.populateData)(data, this);
    }
    toMilliseconds() {
        return this.toSeconds() * 1000;
    }
    toSeconds() {
        if (this.seconds !== undefined) {
            return this.seconds;
        }
        const seconds = intervalToSeconds(this);
        this.seconds = seconds;
        return seconds;
    }
    toMinutes() {
        return this.toSeconds() / 60;
    }
    toHours() {
        return this.toSeconds() / 3600;
    }
    isEqual(interval) {
        return this.toMilliseconds() === interval.toMilliseconds();
    }
    toString() {
        return intervalDump(this) || '';
    }
}
exports.Interval = Interval;
function intervalSum(intervalArgs) {
    const outputData = {};
    interval_common_1.IntervalUnitArray.forEach((unit) => {
        outputData[unit] = 0;
    });
    let direction;
    let data;
    intervalArgs.forEach((interval) => {
        data = (0, interval_forge_1.buildIntervalData)(interval);
        direction = data.inversed ? -1 : 1;
        interval_common_1.IntervalUnitArray.forEach((unit) => {
            outputData[unit] += data[unit] * direction;
        });
    });
    return new Interval(outputData);
}
exports.intervalSum = intervalSum;
function intervalToSeconds(data) {
    let seconds = 0;
    let unit;
    for (unit in interval_common_1.IntervalUnitSecondFactors) {
        seconds += data[unit] * interval_common_1.IntervalUnitSecondFactors[unit] || 0;
    }
    return seconds;
}
exports.intervalToSeconds = intervalToSeconds;
function intervalDump(data) {
    let intervalStr = '';
    interval_common_1.IntervalUnitDescArray.forEach((unit) => {
        if (data[unit]) {
            intervalStr += String(data[unit]) + unit;
        }
    });
    if (intervalStr) {
        return (data.inversed ? '-' : '') + intervalStr;
    }
    return null;
}
exports.intervalDump = intervalDump;
