"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatMilliseconds = exports.getMomentDateWithCustomStartOfWeek = exports.convertWeekDayToMomentWeekDay = void 0;
const tslib_1 = require("tslib");
const moment_timezone_1 = tslib_1.__importDefault(require("moment-timezone"));
const timeFrames_1 = require("./timeFrames");
// from 0 (Monday) to 6 (Sunday) to 0 (Sunday) to 6 (Saturday)
const convertWeekDayToMomentWeekDay = (weekDay) => {
    const converted = weekDay + 1;
    return converted <= 6 ? converted : 0;
};
exports.convertWeekDayToMomentWeekDay = convertWeekDayToMomentWeekDay;
const createMomentLocaleForWeekStart = (name, startOfWeek) => {
    if (!moment_timezone_1.default.locales().includes(name)) {
        moment_timezone_1.default.locale(name, {
            week: {
                dow: (0, exports.convertWeekDayToMomentWeekDay)(startOfWeek),
            },
        });
    }
};
const getMomentDateWithCustomStartOfWeek = (startOfWeek, inp) => {
    if ((0, timeFrames_1.isWeekDay)(startOfWeek)) {
        const localeName = `lightdash-start-of-week-${startOfWeek}`;
        createMomentLocaleForWeekStart(localeName, startOfWeek);
        return (0, moment_timezone_1.default)(inp).locale(localeName);
    }
    return (0, moment_timezone_1.default)(inp);
};
exports.getMomentDateWithCustomStartOfWeek = getMomentDateWithCustomStartOfWeek;
const formatMilliseconds = (ms, fractionDigits = 2) => ms.toFixed(fractionDigits).replace(/\.?0+$/, '');
exports.formatMilliseconds = formatMilliseconds;
//# sourceMappingURL=time.js.map