import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["isToday", "isSelected", "isDisabled", "isWeekend", "date", "onDayClick", "onClick", "children", "className"];
import React, { useContext, memo, useCallback } from 'react';
import { useLocaleForControl } from "../../../lib/locale/useLocaleForControl";
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { cx } from "../../../lib/theming/Emotion";
import { DatePickerLocaleHelper } from "../../DatePicker/locale";
import { InternalDate } from "../../../lib/date/InternalDate";
import { LocaleContext } from "../../../lib/locale";
import { getVisualStateDataAttributes } from "../../../internal/CommonWrapper/utils/getVisualStateDataAttributes";
import { forwardRefAndName } from "../../../lib/forwardRefAndName";
import { styles } from "../DayCellView.styles";
import { CalendarDataTids } from "../Calendar";

/**
 * Компонент дня `CalendarDay` из Calendar.
 *
 * @visibleName Calendar.Day
 */
export var CalendarDay = /*#__PURE__*/memo(forwardRefAndName('CalendarDay', function CalendarDay(_ref, ref) {
  var _cx, _cx2;
  var isToday = _ref.isToday,
    isSelected = _ref.isSelected,
    isDisabled = _ref.isDisabled,
    isWeekend = _ref.isWeekend,
    date = _ref.date,
    onDayClick = _ref.onDayClick,
    onClick = _ref.onClick,
    children = _ref.children,
    className = _ref.className,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var theme = useContext(ThemeContext);
  var _useContext = useContext(LocaleContext),
    langCode = _useContext.langCode;
  var internalDate = new InternalDate({
    langCode: langCode,
    value: date
  });
  var locale = useLocaleForControl('Calendar', DatePickerLocaleHelper);
  var ariaLabel = locale.dayCellChooseDateAriaLabel + ": " + internalDate.toA11YFormat();
  var handleClick = useCallback(function (e) {
    onDayClick();
    onClick == null || onClick(e);
  }, [onDayClick, onClick]);
  var _internalDate$getComp = internalDate.getComponentsLikeNumber(),
    day = _internalDate$getComp.date;
  var caption = children != null ? children : day;
  return /*#__PURE__*/(
    React.createElement("button", _extends({
      ref: ref,
      "data-tid": CalendarDataTids.dayCell,
      "aria-label": ariaLabel,
      tabIndex: -1,
      disabled: isDisabled,
      onClick: handleClick,
      className: cx((_cx = {}, _cx[styles.day(theme)] = true, _cx[styles.selected(theme)] = isSelected, _cx[styles.weekend(theme)] = isWeekend, _cx), className)
    }, getVisualStateDataAttributes({
      selected: isSelected
    }), rest), /*#__PURE__*/

    React.createElement("span", {
      className: cx((_cx2 = {}, _cx2[styles.todayCaption(theme)] = isToday, _cx2))
    }, caption))
  );
}));