"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DatePicker = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactDatetime = _interopRequireDefault(require("react-datetime"));
var _moment = _interopRequireDefault(require("moment"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faCalendar = require("@fortawesome/free-regular-svg-icons/faCalendar");
var _input = require("../input");
var _types = require("./types");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** Wrapper around react-datetime package. Original props from https://github.com/arqex/react-datetime#api are supported
 * @midnight-gray-supported
 * @alpine-light-supported
 * */
class DatePicker extends _react.default.Component {
  static defaultProps = {
    ..._types.DefaultProps,
    startDate: new Date(-8640000000000000),
    // Min allowed Date
    endDate: new Date(8640000000000000),
    // Max allowed Date
    placement: _types.DATE_PICKER_PLACEMENTS.BOTTOM
  };

  // Datetime doesn't provide typings for this function
  isDateEnabled = currentDate => {
    const {
      startDate,
      endDate,
      utc
    } = this.props;
    if (utc) {
      const startDateUtc = _moment.default.utc(startDate);
      const endDateUtc = _moment.default.utc(endDate);
      return !currentDate.startOf('day').isBefore(startDateUtc.startOf('day')) && !currentDate.startOf('day').isAfter(endDateUtc.startOf('day'));
    }
    return !currentDate.endOf('day').isBefore((0, _moment.default)(startDate).startOf('day')) && !currentDate.startOf('day').isAfter((0, _moment.default)(endDate).endOf('day'));
  };

  // Datetime doesn't provide typings for this function
  renderInput = renderProps => {
    const {
      inputNode,
      onClose,
      validity,
      autoFocus
    } = this.props;
    const inputProps = {
      ...renderProps,
      onBlur: onClose,
      autoFocus,
      validity
    };
    return inputNode && /*#__PURE__*/_react.default.createElement(inputNode, inputProps) || /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "datetime-picker-input",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
        type: "text",
        autoFocus: autoFocus,
        rightIcon: _faCalendar.faCalendar,
        ...inputProps
      })
    });
  };
  render() {
    const {
      showInput,
      className,
      placement
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactDatetime.default, {
      renderInput: this.renderInput,
      isValidDate: this.isDateEnabled,
      ...this.props,
      input: showInput,
      className: (0, _classnames.default)(className, `rdt-open-${placement}`)
    });
  }
}
exports.DatePicker = DatePicker;