import React from 'react';
import Datetime from 'react-datetime';
import moment from 'moment';
import classNames from 'classnames';
import { faCalendar } from '@fortawesome/free-regular-svg-icons/faCalendar';
import { Input } from '../input';
import { DefaultProps, DATE_PICKER_PLACEMENTS } from './types';
import { jsx as _jsx } from "react/jsx-runtime";
/** Wrapper around react-datetime package. Original props from https://github.com/arqex/react-datetime#api are supported
 * @midnight-gray-supported
 * @alpine-light-supported
 * */
export class DatePicker extends React.Component {
  static defaultProps = {
    ...DefaultProps,
    startDate: new Date(-8640000000000000),
    // Min allowed Date
    endDate: new Date(8640000000000000),
    // Max allowed Date
    placement: 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.utc(startDate);
      const endDateUtc = moment.utc(endDate);
      return !currentDate.startOf('day').isBefore(startDateUtc.startOf('day')) && !currentDate.startOf('day').isAfter(endDateUtc.startOf('day'));
    }
    return !currentDate.endOf('day').isBefore(moment(startDate).startOf('day')) && !currentDate.startOf('day').isAfter(moment(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.createElement(inputNode, inputProps) || /*#__PURE__*/_jsx("div", {
      className: "datetime-picker-input",
      children: /*#__PURE__*/_jsx(Input, {
        type: "text",
        autoFocus: autoFocus,
        rightIcon: faCalendar,
        ...inputProps
      })
    });
  };
  render() {
    const {
      showInput,
      className,
      placement
    } = this.props;
    return /*#__PURE__*/_jsx(Datetime, {
      renderInput: this.renderInput,
      isValidDate: this.isDateEnabled,
      ...this.props,
      input: showInput,
      className: classNames(className, `rdt-open-${placement}`)
    });
  }
}