var _a;
// tslint:disable-next-line: import-blacklist
import 'moment/locale/fr';
import { FormatMode } from '../../../utils/i18n/i18n';
import { ModulVue } from '../../../utils/vue/vue';
import { TIME_NAME, TIME_PERIOD_NAME } from '../../filter-names';
export var TimeFilterPrepositions;
(function (TimeFilterPrepositions) {
    TimeFilterPrepositions["From"] = "from";
    TimeFilterPrepositions["Until"] = "until";
    TimeFilterPrepositions["At"] = "at";
    TimeFilterPrepositions["None"] = "none";
})(TimeFilterPrepositions || (TimeFilterPrepositions = {}));
var prepositionToTrad = (_a = {},
    _a[TimeFilterPrepositions.From] = 'f-m-time:fromTime',
    _a[TimeFilterPrepositions.Until] = 'f-m-time:untilTime',
    _a[TimeFilterPrepositions.At] = 'f-m-time:atTime',
    _a[TimeFilterPrepositions.None] = 'f-m-time:timeOnly',
    _a);
function getLocalDateTime(time) {
    var date = undefined;
    try {
        if (time instanceof Date) {
            date = new Date(time);
        }
        else if (typeof time === 'string') {
            var now = new Date();
            var datePart = now.getFullYear() + "-" + (now.getMonth() + 1).toString().padStart(2, '0') + "-" + now.getDate().toString().padStart(2, '0');
            var timePart = !time.includes(':') ? time.padStart(2, '0') + ":00:00" : time.split(':').length === 2 ? time + ":00" : time;
            date = new Date(datePart + "T" + timePart + "Z");
            date.setHours(date.getHours() + date.getTimezoneOffset() / 60);
        }
        return isNaN(date) ? undefined : date;
    }
    catch (_a) {
        return undefined;
    }
    throw new Error('Unhandled time type.');
}
function extractHourValueAsString(value) {
    if (!value) {
        return undefined;
    }
    if (value instanceof Date) {
        return value.getHours() + ":" + value.getMinutes;
    }
    else if (typeof value === 'string') {
        return value;
    }
    else {
        throw new Error('Unsupported type.  The value must be undefined, a string or a Date');
    }
}
export var timeFilter = function (time, options) {
    if (options === void 0) { options = { preposition: TimeFilterPrepositions.None }; }
    if (!time) {
        return '';
    }
    var date = getLocalDateTime(time);
    if (date) {
        var intlOptions = {
            hour: 'numeric',
            minute: date.getMinutes() !== 0 ? 'numeric' : undefined
        };
        var formatedTime = new Intl.DateTimeFormat(ModulVue.prototype.$i18n.currentLocale, intlOptions).format(date)
            .replace(/ /g, String.fromCharCode(160));
        // Remove when https://bugs.chromium.org/p/chromium/issues/detail?id=982778#c3 is fixed.
        if (ModulVue.prototype.$i18n.currentLocale === 'fr-CA' && formatedTime.match(/(0)[0-9]\sh\s*[0-9]*[0-9]*/g)) {
            formatedTime = formatedTime.substr(1, formatedTime.length);
        }
        return ModulVue.prototype.$i18n.translate(prepositionToTrad[options.preposition], { time: formatedTime }, 0, '', true, FormatMode.Vsprintf);
    }
    return '';
};
export var timePeriodFilter = function (timeRange, options) {
    if (options === void 0) { options = { preposition: false }; }
    if (!timeRange || (!timeRange.from && !timeRange.to)) {
        return '';
    }
    var formattedFrom = timeFilter(timeRange.from);
    var formattedTo = timeFilter(timeRange.to);
    if (!timeRange.from && timeRange.to) {
        return timeFilter(timeRange.to, { preposition: options.preposition ? TimeFilterPrepositions.Until : TimeFilterPrepositions.None });
    }
    if (timeRange.from && !timeRange.to) {
        return timeFilter(timeRange.from, { preposition: options.preposition ? TimeFilterPrepositions.From : TimeFilterPrepositions.None });
    }
    var fromAsString = extractHourValueAsString(timeRange.from);
    var toAsString = extractHourValueAsString(timeRange.to);
    if (fromAsString !== toAsString) {
        return ModulVue.prototype.$i18n.translate(!options.preposition ? 'f-m-time:timePeriod' : 'f-m-time:timePeriodWithPreposition', { from: formattedFrom, to: formattedTo }, 0, '', true, FormatMode.Vsprintf);
    }
    else {
        return timeFilter(timeRange.from, { preposition: options.preposition ? TimeFilterPrepositions.At : TimeFilterPrepositions.None });
    }
};
var TimeFilterPlugin = {
    install: function (v) {
        v.filter(TIME_NAME, timeFilter);
        v.filter(TIME_PERIOD_NAME, timePeriodFilter);
    }
};
export default TimeFilterPlugin;
//# sourceMappingURL=time.js.map