"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _reactDayPicker = _interopRequireDefault(require("react-day-picker"));

var _box = _interopRequireWildcard(require("../box"));

var _NavigationBar = _interopRequireDefault(require("./NavigationBar"));

var _WeekDay = _interopRequireDefault(require("./WeekDay"));

var _MonthPicker = _interopRequireDefault(require("./MonthPicker"));

var _utils = require("./utils");

var _classnames = _interopRequireDefault(require("classnames"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _uiUtilities = _interopRequireDefault(require("@teamleader/ui-utilities"));

var _localeUtils = _interopRequireDefault(require("./localeUtils"));

var _excluded = ["bordered", "className", "modifiers", "size", "withMonthPicker", "showWeekNumbers", "onChange"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var DatePicker = function DatePicker(_ref) {
  var _ref$bordered = _ref.bordered,
      bordered = _ref$bordered === void 0 ? true : _ref$bordered,
      className = _ref.className,
      modifiers = _ref.modifiers,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      withMonthPicker = _ref.withMonthPicker,
      showWeekNumbers = _ref.showWeekNumbers,
      onChange = _ref.onChange,
      others = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useState = (0, _react.useState)(others.selectedDate),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      selectedDate = _useState2[0],
      setSelectedDate = _useState2[1];

  var _useState3 = (0, _react.useState)(),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      selectedMonth = _useState4[0],
      setSelectedMonth = _useState4[1];

  var handleDayClick = function handleDayClick(day, modifiers) {
    if (modifiers[_theme.default['disabled']]) {
      return;
    }

    setSelectedDate(day);
    onChange && onChange(day);
  };

  var handleYearMonthChange = function handleYearMonthChange(selectedMonth) {
    setSelectedMonth(selectedMonth);
  };

  var getMonthPickerSize = function getMonthPickerSize() {
    var monthPickerSizeByDatePickerSize = {
      small: 'smallest',
      medium: showWeekNumbers ? 'medium' : 'small',
      large: 'large'
    };
    return monthPickerSizeByDatePickerSize[size];
  };

  var classNames = (0, _classnames.default)(_uiUtilities.default['reset-font-smoothing'], _theme.default['date-picker'], _theme.default["is-".concat(size)], (0, _defineProperty2.default)({}, _theme.default['is-bordered'], bordered), className);
  return /*#__PURE__*/_react.default.createElement(_box.default, (0, _box.pickBoxProps)(others), /*#__PURE__*/_react.default.createElement(_reactDayPicker.default, (0, _extends2.default)({}, others, {
    localeUtils: _localeUtils.default,
    initialMonth: others.selectedDate,
    month: selectedMonth,
    className: classNames,
    classNames: _theme.default,
    modifiers: modifiers && (0, _utils.convertModifiersToClassnames)(modifiers, _theme.default),
    navbarElement: function navbarElement(_ref2) {
      var props = (0, _extends2.default)({}, _ref2);
      return /*#__PURE__*/_react.default.createElement(_NavigationBar.default, (0, _extends2.default)({}, props, {
        size: size,
        withMonthPicker: withMonthPicker
      }));
    },
    onDayClick: handleDayClick,
    selectedDays: selectedDate,
    weekdayElement: function weekdayElement(_ref3) {
      var props = (0, _extends2.default)({}, _ref3);
      return /*#__PURE__*/_react.default.createElement(_WeekDay.default, (0, _extends2.default)({}, props, {
        size: size
      }));
    },
    showWeekNumbers: showWeekNumbers,
    captionElement: withMonthPicker ? function (_ref4) {
      var date = _ref4.date,
          locale = _ref4.locale,
          localeUtils = _ref4.localeUtils;
      return /*#__PURE__*/_react.default.createElement(_MonthPicker.default, {
        date: date,
        locale: locale,
        localeUtils: localeUtils,
        onChange: handleYearMonthChange,
        size: getMonthPickerSize()
      });
    } : undefined
  })));
};

var _default = DatePicker;
exports.default = _default;