"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.MIN_WIDTH = exports.DatePickerDataTids = exports.DatePicker = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));

var _Popup = require("../../internal/Popup");
var _locale = require("../../lib/locale");
var _decorators = require("../../lib/locale/decorators");
var _InternalDateGetter = require("../../lib/date/InternalDateGetter");
var _ArrowAUp16Light = require("../../internal/icons2022/ArrowAUpIcon/ArrowAUp16Light");
var _ThemeFactory = require("../../lib/theming/ThemeFactory");
var _InternalDate = require("../../lib/date/InternalDate");
var _constants = require("../../lib/date/constants");
var _types = require("../../lib/date/types");

var _DateInput = require("../DateInput");
var _filterProps = require("../../lib/filterProps");

var _CommonWrapper = require("../../internal/CommonWrapper");
var _client = require("../../lib/client");
var _NativeDateInput = require("../../internal/NativeDateInput");

var _rootNode = require("../../lib/rootNode");
var _utils = require("../../lib/utils");
var _createPropsGetter = require("../../lib/createPropsGetter");

var _Calendar = require("../Calendar");
var _ThemeContext = require("../../lib/theming/ThemeContext");

var _Button = require("../Button");
var _CalendarUtils = require("../Calendar/CalendarUtils");

var _decorator = require("../ResponsiveLayout/decorator");
var _getMenuPositions = require("../../lib/getMenuPositions");
var _ZIndex = require("../../internal/ZIndex");

var _DatePicker2 = require("./DatePicker.styles");

var _locale2 = require("./locale");
var _MobilePicker = require("./MobilePicker");var _dec, _class, _DatePicker;

var INPUT_PASS_PROPS = {
  autoFocus: true,
  disabled: true,
  warning: true,
  error: true,
  size: true,
  onKeyDown: true
};

var MIN_WIDTH = exports.MIN_WIDTH = 120;








































































var DatePickerDataTids = exports.DatePickerDataTids = {
  root: 'DatePicker__root',
  input: 'DatePicker__input',
  label: 'DatePicker__label',
  pickerRoot: 'Picker__root',
  pickerTodayWrapper: 'Picker__todayWrapper'
};



/**
 * Поле `DatePicker` помогает вводить дату с клавиатуры или выбирать ее с помощью мыши.
 *
 * Используйте поле с датой, когда нужно ввести дату в формате ДД.ММ.ГГГГ.
 *
 * Поле с датой отличается от обычного поля ввода наличием иконки, маски и блока календаря.
 */var



