import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["bordered", "className", "style", "modifiers", "size", "withMonthPicker", "showWeekNumbers"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import DayPicker from 'react-day-picker';
import Box, { omitBoxProps, pickBoxProps } from '../box';
import NavigationBar from './NavigationBar';
import WeekDay from './WeekDay';
import MonthPicker from './MonthPicker';
import { convertModifiersToClassnames } from './utils';
import cx from 'classnames';
import theme from './theme.css';
import uiUtilities from '@teamleader/ui-utilities';
import LocaleUtils from './localeUtils';
/** @type {React.ComponentType<any>} */

var DatePicker = /*#__PURE__*/function (_PureComponent) {
  _inherits(DatePicker, _PureComponent);

  var _super = _createSuper(DatePicker);

  function DatePicker() {
    var _this;

    _classCallCheck(this, DatePicker);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "state", {
      selectedDate: null,
      selectedMonth: null
    });

    _defineProperty(_assertThisInitialized(_this), "handleDayClick", function (day) {
      var modifiers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

      if (modifiers[theme['disabled']]) {
        return;
      }

      _this.setState({
        selectedDate: day
      }, function () {
        return _this.props.onChange(day);
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleYearMonthChange", function (selectedMonth) {
      _this.setState({
        selectedMonth: selectedMonth
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getMonthPickerSize", function () {
      var _this$props = _this.props,
          showWeekNumbers = _this$props.showWeekNumbers,
          size = _this$props.size;
      var monthPickerSizeByDatePickerSize = {
        small: 'smallest',
        medium: showWeekNumbers ? 'medium' : 'small',
        large: 'large'
      };
      return monthPickerSizeByDatePickerSize[size];
    });

    return _this;
  }

  _createClass(DatePicker, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          bordered = _this$props2.bordered,
          className = _this$props2.className,
          style = _this$props2.style,
          modifiers = _this$props2.modifiers,
          size = _this$props2.size,
          withMonthPicker = _this$props2.withMonthPicker,
          showWeekNumbers = _this$props2.showWeekNumbers,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var _this$state = this.state,
          selectedDate = _this$state.selectedDate,
          selectedMonth = _this$state.selectedMonth;
      var boxProps = pickBoxProps(others);
      var restProps = omitBoxProps(others);
      var classNames = cx(uiUtilities['reset-font-smoothing'], theme['date-picker'], theme["is-".concat(size)], _defineProperty({}, theme['is-bordered'], bordered), className);
      return /*#__PURE__*/React.createElement(Box, boxProps, /*#__PURE__*/React.createElement(DayPicker, _extends({}, restProps, {
        localeUtils: LocaleUtils,
        initialMonth: selectedDate,
        month: selectedMonth,
        className: classNames,
        style: style,
        classNames: theme,
        modifiers: convertModifiersToClassnames(modifiers, theme),
        navbarElement: /*#__PURE__*/React.createElement(NavigationBar, {
          size: size,
          withMonthPicker: withMonthPicker
        }),
        onDayClick: this.handleDayClick,
        selectedDays: selectedDate,
        weekdayElement: /*#__PURE__*/React.createElement(WeekDay, {
          size: size
        }),
        showWeekNumbers: showWeekNumbers,
        captionElement: withMonthPicker ? function (_ref) {
          var date = _ref.date,
              locale = _ref.locale,
              localeUtils = _ref.localeUtils;
          return /*#__PURE__*/React.createElement(MonthPicker, {
            date: date,
            locale: locale,
            localeUtils: localeUtils,
            onChange: _this2.handleYearMonthChange,
            size: _this2.getMonthPickerSize()
          });
        } : undefined
      })));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (props.selectedDate !== undefined && props.selectedDate !== state.selectedDate) {
        return {
          selectedDate: props.selectedDate
        };
      }

      return null;
    }
  }]);

  return DatePicker;
}(PureComponent);

DatePicker.defaultProps = {
  bordered: true,
  size: 'medium'
};
export default DatePicker;