import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.array.iterator";
import "core-js/modules/es6.object.keys";
import "core-js/modules/es6.object.assign";

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

/**
 * @category controls
 * @component date-picker
 * @variations collab-ui-react
 */
import React from 'react';
import PropTypes from 'prop-types';
import { EventOverlay } from "./..";
import DatePickerCalendar from "./DatePickerCalendar";
import { addDays, addWeeks, isDayDisabled, isSameDay, subtractDays, subtractWeeks } from "../utils/dateUtils";
import moment from 'moment';
import { omit } from 'lodash';

var DatePicker =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(DatePicker, _React$Component);

  function DatePicker() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      anchorNode: null,
      focus: null,
      isOpen: false,
      selected: null
    };

    _this.getChildContext = function () {
      var onMonthChange = _this.props.onMonthChange;
      var _this$state = _this.state,
          focus = _this$state.focus,
          selected = _this$state.selected;
      return {
        handleDayClick: function handleDayClick(event, date) {
          return _this.handleSelect(event, date);
        },
        handleMonthChange: function handleMonthChange(event, date) {
          return onMonthChange && onMonthChange(event, date.toDate());
        },
        focus: focus,
        selected: selected
      };
    };

    _this.setOpen = function (open) {
      _this.setState({
        isOpen: open
      });
    };

    _this.handleSelect = function (event, date) {
      var shouldCloseOnSelect = _this.props.shouldCloseOnSelect;

      _this.setPreSelection(date, event);

      _this.setSelected(date, event);

      shouldCloseOnSelect && _this.setOpen(false);
    };

    _this.setSelected = function (date, event) {
      var onSelect = _this.props.onSelect;
      !isDayDisabled(date, _this.props) && _this.setState({
        selected: date
      }, function () {
        return onSelect && onSelect(event, date.toDate());
      });
    };

    _this.setPreSelection = function (date, event) {
      var onChange = _this.props.onChange;
      !isDayDisabled(date, _this.props) && _this.setState({
        focus: date
      }, function () {
        return onChange && onChange(event, date.toDate());
      });
    };

    _this.handleInputClick = function () {
      _this.setOpen(true);
    };

    _this.handleInputKeyDown = function (event) {
      var _this$state2 = _this.state,
          focus = _this$state2.focus,
          isOpen = _this$state2.isOpen;
      var flag = false;
      var copy = moment(focus);

      switch (!event.shiftKey && event.which) {
        case 32:
        case 13:
          if (!isOpen) {
            _this.handleInputClick();
          } else if (moment.isMoment(focus) || moment.isDate(focus)) {
            _this.handleSelect(event, copy);
          }

          flag = true;
          break;

        case 38:
          //up
          _this.setPreSelection(subtractWeeks(copy, 1));

          flag = true;
          break;

        case 37:
          // left
          _this.setPreSelection(subtractDays(copy, 1));

          flag = true;
          break;

        case 39:
          //right
          _this.setPreSelection(addDays(copy, 1));

          flag = true;
          break;

        case 40:
          //bottom
          _this.setPreSelection(addWeeks(copy, 1));

          flag = true;
          break;

        default:
          break;
      }

      if (flag) {
        event.stopPropagation();
        event.preventDefault();
      }
    };

    return _this;
  }

  var _proto = DatePicker.prototype;

  _proto.componentWillMount = function componentWillMount() {
    var selectedDate = moment(this.props.selectedDate);
    var isValid = selectedDate.isValid() && !isDayDisabled(selectedDate, this.props);
    isValid && this.setPreSelection(selectedDate);
    isValid && this.setSelected(selectedDate);
  };

  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    var selectedDate = moment(this.props.selectedDate);
    var prevSelectedDate = moment(prevProps.selectedDate);
    var isValid = selectedDate.isValid() && !isDayDisabled(selectedDate, this.props);

    if (isValid && !isSameDay(prevSelectedDate, selectedDate)) {
      this.setSelected(selectedDate);
      this.setPreSelection(selectedDate);
    }
  };

  _proto.render = function render() {
    var _this2 = this;

    var _this$props = this.props,
        children = _this$props.children,
        className = _this$props.className,
        direction = _this$props.direction,
        isDynamic = _this$props.isDynamic,
        showArrow = _this$props.showArrow,
        props = _objectWithoutPropertiesLoose(_this$props, ["children", "className", "direction", "isDynamic", "showArrow"]);

    var _this$state3 = this.state,
        selected = _this$state3.selected,
        focus = _this$state3.focus,
        anchorNode = _this$state3.anchorNode,
        isOpen = _this$state3.isOpen;
    var trigger = React.cloneElement(children, {
      ref: function ref(_ref) {
        return !_this2.state.anchorNode && _this2.setState({
          anchorNode: _ref
        });
      },
      onClick: this.handleInputClick,
      onKeyDown: this.handleInputKeyDown
    });
    var otherProps = omit(Object.assign({}, props), ['onSelect', 'onChange', 'onMonthChange', 'shouldCloseOnSelect']);
    var calendar = React.createElement(DatePickerCalendar, _extends({
      focus: focus,
      selected: selected
    }, otherProps));
    var content = React.createElement(EventOverlay, {
      allowClickAway: true,
      isOpen: isOpen,
      anchorNode: anchorNode,
      close: function close() {
        return _this2.setOpen(false);
      },
      direction: direction,
      showArrow: showArrow,
      isDynamic: isDynamic
    }, calendar);
    return React.createElement("div", {
      className: 'cui-datepicker-container' + ("" + (className && " " + className || ''))
    }, trigger, content);
  };

  return DatePicker;
}(React.Component);

