/**
 * DevExtreme (cjs/ui/date_box/ui.date_utils.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _renderer = _interopRequireDefault(require("../../core/renderer"));
var _date_serialization = _interopRequireDefault(require("../../core/utils/date_serialization"));
var _type = require("../../core/utils/type");
var _iterator = require("../../core/utils/iterator");
var _date = _interopRequireDefault(require("../../localization/date"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const DATE_COMPONENTS = ["year", "day", "month", "day"];
const TIME_COMPONENTS = ["hours", "minutes", "seconds", "milliseconds"];
const ONE_MINUTE = 6e4;
const ONE_DAY = 864e5;
const ONE_YEAR = 31536e6;
const getStringFormat = function(format) {
    const formatType = typeof format;
    if ("string" === formatType) {
        return "format"
    }
    if ("object" === formatType && void 0 !== format.type) {
        return format.type
    }
    return null
};
const dateUtils = {
    SUPPORTED_FORMATS: ["date", "time", "datetime"],
    ONE_MINUTE: 6e4,
    ONE_DAY: ONE_DAY,
    ONE_YEAR: ONE_YEAR,
    MIN_DATEVIEW_DEFAULT_DATE: new Date(1900, 0, 1),
    MAX_DATEVIEW_DEFAULT_DATE: function() {
        const newDate = new Date;
        return new Date(newDate.getFullYear() + 50, newDate.getMonth(), newDate.getDate(), 23, 59, 59)
    }(),
    FORMATS_INFO: {
        date: {
            getStandardPattern: function() {
                return "yyyy-MM-dd"
            },
            components: DATE_COMPONENTS
        },
        time: {
            getStandardPattern: function() {
                return "HH:mm"
            },
            components: TIME_COMPONENTS
        },
        datetime: {
            getStandardPattern: function() {
                let standardPattern;
                ! function() {
                    const $input = (0, _renderer.default)("<input>").attr("type", "datetime");
                    $input.val("2000-01-01T01:01Z");
                    if ($input.val()) {
                        standardPattern = "yyyy-MM-ddTHH:mmZ"
                    }
                }();
                if (!standardPattern) {
                    standardPattern = "yyyy-MM-ddTHH:mm:ssZ"
                }
                dateUtils.FORMATS_INFO.datetime.getStandardPattern = function() {
                    return standardPattern
                };
                return standardPattern
            },
            components: [...DATE_COMPONENTS, ...TIME_COMPONENTS]
        },
        "datetime-local": {
            getStandardPattern: function() {
                return "yyyy-MM-ddTHH:mm:ss"
            },
            components: [...DATE_COMPONENTS, "hours", "minutes", "seconds"]
        }
    },
    FORMATS_MAP: {
        date: "shortdate",
        time: "shorttime",
        datetime: "shortdateshorttime"
    },
    SUBMIT_FORMATS_MAP: {
        date: "date",
        time: "time",
        datetime: "datetime-local"
    },
    toStandardDateFormat: function(date, type) {
        const pattern = dateUtils.FORMATS_INFO[type].getStandardPattern();
        return _date_serialization.default.serializeDate(date, pattern)
    },
    fromStandardDateFormat: function(text) {
        const date = _date_serialization.default.dateParser(text);
        return (0, _type.isDate)(date) ? date : void 0
    },
    getMaxMonthDay: function(year, month) {
        return new Date(year, month + 1, 0).getDate()
    },
    mergeDates: function(oldValue, newValue, format) {
        if (!newValue) {
            return newValue || null
        }
        if (!oldValue || isNaN(oldValue.getTime())) {
            const now = new Date(null);
            oldValue = new Date(now.getFullYear(), now.getMonth(), now.getDate())
        }
        const result = new Date(oldValue.valueOf());
        const formatInfo = dateUtils.FORMATS_INFO[format];
        (0, _iterator.each)(formatInfo.components, (function() {
            const componentInfo = dateUtils.DATE_COMPONENTS_INFO[this];
            result[componentInfo.setter](newValue[componentInfo.getter]())
        }));
        return result
    },
    getLongestCaptionIndex: function(captionArray) {
        let longestIndex = 0;
        let longestCaptionLength = 0;
        let i;
        for (i = 0; i < captionArray.length; ++i) {
            if (captionArray[i].length > longestCaptionLength) {
                longestIndex = i;
                longestCaptionLength = captionArray[i].length
            }
        }
        return longestIndex
    },
    formatUsesMonthName: function(format) {
        return _date.default.formatUsesMonthName(format)
    },
    formatUsesDayName: function(format) {
        return _date.default.formatUsesDayName(format)
    },
    getLongestDate: function(format, monthNames, dayNames) {
        const stringFormat = getStringFormat(format);
        let month = 9;
        if (!stringFormat || dateUtils.formatUsesMonthName(stringFormat)) {
            month = dateUtils.getLongestCaptionIndex(monthNames)
        }
        const longestDate = new Date(1888, month, 21, 23, 59, 59, 999);
        if (!stringFormat || dateUtils.formatUsesDayName(stringFormat)) {
            const date = longestDate.getDate() - longestDate.getDay() + dateUtils.getLongestCaptionIndex(dayNames);
            longestDate.setDate(date)
        }
        return longestDate
    },
    normalizeTime: function(date) {
        date.setSeconds(0);
        date.setMilliseconds(0)
    }
};
dateUtils.DATE_COMPONENTS_INFO = {
    year: {
        getter: "getFullYear",
        setter: "setFullYear",
        formatter: function(value, date) {
            const formatDate = new Date(date.getTime());
            formatDate.setFullYear(value);
            return _date.default.format(formatDate, "yyyy")
        },
        startValue: void 0,
        endValue: void 0
    },
    day: {
        getter: "getDate",
        setter: "setDate",
        formatter: function(value, date) {
            const formatDate = new Date(date.getTime());
            formatDate.setDate(value);
            return _date.default.format(formatDate, "d")
        },
        startValue: 1,
        endValue: void 0
    },
    month: {
        getter: "getMonth",
        setter: "setMonth",
        formatter: function(value) {
            return _date.default.getMonthNames()[value]
        },
        startValue: 0,
        endValue: 11
    },
    hours: {
        getter: "getHours",
        setter: "setHours",
        formatter: function(value) {
            return _date.default.format(new Date(0, 0, 0, value), "hour")
        },
        startValue: 0,
        endValue: 23
    },
    minutes: {
        getter: "getMinutes",
        setter: "setMinutes",
        formatter: function(value) {
            return _date.default.format(new Date(0, 0, 0, 0, value), "minute")
        },
        startValue: 0,
        endValue: 59
    },
    seconds: {
        getter: "getSeconds",
        setter: "setSeconds",
        formatter: function(value) {
            return _date.default.format(new Date(0, 0, 0, 0, 0, value), "second")
        },
        startValue: 0,
        endValue: 59
    },
    milliseconds: {
        getter: "getMilliseconds",
        setter: "setMilliseconds",
        formatter: function(value) {
            return _date.default.format(new Date(0, 0, 0, 0, 0, 0, value), "millisecond")
        },
        startValue: 0,
        endValue: 999
    }
};
var _default = dateUtils;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
