var _excluded = ["onSelect"],
  _excluded2 = ["beforeHide", "beforeShow", "afterHide", "afterShow", "disabledDate", "onChange", "onClear", "value"];
var _process$env$TZ_LOG;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import { Calendar as CalendarBase } from '../Calendar/Calendar';
import { Card } from '../Card/Card';
import { DateInput, isValidYearRange } from '../DateInput/DateInput';
import { OverlayTrigger, useOverlayTriggerContext } from '../OverlayTrigger/OverlayTrigger';
import { isEventSource } from '../_hooks/ClickOutside';
import { useDateTime } from '../_hooks/DateTime';
var enableLogs = ((_process$env$TZ_LOG = process.env.TZ_LOG) === null || _process$env$TZ_LOG === void 0 ? void 0 : _process$env$TZ_LOG.toLowerCase()) === 'true';
function consoleMessageChange(_ref) {
  var date = _ref.date;
  if (enableLogs) {
    console.log("core-react: DateInput onChange, relative start of day in destination time zone.\n", date);
  }
}
var showKeys = [];
var hideKeys = {
  target: ['Esc', 'Escape', 'Enter'],
  overlay: ['Esc', 'Escape']
};
function lifecycleDefault() {
  return true;
}
function noop() {}
var Calendar = /*#__PURE__*/React.forwardRef(function Calendar(_ref2, ref) {
  var _onSelect = _ref2.onSelect,
    props = _objectWithoutProperties(_ref2, _excluded);
  var _useOverlayTriggerCon = useOverlayTriggerContext(),
    hide = _useOverlayTriggerCon.hide;
  return /*#__PURE__*/React.createElement(Card, {
    ref: ref,
    shadowStrength: 2
  }, /*#__PURE__*/React.createElement(CalendarBase, _extends({}, props, {
    onSelect: function onSelect(date) {
      _onSelect === null || _onSelect === void 0 ? void 0 : _onSelect(date);
      hide(true);
    }
  })));
});

/**

 We use date selects to allow users to enter a date into a form by either
 selecting it from a calendar or typing it into a date field.

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-dateselect--demo)

 @see [Design Guidelines](https://design.procore.com/date-select)

 */
export var DateSelect = /*#__PURE__*/React.forwardRef(function DateSelect(_ref3, ref) {
  var _ref3$beforeHide = _ref3.beforeHide,
    _beforeHide = _ref3$beforeHide === void 0 ? lifecycleDefault : _ref3$beforeHide,
    _ref3$beforeShow = _ref3.beforeShow,
    _beforeShow = _ref3$beforeShow === void 0 ? lifecycleDefault : _ref3$beforeShow,
    _ref3$afterHide = _ref3.afterHide,
    afterHide = _ref3$afterHide === void 0 ? noop : _ref3$afterHide,
    _ref3$afterShow = _ref3.afterShow,
    afterShow = _ref3$afterShow === void 0 ? noop : _ref3$afterShow,
    disabledDate = _ref3.disabledDate,
    _ref3$onChange = _ref3.onChange,
    _onChange = _ref3$onChange === void 0 ? noop : _ref3$onChange,
    _ref3$onClear = _ref3.onClear,
    onClear = _ref3$onClear === void 0 ? noop : _ref3$onClear,
    value = _ref3.value,
    props = _objectWithoutProperties(_ref3, _excluded2);
  var dateTime = useDateTime();
  var clearRef = React.useRef(null);
  var dateInputRef = ref;
  React.useRef(null);
  var monthRef = React.useRef(null);
  var yearRef = React.useRef(null);
  var segmentRefs = {
    segmentThree: React.useRef(null)
  };
  var _React$useState = React.useState(value),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    selected = _React$useState2[0],
    setSelected = _React$useState2[1];
  var _React$useState3 = React.useState(value || dateTime.newDate()),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    displayed = _React$useState4[0],
    setDisplayed = _React$useState4[1];
  React.useEffect(function () {
    setSelected(value);
    setDisplayed(value || dateTime.newDate());
  }, [value]);
  var overlay = /*#__PURE__*/React.createElement(Calendar, {
    "data-qa": "core-date-select-calendar",
    disabledDate: disabledDate,
    displayDate: displayed,
    monthRef: monthRef,
    onNavigate: setDisplayed,
    onSelect: function onSelect(date) {
      segmentRefs.segmentThree.current && segmentRefs.segmentThree.current.focus();
      setDisplayed(date);
      setSelected(date);
      _onChange(date);
    },
    selectedStart: selected,
    selectedEnd: selected,
    yearRef: yearRef
  });
  return /*#__PURE__*/React.createElement(OverlayTrigger, {
    afterHide: afterHide,
    afterShow: afterShow,
    beforeHide: function beforeHide(e) {
      if (e instanceof KeyboardEvent) {
        segmentRefs.segmentThree.current && segmentRefs.segmentThree.current.focus();
        return _beforeHide(e);
      }
      if (isEventSource(dateInputRef, e) || isEventSource(clearRef, e)) {
        return false;
      }
      return _beforeHide(e);
    },
    beforeShow: function beforeShow(e) {
      if (isEventSource(clearRef, e)) {
        return false;
      }
      return _beforeShow(e);
    },
    clickOutsideIgnoreRefs: [monthRef, yearRef],
    hideKeys: hideKeys,
    overlay: overlay,
    placement: "bottom-left",
    ref: dateInputRef,
    showKeys: showKeys
  }, /*#__PURE__*/React.createElement(DateInput, _extends({}, props, {
    clearRef: clearRef,
    segmentRefs: segmentRefs,
    onChangeSegment: function onChangeSegment(type, value) {
      if (value === -1) {
        return;
      }
      if (type === 'month') {
        setDisplayed(dateTime.newDate(displayed.setMonth(value - 1)));
      } else if (type === 'year') {
        // isValidYearRange prevents calendar from changing, the display date
        if (isValidYearRange(value)) {
          setDisplayed(dateTime.newDate(displayed.setFullYear(value)));
        }
      }
    },
    onChange: function onChange(date) {
      consoleMessageChange({
        date: date
      });
      if (date) {
        setSelected(date);
        setDisplayed(date);
      } else {
        setDisplayed(dateTime.newDate());
        setSelected(undefined);
      }
      _onChange(date);
    },
    onClear: onClear,
    value: selected
  })));
});
//# sourceMappingURL=DateSelect.js.map