"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var string_1 = require("@dojo/framework/shim/string");
var components = {
    day: '(\\d{1,2})',
    month: '(\\d{1,2})',
    year: '(\\d{4})'
};
var dateExpressions = function () {
    var tokens = [
        [
            // standard 'YYYY-mm-dd' format
            new RegExp("^" + components.year + "-" + components.month + "-" + components.day + "$"),
            { month: 2, day: 3, year: 1 }
        ]
    ];
    var usCanary = Intl.DateTimeFormat().format(new Date(2018, 3, 3)); // April 3
    switch (usCanary) {
        case '4/3/2018':
        case '04/03/2018':
            // US-only 'mm/dd/yyyy' format
            tokens.push([
                new RegExp("^" + components.month + "/" + components.day + "/" + components.year + "$"),
                { month: 1, day: 2, year: 3 }
            ]);
            break;
        case '3/4/2018':
        case '03/04/2018':
            // standard 'dd/mm/yyyy' format
            tokens.push([
                new RegExp("^" + components.day + "/" + components.month + "/" + components.year + "$"),
                { month: 2, day: 1, year: 3 }
            ]);
            break;
        case '3.4.2018':
        case '03.04.2018':
            // standard 'dd.mm.yyyy' format
            tokens.push([
                new RegExp("^" + components.day + "\\." + components.month + "\\." + components.year + "$"),
                { month: 2, day: 1, year: 3 }
            ]);
            break;
        case '3-4-2018':
        case '03-04-2018':
            // standard 'dd.mm.yyyy' format
            tokens.push([
                new RegExp("^" + components.day + "-" + components.month + "-" + components.year + "$"),
                { month: 2, day: 1, year: 3 }
            ]);
            break;
        case '2018/4/3':
        case '2018/04/03':
            // standard 'yyyy/mm/dd' format
            tokens.push([
                new RegExp("^" + components.year + "/" + components.month + "/" + components.day + "$"),
                { month: 2, day: 3, year: 1 }
            ]);
            break;
    }
    return tokens;
};
function parseDate(value) {
    var e_1, _a;
    if (!value) {
        return undefined;
    }
    try {
        for (var _b = tslib_1.__values(dateExpressions()), _c = _b.next(); !_c.done; _c = _b.next()) {
            var _d = tslib_1.__read(_c.value, 2), exp = _d[0], order = _d[1];
            var match = value.match(exp);
            if (match !== null) {
                return new Date(parseInt(match[order.year], 10), parseInt(match[order.month], 10) - 1, parseInt(match[order.day], 10));
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return undefined;
}
exports.parseDate = parseDate;
function formatDateISO(date) {
    if (!date) {
        return '';
    }
    var year = date.getFullYear();
    var month = string_1.padStart((date.getMonth() + 1).toString(), 2, '0');
    var day = string_1.padStart(date.getDate().toString(), 2, '0');
    return year + "-" + month + "-" + day;
}
exports.formatDateISO = formatDateISO;
function formatDate(date) {
    var formattedDate = Intl.DateTimeFormat().format(date);
    // Check if is a supported date format
    var parsedDate = parseDate(formattedDate);
    if (parsedDate && parsedDate.valueOf() === date.valueOf()) {
        return formattedDate;
    }
    return formatDateISO(date);
}
exports.formatDate = formatDate;

/*# sourceMappingURL=date-utils.js.map*/