import { customDayjs } from '../../../utils/datejs.js';
import { classes } from '../DatePicker.tokens.js';
import { getMaskedDateOnInput } from '../utils/getMaskedDateOnInput.js';
import { getFormattedDates } from '../utils/getFormattedDates.js';
import { invokeOnCommitDate } from '../utils/invokeOnCommitDate.js';
import { QUARTER_NAMES } from '../../Calendar/utils/constants.js';

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

export { useDatePicker };
