"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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _datepicker = _interopRequireDefault(require("../datepicker"));

var _icon = _interopRequireDefault(require("../icon"));

var _input = _interopRequireDefault(require("../input"));

var _popover = _interopRequireDefault(require("../popover"));

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

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

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

var _uiIcons = require("@teamleader/ui-icons");

var _excluded = ["className", "dayPickerProps", "footer", "inverse", "inputProps", "locale", "popoverProps", "size", "inputSize", "datePickerSize"];

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; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(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; } }

/** @type {React.ComponentType<any>} */
var DatePickerInput = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2.default)(DatePickerInput, _PureComponent);

  var _super = _createSuper(DatePickerInput);

  function DatePickerInput() {
    var _this;

    (0, _classCallCheck2.default)(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));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      isPopoverActive: false,
      popoverAnchorEl: null,
      selectedDate: null
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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 (0, _localeUtils.formatDate)(selectedDate, locale);
      }

      return customFormatDate(selectedDate, locale);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handlePopoverClose", function () {
      var onBlur = _this.props.onBlur;
      onBlur && onBlur({
        relatedTarget: null
      });

      _this.setState({
        isPopoverActive: false
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleDatePickerDateChange", function (date) {
      _this.setState({
        isPopoverActive: false,
        selectedDay: date
      }, function () {
        return _this.props.onChange(date);
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderIcon", function () {
      var inverse = _this.props.inputProps && _this.props.inputProps.inverse;
      return /*#__PURE__*/_react.default.createElement(_icon.default, {
        color: inverse ? 'teal' : 'neutral',
        tint: inverse ? 'light' : 'darkest',
        marginRight: 2
      }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconCalendarSmallOutline, null));
    });
    return _this;
  }

  (0, _createClass2.default)(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 = (0, _objectWithoutProperties2.default)(_this$props2, _excluded);
      var _this$state = this.state,
          isPopoverActive = _this$state.isPopoverActive,
          popoverAnchorEl = _this$state.popoverAnchorEl,
          selectedDate = _this$state.selectedDate;
      var boxProps = (0, _box.pickBoxProps)(others);
      var datePickerClassNames = (0, _classnames.default)(_theme.default['date-picker-input'], _theme.default["is-".concat(datePickerSize || size)]);
      return /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({
        className: className
      }, boxProps), /*#__PURE__*/_react.default.createElement(_input.default, (0, _extends2.default)({
        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.default.createElement(_popover.default, (0, _extends2.default)({
        active: isPopoverActive,
        anchorEl: popoverAnchorEl,
        backdrop: "transparent",
        maxWidth: "min-content",
        onEscKeyDown: this.handlePopoverClose,
        onOverlayClick: this.handlePopoverClose,
        position: "end",
        offsetCorrection: 30,
        returnFocusToSource: false
      }, popoverProps), /*#__PURE__*/_react.default.createElement(_box.default, {
        overflowY: "auto"
      }, /*#__PURE__*/_react.default.createElement(_datepicker.default, (0, _extends2.default)({
        className: datePickerClassNames,
        locale: locale,
        month: selectedDate,
        onChange: this.handleDatePickerDateChange,
        selectedDate: selectedDate,
        size: datePickerSize || size
      }, dayPickerProps))), footer && /*#__PURE__*/_react.default.createElement(_box.default, {
        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;
}(_react.PureComponent);

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