'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var clsx = require('clsx');
var React = require('react');
var Calendar = require('../calendar/Calendar.js');
require('../common/theme.js');
require('../common/direction.js');
require('@transferwise/neptune-tokens');
require('../common/propsValues/control.js');
var size = require('../common/propsValues/size.js');
require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
var monthFormat = require('../common/propsValues/monthFormat.js');
var position = require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
require('@transferwise/formatting');
require('@transferwise/icons');
require('react-intl');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var ResponsivePanel = require('../common/responsivePanel/ResponsivePanel.js');
var contexts = require('../inputs/contexts.js');
var OverlayIdProvider = require('../provider/overlay/OverlayIdProvider.js');
var DateTrigger = require('./dateTrigger/DateTrigger.js');

class DateLookup extends React.PureComponent {
  static defaultProps = {
    value: null,
    min: null,
    max: null,
    size: size.Size.MEDIUM,
    placeholder: '',
    label: '',
    monthFormat: monthFormat.MonthFormat.LONG,
    disabled: false,
    clearable: false
  };
  element = /*#__PURE__*/React.createRef();
  constructor(props) {
    super(props);
    this.state = {
      originalDate: null,
      open: false
    };
  }
  componentDidUpdate(previousProps) {
    if (this.props.value?.getTime() !== previousProps.value?.getTime() && this.state.open) {
      this.focusOn('.active');
    }
  }
  open = () => {
    const {
      onFocus
    } = this.props;
    this.setState({
      open: true
    });
    if (onFocus) {
      onFocus();
    }
  };
  discard = () => {
    const {
      originalDate
    } = this.state;
    if (originalDate !== null) {
      this.props.onChange(originalDate);
    }
    this.close();
  };
  close = () => {
    const {
      onBlur
    } = this.props;
    this.setState({
      open: false,
      originalDate: null
    });
    if (onBlur) {
      onBlur();
    }
  };
  handleKeyDown = event => {
    const {
      open,
      originalDate
    } = this.state;
    switch (event.key) {
      case 'ArrowLeft':
      case 'ArrowUp':
      case 'ArrowRight':
      case 'ArrowDown':
        if (!open) {
          this.open();
          event.preventDefault();
        }
        break;
      case 'Escape':
        if (originalDate !== null) {
          this.props.onChange(originalDate);
        }
        this.close();
        event.preventDefault();
        break;
    }
  };
  focusOn = (preferredElement, fallbackElement) => {
    const element = this.element.current?.querySelector(preferredElement);
    if (element) {
      element.focus();
    } else if (fallbackElement) {
      this.focusOn(fallbackElement);
    }
  };
  handleDateChange = date => {
    this.setState(state => {
      const originalDate = state.originalDate ?? this.props.value;
      return {
        originalDate
      };
    }, () => {
      this.props.onChange(date);
    });
  };
  handleDateSelect = date => {
    this.props.onChange(date);
    this.close();
    this.focusOn('.btn');
  };
  getCalendar = () => {
    const {
      value,
      min,
      max,
      monthFormat,
      placeholder
    } = this.props;
    return /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "p-a-1",
      children: /*#__PURE__*/jsxRuntime.jsx(Calendar.default, {
        value: value,
        min: min,
        max: max,
        monthFormat: monthFormat,
        placeholder: placeholder,
        onChange: this.handleDateChange,
        onSelect: this.handleDateSelect
      })
    });
  };
  handleClear = () => {
    this.props.onChange(null);
    this.focusOn('.np-date-trigger');
  };
  render() {
    const {
      open
    } = this.state;
    const {
      inputAttributes,
      id: idProp,
      'aria-labelledby': ariaLabelledByProp,
      size,
      placeholder,
      label,
      monthFormat,
      disabled,
      clearable,
      value
    } = this.props;
    const id = idProp ?? inputAttributes?.id;
    const ariaLabelledBy = ariaLabelledByProp ?? inputAttributes?.['aria-labelledby'];
    return /*#__PURE__*/jsxRuntime.jsx("div", {
      ref: this.element,
      "aria-labelledby": id,
      "aria-invalid": inputAttributes?.['aria-invalid'],
      "aria-describedby": inputAttributes?.['aria-describedby'],
      className: clsx.clsx('input-group', disabled && 'disabled'),
      onKeyDown: this.handleKeyDown,
      children: /*#__PURE__*/jsxRuntime.jsxs(OverlayIdProvider.OverlayIdProvider, {
        open: open,
        children: [/*#__PURE__*/jsxRuntime.jsx(DateTrigger.default, {
          id: id,
          ariaLabelledBy: ariaLabelledBy,
          selectedDate: value,
          size: size,
          placeholder: placeholder,
          label: label,
          monthFormat: monthFormat,
          disabled: disabled || false,
          onClick: this.open,
          onClear: !disabled && clearable && value ? this.handleClear : undefined
        }), /*#__PURE__*/jsxRuntime.jsx(ResponsivePanel.default, {
          anchorRef: this.element,
          open: open,
          className: "tw-date-lookup-menu",
          position: position.Position.BOTTOM,
          considerHeight: true,
          onClose: this.discard,
          children: this.getCalendar()
        })]
      })
    });
  }
}
const WrappedDateLookup = contexts.withInputAttributes(DateLookup, {
  nonLabelable: true
});
WrappedDateLookup.displayName = 'DateLookup';

exports.default = WrappedDateLookup;
//# sourceMappingURL=DateLookup.js.map