DatePicker.displayName = 'DatePicker';
DatePicker.childContextTypes = {
  focus: PropTypes.instanceOf(moment),
  handleDayClick: PropTypes.func,
  handleMonthChange: PropTypes.func,
  selected: PropTypes.instanceOf(moment)
};
DatePicker.propTypes = {
  children: PropTypes.element,
  className: PropTypes.string,
  direction: PropTypes.string,
  filterDate: PropTypes.func,
  isDynamic: PropTypes.bool,
  locale: PropTypes.string,
  maxDate: PropTypes.instanceOf(Date),
  minDate: PropTypes.instanceOf(Date),
  monthFormat: PropTypes.string,
  nextArialLabel: PropTypes.string,
  onChange: PropTypes.func,
  onMonthChange: PropTypes.func,
  onSelect: PropTypes.func,
  previousArialLabel: PropTypes.string,
  selectedDate: PropTypes.instanceOf(Date),
  shouldCloseOnSelect: PropTypes.bool,
  showArrow: PropTypes.bool
};
DatePicker.defaultProps = {
  children: null,
  className: '',
  direction: 'bottom-center',
  filterDate: null,
  isDynamic: true,
  locale: 'en',
  maxDate: null,
  minDate: null,
  monthFormat: 'MMMM YYYY',
  nextArialLabel: 'next',
  onChange: null,
  onMonthChange: null,
  onSelect: null,
  previousArialLabel: 'previous',
  selectedDate: moment().toDate(),
  shouldCloseOnSelect: true,
  showArrow: false
};
export default DatePicker;
/**
* @name DatePicker
*
* @category controls
* @component date-picker
* @section default
*
* @js

import { Button } from '@collab-ui/react';

export default class DatePickerDefault extends React.PureComponent {
  state = {
    date: null,
  };
  render() {
    const { date } = this.state;
    return (
      <div>
        <h4 className="columns">Selected Date: {date && date.toDateString()}</h4>
        <DatePicker onSelect={(e, date) => this.setState({ date })}>
          <Button
            children='Pick a Date'
            ariaLabel='DatePicker'
          />
        </DatePicker>
      </div>
    );
  }
}
**/