"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _reactIntl = require("react-intl");
var _Input = _interopRequireDefault(require("../../Input"));
var _ = require("../../..");
var _utils = require("../../../common/utils");
var _Hooks = require("../../../common/Hooks");
var _dateUtils = require("../shared/dateUtils");
var _Header = _interopRequireDefault(require("./Header"));
var _Calendar = _interopRequireDefault(require("./Calendar"));
var _constants = require("../shared/constants");
var _hooks = require("../shared/hooks");
var _DatePicker = require("../shared/DatePicker.styled");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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(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(arr) { if (Array.isArray(arr)) return arr; }
var INPUT_SPACING = 4;
var StyledInputText = (0, _styledComponents.default)(_Input.default.Text).withConfig({
  displayName: "DoubleDatePicker__StyledInputText",
  componentId: "sc-y1rncb-0"
})(["z-index:1;margin-bottom:", "px;"], function (p) {
  return p.theme.spacing.unit(1);
});
var StyledDropdownBubble = (0, _styledComponents.default)(_.DropdownBubble).withConfig({
  displayName: "DoubleDatePicker__StyledDropdownBubble",
  componentId: "sc-y1rncb-1"
})(["width:", "px;z-index:", ";box-sizing:border-box;top:-10px;border-radius:", ";&::after,&::before{display:none;}"], function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.unit(156 + INPUT_SPACING);
}, function (_ref2) {
  var theme = _ref2.theme;
  return theme.zIndex.overlay;
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.borderRadius8;
});
var isWithClearButton = function isWithClearButton(props) {
  return Boolean(props.showClearButton);
};
var DoubleDatePicker = _react.default.forwardRef(function (props, ref) {
  var ariaLabelPrevious = props.ariaLabelPrevious,
    ariaLabelNext = props.ariaLabelNext,
    onChange = props.onChange,
    labelFrom = props.labelFrom,
    _props$labelTo = props.labelTo,
    labelTo = _props$labelTo === void 0 ? '' : _props$labelTo,
    disabled = props.disabled,
    disableDate = props.disableDate,
    enableDate = props.enableDate,
    id = props.id,
    _props$open = props.open,
    openProp = _props$open === void 0 ? false : _props$open,
    selectedStartDateProp = props.selectedStartDate,
    selectedEndDateProp = props.selectedEndDate,
    _props$width = props.width,
    width = _props$width === void 0 ? _constants.DEFAULT_INPUT_WIDTH : _props$width,
    yearSelectLength = props.yearSelectLength,
    inputSize = props.inputSize,
    _props$allowSingleDay = props.allowSingleDayRange,
    allowSingleDayRange = _props$allowSingleDay === void 0 ? true : _props$allowSingleDay,
    allowDateUpdateOnType = props.allowDateUpdateOnType,
    showClearButton = props.showClearButton,
    selectMonthLabel = props.selectMonthLabel,
    selectYearLabel = props.selectYearLabel;
  (0, _utils.assert)(Boolean(props.id), "DatePicker: \"id\" is required.");
  if (disableDate) {
    (0, _utils.assert)((0, _utils.isUndefined)(enableDate), "DatePicker: \"enableDate\" cannot be used at the same time as \"disableDate\".");
  }
  if (showClearButton) {
    (0, _utils.assert)(!(0, _utils.isUndefined)(props.clearButtonLabel), "DatePicker: \"clearButtonLabel\" is required when \"showClearButton\" is true");
  }
  var theme = (0, _styledComponents.useTheme)();
  var _useIntl = (0, _reactIntl.useIntl)(),
    locale = _useIntl.locale;
  var dateFormat = (0, _dateUtils.getDateFormat)(locale);
  var _useState = (0, _react.useState)(openProp),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  (0, _react.useEffect)(function () {
    setOpen(openProp);
  }, [openProp]);
  var _useDatePicker = (0, _hooks.useDatePicker)({
      id: id,
      selectedDateProp: selectedStartDateProp,
      selectedEndDateProp: selectedEndDateProp,
      enableDate: enableDate,
      disableDate: disableDate,
      onChange: onChange,
      allowDateUpdateOnType: allowDateUpdateOnType,
      allowSingleDayRange: allowSingleDayRange,
      noOfMonthsInCalendarView: 2,
      isRangePicker: true
    }),
    selectedStartDate = _useDatePicker.selectedDate,
    selectedEndDate = _useDatePicker.selectedEndDate,
    inputValueStart = _useDatePicker.inputValue,
    inputValueEnd = _useDatePicker.inputValueEnd,
    handleInputKeyDown = _useDatePicker.handleInputKeyDown,
    handleInputOnChange = _useDatePicker.handleInputOnChange,
    handleInputOnBlur = _useDatePicker.handleInputOnBlur,
    onMonthChange = _useDatePicker.onMonthChange,
    onYearChange = _useDatePicker.onYearChange,
    onDateClick = _useDatePicker.onDateClick,
    viewedDate = _useDatePicker.viewedDate,
    clearDate = _useDatePicker.clearDate;
  var handleInputOnFocus = (0, _react.useCallback)(function () {
    return setOpen(true);
  }, [setOpen]);
  var datepicker = _react.default.createElement(_.Box, {
    my: 3,
    mx: 2
  }, _react.default.createElement(_Header.default, {
    ariaLabelPrevious: ariaLabelPrevious,
    ariaLabelNext: ariaLabelNext,
    id: id,
    viewedDate: viewedDate,
    locale: locale,
    onMonthChange: onMonthChange,
    onYearChange: onYearChange,
    yearSelectLength: yearSelectLength,
    selectMonthLabel: selectMonthLabel,
    selectYearLabel: selectYearLabel
  }), _react.default.createElement(_Calendar.default, {
    disableDate: disableDate,
    enableDate: enableDate,
    viewedDate: viewedDate,
    locale: locale,
    onClick: onDateClick,
    selectedStartDate: selectedStartDate,
    selectedEndDate: selectedEndDate,
    onMonthChange: onMonthChange
  }), isWithClearButton(props) ? _react.default.createElement(_.Box, {
    pt: 3
  }, _react.default.createElement(_.Button, {
    variant: "neutral",
    color: function color(p) {
      return p.colorTokens.action.background_default;
    },
    onClick: clearDate
  }, props.clearButtonLabel)) : null);
  var inputRightAddon = _react.default.createElement(_.Icon.Calendar24, null);
  var selfRef = (0, _react.useRef)(null);
  (0, _Hooks.useOnClickOutside)(selfRef, function () {
    setOpen(false);
  });
  return _react.default.createElement("div", {
    ref: ref || selfRef
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    wrap: "wrap",
    gap: 1,
    alignItems: "flex-end"
  }, _react.default.createElement(StyledInputText, {
    size: inputSize,
    label: labelFrom,
    disabled: disabled,
    id: (0, _constants.INPUT_ID_START)(id),
    placeholder: dateFormat.toLowerCase(),
    value: inputValueStart,
    rightAddon: inputRightAddon,
    onChange: handleInputOnChange,
    onKeyDown: handleInputKeyDown,
    onFocus: handleInputOnFocus,
    onBlur: handleInputOnBlur,
    width: typeof width === 'string' ? width : "".concat(theme.spacing.unit(width), "px"),
    autoComplete: "off"
  }), _react.default.createElement(StyledInputText, {
    size: inputSize,
    label: labelTo,
    disabled: disabled,
    id: (0, _constants.INPUT_ID_END)(id),
    placeholder: dateFormat.toLowerCase(),
    value: inputValueEnd,
    rightAddon: inputRightAddon,
    onChange: handleInputOnChange,
    onKeyDown: handleInputKeyDown,
    onFocus: handleInputOnFocus,
    onBlur: handleInputOnBlur,
    width: typeof width === 'string' ? width : "".concat(theme.spacing.unit(width), "px"),
    autoComplete: "off"
  })), open ? _react.default.createElement(_DatePicker.StyledDropdownBubbleWrapper, null, _react.default.createElement(StyledDropdownBubble, null, datepicker)) : null);
});
var _default = exports.default = DoubleDatePicker;