/**
 * DevExtreme (ui/date_box/ui.date_box.mask.parts.js)
 * Version: 18.2.3 (build 18323)
 * Build date: Mon Nov 19 2018
 *
 * Copyright (c) 2012 - 2018 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.renderDateParts = exports.getDatePartIndexByPosition = void 0;
var _date = require("../../localization/ldml/date.parser");
var _date2 = require("../../localization/date");
var _date3 = _interopRequireDefault(_date2);
var _extend = require("../../core/utils/extend");
var _math = require("../../core/utils/math");
var _common = require("../../core/utils/common");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        "default": obj
    }
}
var PATTERN_GETTERS = {
    a: function(date) {
        return date.getHours() < 12 ? 0 : 1
    },
    E: "getDay",
    y: "getFullYear",
    M: "getMonth",
    L: "getMonth",
    d: "getDate",
    H: "getHours",
    h: "getHours",
    m: "getMinutes",
    s: "getSeconds",
    S: "getMilliseconds"
};
var PATTERN_SETTERS = (0, _extend.extend)({}, (0, _date.getPatternSetters)(), {
    a: function(date, value) {
        var hours = date.getHours(),
            current = hours >= 12;
        if (current === value) {
            return
        }
        date.setHours((hours + 12) % 24)
    },
    M: function(date, value) {
        var day = date.getDate();
        date.setMonth(value, 1);
        var dayLimits = getLimits("getDate", date),
            newDay = (0, _math.fitIntoRange)(day, dayLimits.min, dayLimits.max);
        date.setDate(newDay)
    },
    E: function(date, value) {
        if (value < 0) {
            return
        }
        date.setDate(date.getDate() - date.getDay() + value)
    },
    y: function(date, value) {
        var currentYear = date.getFullYear(),
            valueLength = String(value).length,
            newValue = currentYear - currentYear % Math.pow(10, valueLength) + value;
        date.setFullYear(newValue)
    }
});
var getPatternGetter = function(patternChar) {
    var unsupportedCharGetter = function() {
        return patternChar
    };
    return PATTERN_GETTERS[patternChar] || unsupportedCharGetter
};
var renderDateParts = function(text, format) {
    var regExpInfo = (0, _date.getRegExpInfo)(format, _date3.default),
        result = regExpInfo.regexp.exec(text);
    var start = 0,
        end = 0,
        sections = [];
    for (var i = 1; i < result.length; i++) {
        start = end;
        end = start + result[i].length;
        var pattern = regExpInfo.patterns[i - 1],
            getter = getPatternGetter(pattern[0]);
        sections.push({
            index: i - 1,
            isStub: pattern === (0, _common.escapeRegExp)(result[i]),
            caret: {
                start: start,
                end: end
            },
            pattern: pattern,
            text: result[i],
            limits: getLimits.bind(void 0, getter),
            setter: PATTERN_SETTERS[pattern[0]] || _common.noop,
            getter: getter
        })
    }
    return sections
};
var getLimits = function(getter, date) {
    var limits = {
        getFullYear: {
            min: 0,
            max: 9999
        },
        getMonth: {
            min: 0,
            max: 11
        },
        getDate: {
            min: 1,
            max: new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate()
        },
        getDay: {
            min: 0,
            max: 6
        },
        getHours: {
            min: 0,
            max: 23
        },
        getMinutes: {
            min: 0,
            max: 59
        },
        getAmPm: {
            min: 0,
            max: 1
        }
    };
    return limits[getter] || limits.getAmPm
};
var getDatePartIndexByPosition = function(dateParts, position) {
    for (var i = 0; i < dateParts.length; i++) {
        var caretInGroup = dateParts[i].caret.end >= position;
        if (!dateParts[i].isStub && caretInGroup) {
            return i
        }
    }
    return null
};
exports.getDatePartIndexByPosition = getDatePartIndexByPosition;
exports.renderDateParts = renderDateParts;
