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; }
import React, { useCallback, useEffect, useRef, useState } from 'react';
import styled, { useTheme } from 'styled-components';
import { useIntl } from 'react-intl';
import Input from '../../Input';
import { Box, DropdownBubble, Flexbox, Icon, Button } from '../../..';
import { assert, isUndefined } from '../../../common/utils';
import { useOnClickOutside } from '../../../common/Hooks';
import { getDateFormat } from '../shared/dateUtils';
import DoubleHeader from './Header';
import DoubleCalendar from './Calendar';
import { DEFAULT_INPUT_WIDTH, INPUT_ID_END, INPUT_ID_START } from '../shared/constants';
import { useDatePicker } from '../shared/hooks';
import { StyledDropdownBubbleWrapper } from '../shared/DatePicker.styled';
var INPUT_SPACING = 4;
var StyledInputText = styled(Input.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 = styled(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.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 ? 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;
  assert(Boolean(props.id), "DatePicker: \"id\" is required.");
  if (disableDate) {
    assert(isUndefined(enableDate), "DatePicker: \"enableDate\" cannot be used at the same time as \"disableDate\".");
  }
  if (showClearButton) {
    assert(!isUndefined(props.clearButtonLabel), "DatePicker: \"clearButtonLabel\" is required when \"showClearButton\" is true");
  }
  var theme = useTheme();
  var _useIntl = useIntl(),
    locale = _useIntl.locale;
  var dateFormat = getDateFormat(locale);
  var _useState = useState(openProp),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  useEffect(function () {
    setOpen(openProp);
  }, [openProp]);
  var _useDatePicker = 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 = useCallback(function () {
    return setOpen(true);
  }, [setOpen]);
  var datepicker = React.createElement(Box, {
    my: 3,
    mx: 2
  }, React.createElement(DoubleHeader, {
    ariaLabelPrevious: ariaLabelPrevious,
    ariaLabelNext: ariaLabelNext,
    id: id,
    viewedDate: viewedDate,
    locale: locale,
    onMonthChange: onMonthChange,
    onYearChange: onYearChange,
    yearSelectLength: yearSelectLength,
    selectMonthLabel: selectMonthLabel,
    selectYearLabel: selectYearLabel
  }), React.createElement(DoubleCalendar, {
    disableDate: disableDate,
    enableDate: enableDate,
    viewedDate: viewedDate,
    locale: locale,
    onClick: onDateClick,
    selectedStartDate: selectedStartDate,
    selectedEndDate: selectedEndDate,
    onMonthChange: onMonthChange
  }), isWithClearButton(props) ? React.createElement(Box, {
    pt: 3
  }, React.createElement(Button, {
    variant: "neutral",
    color: function color(p) {
      return p.colorTokens.action.background_default;
    },
    onClick: clearDate
  }, props.clearButtonLabel)) : null);
  var inputRightAddon = React.createElement(Icon.Calendar24, null);
  var selfRef = useRef(null);
  useOnClickOutside(selfRef, function () {
    setOpen(false);
  });
  return React.createElement("div", {
    ref: ref || selfRef
  }, React.createElement(Flexbox, {
    container: true,
    wrap: "wrap",
    gap: 1,
    alignItems: "flex-end"
  }, React.createElement(StyledInputText, {
    size: inputSize,
    label: labelFrom,
    disabled: disabled,
    id: 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.createElement(StyledInputText, {
    size: inputSize,
    label: labelTo,
    disabled: disabled,
    id: 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.createElement(StyledDropdownBubbleWrapper, null, React.createElement(StyledDropdownBubble, null, datepicker)) : null);
});
export default DoubleDatePicker;