DatePicker = exports.DatePicker = (_dec = (0, _decorators.locale)('DatePicker', _locale2.DatePickerLocaleHelper), (0, _decorator.responsiveLayout)(_class = (0, _rootNode.rootNode)(_class = _dec(_class = (_DatePicker = /*#__PURE__*/function (_React$PureComponent) {function DatePicker() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;_this.








    getProps = (0, _createPropsGetter.createPropsGetter)(DatePicker.defaultProps);_this.


    canOpenPopup = true;_this.


























    state = { opened: false, canUseMobileNativeDatePicker: false, today: (0, _CalendarUtils.getTodayDate)() };_this.

    input = null;_this.
    focused = false;_this.
















































































    renderMain = function (props) {
      var picker = null;

      var _this$getProps = _this.getProps(),minDate = _this$getProps.minDate,maxDate = _this$getProps.maxDate,menuPos = _this$getProps.menuPos,menuAlign = _this$getProps.menuAlign;

      var isMobile = _this.isMobileLayout;

      if (_this.state.opened) {
        if (isMobile) {
          picker = /*#__PURE__*/
          _react.default.createElement(_MobilePicker.MobilePicker, {
            value: _this.props.value,
            minDate: _this.parseValueToDate(minDate),
            maxDate: _this.parseValueToDate(maxDate),
            onValueChange: _this.props.onValueChange,
            enableTodayLink: _this.props.enableTodayLink,
            isHoliday: _this.props.isHoliday,
            onCloseRequest: _this.handleMobileCloseRequest,
            renderDay: props.renderDay,
            onMonthChange: props.onMonthChange }
          );

        } else {
          picker = /*#__PURE__*/
          _react.default.createElement(_locale.LocaleContext.Provider, {
            value: {
              locale: {
                Calendar: {
                  months: _this.locale.months,
                  dayCellChooseDateAriaLabel: _this.locale.dayCellChooseDateAriaLabel,
                  selectMonthAriaLabel: _this.locale.selectMonthAriaLabel,
                  selectYearAriaLabel: _this.locale.selectYearAriaLabel,
                  selectChosenAriaLabel: _this.locale.selectChosenAriaLabel
                }
              }
            } }, /*#__PURE__*/

          _react.default.createElement(_Popup.Popup, {
            opened: true,
            hasShadow: true,
            priority: _ZIndex.ZIndex.priorities.PopupMenu,
            positions: (0, _getMenuPositions.getMenuPositions)(menuPos, menuAlign),
            "data-tid": DatePickerDataTids.root,
            anchorElement: _this.getParent(),
            margin: parseInt(_this.theme.datePickerMenuOffsetY) }, /*#__PURE__*/

          _react.default.createElement("div", {
            "data-tid": DatePickerDataTids.pickerRoot,
            className: _DatePicker2.styles.calendarWrapper(_this.theme),
            onMouseDown: function onMouseDown(e) {return e.preventDefault();} }, /*#__PURE__*/

          _react.default.createElement(_Calendar.Calendar, {
            maxDate: _this.parseValueToDate(maxDate),
            minDate: _this.parseValueToDate(minDate),
            onValueChange: _this.handleValueChange,
            isHoliday: _this.props.isHoliday,
            value: _this.parseValueToDate(_this.props.value),
            renderDay: _this.props.renderDay,
            onMonthChange: _this.props.onMonthChange }
          ),
          _this.props.enableTodayLink && _this.renderTodayLink(), ' '
          )
          )
          );

        }
      }
      return /*#__PURE__*/(
        _react.default.createElement("label", {
          className: _DatePicker2.styles.root(),
          style: _this.getRootStyle(),
          onMouseEnter: _this.props.onMouseEnter,
          onMouseLeave: _this.props.onMouseLeave,
          onMouseOver: _this.props.onMouseOver,
          "data-tid": DatePickerDataTids.label }, /*#__PURE__*/

        _react.default.createElement(_DateInput.DateInput, (0, _extends2.default)({
          id: _this.props.id },
        (0, _filterProps.filterProps)(props, INPUT_PASS_PROPS), {
          ref: _this.getInputRef,
          value: _this.props.value || '',
          width: "100%",
          withIcon: true,
          minDate: minDate,
          maxDate: maxDate,
          onBlur: isMobile ? undefined : _this.handleBlur,
          onFocus: _this.handleFocus,
          onValueChange: _this.props.onValueChange,
          onClick: _this.openPickerPopup,
          onKeyDown: _this.handleKeyDown,
          "data-tid": DatePickerDataTids.input })
        ),
        _this.state.canUseMobileNativeDatePicker ? /*#__PURE__*/
        _react.default.createElement(_NativeDateInput.NativeDateInput, {
          onValueChange: _this.props.onValueChange,
          value: _this.props.value || '',
          minDate: minDate,
          maxDate: maxDate,
          disabled: _this.props.disabled }
        ) :

        picker

        ));

    };_this.





































    getParent = function () {
      return (0, _rootNode.getRootNode)(_this);
    };_this.

    getRootStyle = function () {
      var width = _this.props.width;
      return (0, _utils.isNonNullable)(width) ? { width: width } : { minWidth: MIN_WIDTH };
    };_this.

    getInputRef = function (ref) {
      _this.input = ref;
    };_this.

    openPickerPopup = function () {
      _this.setState({ opened: true });
    };_this.

    handleFocus = function () {
      if (_this.focused) {
        return;
      }

      if (!_this.canOpenPopup) {
        _this.canOpenPopup = true;
      } else {
        _this.openPickerPopup();
      }

      _this.focused = true;

      if (_this.props.onFocus) {
        _this.props.onFocus();
      }
    };_this.

    handleKeyDown = function (e) {
      if (!_this.state.opened) {
        _this.openPickerPopup();
      }

      if (_this.props.onKeyDown) {
        _this.props.onKeyDown(e);
      }
    };_this.

    handleBlur = function () {
      if (!_this.focused) {
        return;
      }

      _this.focused = false;
      _this.close();

      if (_this.props.onBlur) {
        _this.props.onBlur();
      }
    };_this.

    handleValueChange = function (value) {
      _this.handleSelect(value);
      _this.blur();
    };_this.

    handleSelect = function (value) {
      if (!value) {
        return null;
      }

      if (_this.props.onValueChange) {
        _this.props.onValueChange(value);
      }
    };_this.

    handleMobileCloseRequest = function () {
      _this.close();
    };return _this;}(0, _inheritsLoose2.default)(DatePicker, _React$PureComponent);var _proto = DatePicker.prototype;_proto.componentDidMount = function componentDidMount() {if (this.props.useMobileNativeDatePicker && _client.isMobile) {this.setState({ canUseMobileNativeDatePicker: true });}if (this.props.autoFocus) {this.focus();}};_proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {var disabled = this.props.disabled;var opened = this.state.opened;if (disabled && opened) {this.close();}if (prevState.opened && !opened && this.isMobileLayout) {this.handleBlur();}} /**
   * @public
   */;_proto.blur = function blur() {if (this.input) {this.input.blur();}this.handleBlur();} /**
   * @public
   */;_proto.focus = function focus(opts) {if (this.props.disabled) {return;}if (opts != null && opts.withoutOpenDropdown) {this.canOpenPopup = false;}if (this.input) {this.input.focus();}} /**
   * Закрывает выпадашку выбора дня
   * @public
   */;_proto.close = function close() {this.setState({ opened: false });};_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Provider, { value: _ThemeFactory.ThemeFactory.create({ calendarBottomSeparatorBorder: 'none' }, theme) }, /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: _this2.setRootNode }, _this2.getProps()), _this2.renderMain));});};_proto.parseValueToDate = function parseValueToDate(value) {if (value === undefined || value === null) {return undefined;}var date = new _InternalDate.InternalDate({ value: value });if (date.validate({ checks: [_types.InternalDateValidateCheck.NotNull, _types.InternalDateValidateCheck.Native] })) {return date.toString({ withPad: true });}return undefined;};_proto.renderTodayLink = function renderTodayLink() {var _this3 = this;var _this$locale = this.locale,order = _this$locale.order,separator = _this$locale.separator;var today = new _InternalDate.InternalDate({ order: order, separator: separator }).setComponents(_InternalDateGetter.InternalDateGetter.getTodayComponents()).toString({ withPad: true, withSeparator: true });return /*#__PURE__*/_react.default.createElement("div", { style: { margin: 8 } }, /*#__PURE__*/_react.default.createElement(_Button.Button, { "aria-label": this.locale.todayAriaLabel, "data-tid": DatePickerDataTids.pickerTodayWrapper, width: "100%", onClick: function onClick() {_this3.handleSelect(today);}, icon: /*#__PURE__*/_react.default.createElement(_ArrowAUp16Light.ArrowAUpIcon16Light, null) }, this.locale.today));};return DatePicker;}(_react.default.PureComponent), _DatePicker.__KONTUR_REACT_UI__ = 'DatePicker', _DatePicker.displayName = 'DatePicker', _DatePicker.defaultProps = { minDate: _constants.MIN_FULLDATE, maxDate: _constants.MAX_FULLDATE }, _DatePicker.validate = function (value, range) {if (range === void 0) {range = {};}if (!value) {return false;}var _range = range,_range$minDate = _range.minDate,minDate = _range$minDate === void 0 ? _constants.MIN_FULLDATE : _range$minDate,_range$maxDate = _range.maxDate,maxDate = _range$maxDate === void 0 ? _constants.MAX_FULLDATE : _range$maxDate;var internalDate = new _InternalDate.InternalDate({ order: _types.InternalDateOrder.DMY, separator: _types.InternalDateSeparator.Dot }).setRangeStart(new _InternalDate.InternalDate({ value: minDate })).setRangeEnd(new _InternalDate.InternalDate({ value: maxDate })).parseValue(value);return internalDate.validate({ checks: [_types.InternalDateValidateCheck.NotNull, _types.InternalDateValidateCheck.Number, _types.InternalDateValidateCheck.Native, _types.InternalDateValidateCheck.Limits, _types.InternalDateValidateCheck.Range] });}, _DatePicker)) || _class) || _class) || _class);