"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useDatePicker", {
    enumerable: true,
    get: function() {
        return useDatePicker;
    }
});
var _datejs = require("../../../utils/datejs");
var _utils = require("../../Calendar/utils");
var _DatePickertokens = require("../DatePicker.tokens");
var _utils1 = require("../utils");
var useDatePicker = function useDatePicker(param) {
    var currentValue = param.currentValue, format = param.format, _param_lang = param.lang, lang = _param_lang === void 0 ? 'ru' : _param_lang, disabled = param.disabled, readOnly = param.readOnly, valueError = param.valueError, valueSuccess = param.valueSuccess, name = param.name, type = param.type, min = param.min, max = param.max, includeEdgeDates = param.includeEdgeDates, maskWithFormat = param.maskWithFormat, dateFormatDelimiter = param.dateFormatDelimiter, setCorrectDates = param.setCorrectDates, setInnerDate = param.setInnerDate, onChangeValue = param.onChangeValue, onCommitDate = param.onCommitDate, onChange = param.onChange;
    var datePickerErrorClass = valueError ? _DatePickertokens.classes.datePickerError : undefined;
    var datePickerSuccessClass = valueSuccess ? _DatePickertokens.classes.datePickerSuccess : undefined;
    var getQuarterInfo = function getQuarterInfo(originalDate) {
        if (type !== 'Quarters') {
            return;
        }
        var endQuarter = new Date(originalDate);
        endQuarter.setMonth(originalDate.getMonth() + 3);
        endQuarter.setDate(0);
        _datejs.customDayjs.locale(lang);
        var quarterIndex = (0, _datejs.customDayjs)(originalDate).quarter() - 1;
        return {
            name: _utils.QUARTER_NAMES[quarterIndex],
            fullValue: [
                originalDate,
                endQuarter
            ]
        };
    };
    var handleChangeValue = function handleChangeValue(event) {
        if (disabled || readOnly) {
            return;
        }
        var value = event.target.value;
        var newValue = maskWithFormat ? (0, _utils1.getMaskedDateOnInput)(value, format, dateFormatDelimiter, currentValue) : value;
        var _getFormattedDates = (0, _utils1.getFormattedDates)({
            value: newValue,
            lang: lang,
            delimiter: dateFormatDelimiter,
            format: format,
            includeEdgeDates: includeEdgeDates,
            min: min,
            max: max
        }), formattedDate = _getFormattedDates.formattedDate, isoDate = _getFormattedDates.isoDate, originalDate = _getFormattedDates.originalDate;
        if (originalDate) {
            setCorrectDates({
                calendar: originalDate,
                input: formattedDate
            });
            var dateInfo = getQuarterInfo(originalDate);
            if (onCommitDate) {
                (0, _utils1.invokeOnCommitDate)({
                    callback: onCommitDate,
                    value: formattedDate,
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo: dateInfo,
                        originalDate: originalDate,
                        isoDate: isoDate
                    }
                });
            }
            setInnerDate(originalDate);
            if (onChangeValue) {
                onChangeValue(event, formattedDate, originalDate, isoDate);
            }
            if (onChange) {
                onChange({
                    target: {
                        value: formattedDate,
                        originalDate: originalDate,
                        isoDate: isoDate,
                        name: name
                    }
                });
            }
            return;
        }
        if (formattedDate === '') {
            setCorrectDates({
                calendar: undefined,
                input: ''
            });
        }
        setInnerDate(formattedDate);
        if (onChangeValue) {
            onChangeValue(event, formattedDate, originalDate, isoDate);
        }
        if (onChange) {
            onChange({
                target: {
                    value: formattedDate,
                    originalDate: originalDate,
                    isoDate: isoDate,
                    name: name
                }
            });
        }
    };
    var handleSearch = function handleSearch(date) {
        if (disabled || readOnly) {
            return;
        }
        if (!date) {
            setInnerDate(undefined);
            if (onCommitDate) {
                (0, _utils1.invokeOnCommitDate)({
                    callback: onCommitDate,
                    value: '',
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo: undefined,
                        originalDate: undefined,
                        isoDate: ''
                    }
                });
            }
            return;
        }
        var _getFormattedDates = (0, _utils1.getFormattedDates)({
            value: date,
            lang: lang,
            delimiter: dateFormatDelimiter,
            format: format,
            includeEdgeDates: includeEdgeDates,
            min: min,
            max: max
        }), formattedDate = _getFormattedDates.formattedDate, isoDate = _getFormattedDates.isoDate, originalDate = _getFormattedDates.originalDate;
        if (originalDate) {
            var dateInfo = getQuarterInfo(originalDate);
            if (onCommitDate) {
                (0, _utils1.invokeOnCommitDate)({
                    callback: onCommitDate,
                    value: formattedDate,
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo: dateInfo,
                        originalDate: originalDate,
                        isoDate: isoDate
                    }
                });
            }
        }
    };
    var handleCalendarPick = function handleCalendarPick(date, dateInfo) {
        if (disabled || readOnly) {
            return;
        }
        if (!date) {
            setInnerDate(date);
            setCorrectDates({
                calendar: date || undefined,
                input: ''
            });
            if (onChangeValue) {
                onChangeValue(null, '', date, '');
            }
            if (onChange) {
                onChange({
                    target: {
                        value: '',
                        originalDate: date,
                        isoDate: '',
                        name: name
                    }
                });
            }
            if (onCommitDate) {
                (0, _utils1.invokeOnCommitDate)({
                    callback: onCommitDate,
                    value: '',
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo: dateInfo,
                        originalDate: date || undefined,
                        isoDate: ''
                    }
                });
            }
            return;
        }
        _datejs.customDayjs.locale(lang);
        var formattedDate = (0, _datejs.customDayjs)(date).format(format);
        var isoDate = date.toISOString();
        setInnerDate(date);
        setCorrectDates({
            calendar: date,
            input: formattedDate
        });
        if (onChangeValue) {
            onChangeValue(null, formattedDate, date, isoDate);
        }
        if (onChange) {
            onChange({
                target: {
                    value: formattedDate,
                    originalDate: date,
                    isoDate: isoDate,
                    name: name
                }
            });
        }
        if (onCommitDate) {
            (0, _utils1.invokeOnCommitDate)({
                callback: onCommitDate,
                value: formattedDate,
                formattedValues: {
                    error: false,
                    success: true,
                    dateInfo: dateInfo,
                    originalDate: date,
                    isoDate: isoDate
                }
            });
        }
    };
    var updateExternalDate = function updateExternalDate(externalDate) {
        var _getFormattedDates = (0, _utils1.getFormattedDates)({
            value: externalDate || null,
            lang: lang,
            delimiter: dateFormatDelimiter,
            format: format,
            includeEdgeDates: includeEdgeDates,
            min: min,
            max: max
        }), formattedDate = _getFormattedDates.formattedDate, originalDate = _getFormattedDates.originalDate;
        setInnerDate(originalDate);
        if (originalDate) {
            setCorrectDates({
                calendar: originalDate,
                input: formattedDate
            });
        }
        if (formattedDate === '') {
            setCorrectDates({
                calendar: undefined,
                input: ''
            });
        }
    };
    return {
        datePickerErrorClass: datePickerErrorClass,
        datePickerSuccessClass: datePickerSuccessClass,
        handleChangeValue: handleChangeValue,
        handleSearch: handleSearch,
        handleCalendarPick: handleCalendarPick,
        updateExternalDate: updateExternalDate,
        getQuarterInfo: getQuarterInfo
    };
};
