var _excluded = ["onSelect"],
  _excluded2 = ["beforeHide", "beforeShow", "afterHide", "afterShow", "disabledDate", "onChange", "onClear", "value"];
var _process$env$TZ_LOG;
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React from 'react';
import { Calendar as CalendarBase } from '../Calendar';
import { Card } from '../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