import { customDayjs } from "../../../utils/datejs";
import { QUARTER_NAMES } from "../../Calendar/utils";
import { classes } from "../DatePicker.tokens";
import { getFormattedDates, getMaskedDateOnInput, invokeOnCommitDate } from "../utils";
export const useDatePicker = ({ currentValue, format, lang = 'ru', disabled, readOnly, valueError, valueSuccess, valueEdited, name, type, min, max, includeEdgeDates, maskWithFormat, dateFormatDelimiter, setCorrectDates, setInnerDate, onChangeValue, onCommitDate, onChange })=>{
    const datePickerErrorClass = valueError ? classes.datePickerError : undefined;
    const datePickerSuccessClass = valueSuccess ? classes.datePickerSuccess : undefined;
    const datePickerEditedClass = valueEdited ? classes.datePickerEdited : undefined;
    const getQuarterInfo = (originalDate)=>{
        if (type !== 'Quarters') {
            return;
        }
        const endQuarter = new Date(originalDate);
        endQuarter.setMonth(originalDate.getMonth() + 3);
        endQuarter.setDate(0);
        customDayjs.locale(lang);
        const quarterIndex = customDayjs(originalDate).quarter() - 1;
        return {
            name: QUARTER_NAMES[quarterIndex],
            fullValue: [
                originalDate,
                endQuarter
            ]
        };
    };
    const handleChangeValue = (event)=>{
        if (disabled || readOnly) {
            return;
        }
        const { value } = event.target;
        const newValue = maskWithFormat ? getMaskedDateOnInput(value, format, dateFormatDelimiter, currentValue) : value;
        const { formattedDate, isoDate, originalDate } = getFormattedDates({
            value: newValue,
            lang,
            delimiter: dateFormatDelimiter,
            format,
            includeEdgeDates,
            min,
            max
        });
        if (originalDate) {
            setCorrectDates({
                calendar: originalDate,
                input: formattedDate
            });
            const dateInfo = getQuarterInfo(originalDate);
            if (onCommitDate) {
                invokeOnCommitDate({
                    callback: onCommitDate,
                    value: formattedDate,
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo,
                        originalDate,
                        isoDate
                    }
                });
            }
            setInnerDate(originalDate);
            if (onChangeValue) {
                onChangeValue(event, formattedDate, originalDate, isoDate);
            }
            if (onChange) {
                onChange({
                    target: {
                        value: formattedDate,
                        originalDate,
                        isoDate,
                        name
                    }
                });
            }
            return;
        }
        if (formattedDate === '') {
            setCorrectDates({
                calendar: undefined,
                input: ''
            });
        }
        setInnerDate(formattedDate);
        if (onChangeValue) {
            onChangeValue(event, formattedDate, originalDate, isoDate);
        }
        if (onChange) {
            onChange({
                target: {
                    value: formattedDate,
                    originalDate,
                    isoDate,
                    name
                }
            });
        }
    };
    const handleSearch = (date)=>{
        if (disabled || readOnly) {
            return;
        }
        if (!date) {
            setInnerDate(undefined);
            if (onCommitDate) {
                invokeOnCommitDate({
                    callback: onCommitDate,
                    value: '',
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo: undefined,
                        originalDate: undefined,
                        isoDate: ''
                    }
                });
            }
            return;
        }
        const { formattedDate, isoDate, originalDate } = getFormattedDates({
            value: date,
            lang,
            delimiter: dateFormatDelimiter,
            format,
            includeEdgeDates,
            min,
            max
        });
        if (originalDate) {
            const dateInfo = getQuarterInfo(originalDate);
            if (onCommitDate) {
                invokeOnCommitDate({
                    callback: onCommitDate,
                    value: formattedDate,
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo,
                        originalDate,
                        isoDate
                    }
                });
            }
        }
    };
    const 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
                    }
                });
            }
            if (onCommitDate) {
                invokeOnCommitDate({
                    callback: onCommitDate,
                    value: '',
                    formattedValues: {
                        error: false,
                        success: true,
                        dateInfo,
                        originalDate: date || undefined,
                        isoDate: ''
                    }
                });
            }
            return;
        }
        customDayjs.locale(lang);
        const formattedDate = customDayjs(date).format(format);
        const 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,
                    name
                }
            });
        }
        if (onCommitDate) {
            invokeOnCommitDate({
                callback: onCommitDate,
                value: formattedDate,
                formattedValues: {
                    error: false,
                    success: true,
                    dateInfo,
                    originalDate: date,
                    isoDate
                }
            });
        }
    };
    const updateExternalDate = (externalDate)=>{
        const { formattedDate, originalDate } = getFormattedDates({
            value: externalDate || null,
            lang,
            delimiter: dateFormatDelimiter,
            format,
            includeEdgeDates,
            min,
            max
        });
        setInnerDate(originalDate);
        if (originalDate) {
            setCorrectDates({
                calendar: originalDate,
                input: formattedDate
            });
        }
        if (formattedDate === '') {
            setCorrectDates({
                calendar: undefined,
                input: ''
            });
        }
    };
    return {
        datePickerErrorClass,
        datePickerSuccessClass,
        datePickerEditedClass,
        handleChangeValue,
        handleSearch,
        handleCalendarPick,
        updateExternalDate,
        getQuarterInfo
    };
};
