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 = ["className", "dayPickerProps", "footer", "inverse", "inputProps", "locale", "popoverProps", "size", "inputSize", "datePickerSize"];

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 Box, { pickBoxProps } from '../box';
import DatePicker from '../datepicker';
import Icon from '../icon';
import Input from '../input';
import Popover from '../popover';
import cx from 'classnames';
import theme from './theme.css';
import { formatDate } from './localeUtils';
import { IconCalendarSmallOutline } from '@teamleader/ui-icons';
/** @type {React.ComponentType<any>} */

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

  var _super = _createSuper(DatePickerInput);

  function DatePickerInput() {
    var _this;

    _classCallCheck(this, DatePickerInput);

    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", {
      isPopoverActive: false,
      popoverAnchorEl: null,
      selectedDate: null
    });

    _defineProperty(_assertThisInitialized(_this), "getFormattedDate", function () {
      var _this$props = _this.props,
          customFormatDate = _this$props.formatDate,
          locale = _this$props.locale;
      var selectedDate = _this.state.selectedDate;

      if (!selectedDate) {
        return '';
      }

      if (!customFormatDate) {
        return formatDate(selectedDate, locale);
      }

      return customFormatDate(selectedDate, locale);
    });

    _defineProperty(_assertThisInitialized(_this), "handleInputFocus", function (event) {
      var _this$props$inputProp = _this.props.inputProps,
          onFocus = _this$props$inputProp.onFocus,
          readOnly = _this$props$inputProp.readOnly;
      var openPickerOnFocus = _this.props.openPickerOnFocus;

      if (readOnly) {
        return;
      }

      if (openPickerOnFocus) {
        _this.setState({
          popoverAnchorEl: event.currentTarget,
          isPopoverActive: true
        }, function () {
          return onFocus && onFocus(event);
        });
      } else {
        onFocus && onFocus(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleInputClick", function (event) {
      var _this$props$inputProp2 = _this.props.inputProps,
          onFocus = _this$props$inputProp2.onFocus,
          onClick = _this$props$inputProp2.onClick;
      var openPickerOnFocus = _this.props.openPickerOnFocus;

      if (!openPickerOnFocus) {
        _this.setState({
          popoverAnchorEl: event.currentTarget,
          isPopoverActive: true
        }, function () {
          return onFocus && onFocus();
        });
      }

      onClick && onClick(event);
    });

    _defineProperty(_assertThisInitialized(_this), "handlePopoverClose", function () {
      var onBlur = _this.props.onBlur;
      onBlur && onBlur({
        relatedTarget: null
      });

      _this.setState({
        isPopoverActive: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleDatePickerDateChange", function (date) {
      _this.setState({
        isPopoverActive: false,
        selectedDay: date
      }, function () {
        return _this.props.onChange(date);
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderIcon", function () {
      var inverse = _this.props.inputProps && _this.props.inputProps.inverse;
      return /*#__PURE__*/React.createElement(Icon, {
        color: inverse ? 'teal' : 'neutral',
        tint: inverse ? 'light' : 'darkest',
        marginRight: 2
      }, /*#__PURE__*/React.createElement(IconCalendarSmallOutline, null));
    });

    return _this;
  }

  _createClass(DatePickerInput, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          className = _this$props2.className,
          dayPickerProps = _this$props2.dayPickerProps,
          footer = _this$props2.footer,
          inverse = _this$props2.inverse,
          inputProps = _this$props2.inputProps,
          locale = _this$props2.locale,
          popoverProps = _this$props2.popoverProps,
          size = _this$props2.size,
          inputSize = _this$props2.inputSize,
          datePickerSize = _this$props2.datePickerSize,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var _this$state = this.state,
          isPopoverActive = _this$state.isPopoverActive,
          popoverAnchorEl = _this$state.popoverAnchorEl,
          selectedDate = _this$state.selectedDate;
      var boxProps = pickBoxProps(others);
      var datePickerClassNames = cx(theme['date-picker-input'], theme["is-".concat(datePickerSize || size)]);
      return /*#__PURE__*/React.createElement(Box, _extends({
        className: className
      }, boxProps), /*#__PURE__*/React.createElement(Input, _extends({
        inverse: inverse,
        prefix: this.renderIcon(),
        size: inputSize || size,
        value: this.getFormattedDate(),
        width: "120px",
        noInputStyling: dayPickerProps && dayPickerProps.withMonthPicker
      }, inputProps, {
        onClick: this.handleInputClick,
        onFocus: this.handleInputFocus
      })), /*#__PURE__*/React.createElement(Popover, _extends({
        active: isPopoverActive,
        anchorEl: popoverAnchorEl,
        backdrop: "transparent",
        maxWidth: "min-content",
        onEscKeyDown: this.handlePopoverClose,
        onOverlayClick: this.handlePopoverClose,
        position: "end",
        offsetCorrection: 30,
        returnFocusToSource: false
      }, popoverProps), /*#__PURE__*/React.createElement(Box, {
        overflowY: "auto"
      }, /*#__PURE__*/React.createElement(DatePicker, _extends({
        className: datePickerClassNames,
        locale: locale,
        month: selectedDate,
        onChange: this.handleDatePickerDateChange,
        selectedDate: selectedDate,
        size: datePickerSize || size
      }, dayPickerProps))), footer && /*#__PURE__*/React.createElement(Box, {
        backgroundColor: "neutral",
        backgroundTint: "light",
        borderColor: "neutral",
        borderTint: "normal",
        borderTopWidth: 1,
        flex: "1 0 auto",
        overflowX: "hidden",
        paddingHorizontal: 3,
        paddingVertical: 3,
        tabIndex: "0"
      }, footer)));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (props.selectedDate !== undefined && props.selectedDate !== state.selectedDate) {
        return {
          selectedDate: props.selectedDate
        };
      }

      return null;
    }
  }]);

  return DatePickerInput;
}(PureComponent);

DatePickerInput.defaultProps = {
  dayPickerProps: {},
  inputProps: {},
  inverse: false,
  locale: 'en-GB',
  popoverProps: {},
  size: 'medium',
  openPickerOnFocus: true
};
export default DatePickerInput;