"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.isFullscreenMode = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = 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("../Single/Header"));
var _Calendar = _interopRequireDefault(require("../Single/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 isFullscreenMode = exports.isFullscreenMode = function isFullscreenMode(props, isSmallScreen) {
  return Boolean(props.fullscreenOnMobile) && Boolean(isSmallScreen);
};
var isWithClearButton = function isWithClearButton(props) {
  return Boolean(props.showClearButton);
};
var DateRangePicker = _react.default.forwardRef(function (props, ref) {
  var _props$fullscreenProp, _props$fullscreenProp2;
  var ariaLabelPrevious = props.ariaLabelPrevious,
    ariaLabelNext = props.ariaLabelNext,
    _props$allowDateUpdat = props.allowDateUpdateOnType,
    allowDateUpdateOnType = _props$allowDateUpdat === void 0 ? false : _props$allowDateUpdat,
    onChange = props.onChange,
    label = props.label,
    disabled = props.disabled,
    disableDate = props.disableDate,
    enableDate = props.enableDate,
    id = props.id,
    _props$open = props.open,
    openProp = _props$open === void 0 ? false : _props$open,
    selectedDateProp = props.selectedDate,
    selectedEndDateProp = props.selectedEndDate,
    _props$width = props.width,
    width = _props$width === void 0 ? _constants.DEFAULT_INPUT_WIDTH : _props$width,
    yearSelectLength = props.yearSelectLength,
    inputSize = props.inputSize,
    allowSingleDayRange = props.allowSingleDayRange,
    showClearButton = props.showClearButton,
    fullscreenOnMobile = props.fullscreenOnMobile,
    selectMonthLabel = props.selectMonthLabel,
    selectYearLabel = props.selectYearLabel,
    errorMessage = props.errorMessage;
  (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");
  }
  if (fullscreenOnMobile) {
    (0, _utils.assert)(!(0, _utils.isUndefined)(props.fullscreenProps), "DatePicker: \"fullscreenProps\" is required when \"fullscreenOnMobile\" is true");
  }
  var theme = (0, _styledComponents.useTheme)();
  var _useIntl = (0, _reactIntl.useIntl)(),
    locale = _useIntl.locale;
  var dateFormat = (0, _dateUtils.getDateFormat)(locale);
  var isSmallScreen = (0, _.useMedia)(function (t) {
    return t.media.lessThan(t.breakpoints.sm);
  });
  var _useState = (0, _react.useState)(openProp),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  (0, _react.useEffect)(function () {
    setOpen(openProp);
  }, [openProp]);
  var onClose = (0, _react.useCallback)(function () {
    return setOpen(false);
  }, []);
  var _useDatePicker = (0, _hooks.useDatePicker)({
      id: id,
      selectedDateProp: selectedDateProp,
      selectedEndDateProp: selectedEndDateProp,
      enableDate: enableDate,
      disableDate: disableDate,
      onChange: onChange,
      allowDateUpdateOnType: allowDateUpdateOnType,
      allowSingleDayRange: allowSingleDayRange,
      isRangePicker: true
    }),
    selectedDate = _useDatePicker.selectedDate,
    selectedEndDate = _useDatePicker.selectedEndDate,
    inputValue = _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,
    invalidDate = _useDatePicker.invalidDate;
  var handleInputOnFocus = (0, _react.useCallback)(function () {
    return setOpen(true);
  }, [setOpen]);
  var datepicker = _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_Header.default, {
    ariaLabelPrevious: ariaLabelPrevious,
    ariaLabelNext: ariaLabelNext,
    id: id,
    viewedDate: viewedDate,
    locale: locale,
    onMonthChange: onMonthChange,
    onYearChange: onYearChange,
    yearSelectLength: yearSelectLength,
    fullscreenMode: isFullscreenMode(props, isSmallScreen),
    selectMonthLabel: selectMonthLabel,
    selectYearLabel: selectYearLabel
  }), _react.default.createElement(_Calendar.default, {
    disableDate: disableDate,
    enableDate: enableDate,
    viewedDate: viewedDate,
    onMonthChange: onMonthChange,
    locale: locale,
    onClick: onDateClick,
    selectedDate: selectedDate,
    selectedEndDate: selectedEndDate,
    fullscreenMode: isFullscreenMode(props, isSmallScreen)
  }), 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 () {
    if (!isFullscreenMode(props, isSmallScreen)) {
      setOpen(false);
    }
  });
  var getFormattedDate = (0, _react.useCallback)(function () {
    if (inputValue && !inputValueEnd) {
      return (0, _dateUtils.parseDateString)(inputValue, locale) ? "".concat(inputValue, " - ") : inputValue;
    }
    if (inputValue && inputValueEnd) {
      return "".concat(inputValue, " - ").concat(inputValueEnd);
    }
    return '';
  }, [inputValue, inputValueEnd, locale]);
  return _react.default.createElement("div", {
    ref: ref || selfRef
  }, _react.default.createElement(_DatePicker.StyledInputText, {
    size: inputSize,
    label: label,
    disabled: disabled,
    id: (0, _constants.INPUT_ID_RANGE)(id),
    placeholder: "".concat(dateFormat.toLowerCase(), " - ").concat(dateFormat.toLowerCase()),
    value: getFormattedDate(),
    rightAddon: inputRightAddon,
    onChange: handleInputOnChange,
    onKeyDown: handleInputKeyDown,
    onFocus: handleInputOnFocus,
    onBlur: handleInputOnBlur,
    width: typeof width === 'string' ? width : "".concat(theme.spacing.unit(width), "px"),
    autoComplete: "off",
    error: invalidDate ? errorMessage : ''
  }), open && !isFullscreenMode(props, isSmallScreen) && _react.default.createElement(_DatePicker.StyledDropdownBubbleWrapper, null, _react.default.createElement(_DatePicker.StyledDropdownBubble, null, _react.default.createElement(_.Box, {
    my: 3,
    mx: 2
  }, datepicker))), open && isFullscreenMode(props, isSmallScreen) && _react.default.createElement(_.Modal, {
    title: props.fullscreenProps.title,
    open: open,
    onClose: onClose,
    fullScreenMobile: true,
    footer: _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_.Box, {
      py: 5
    }, _react.default.createElement(_.Button, {
      variant: "neutral",
      color: function color(p) {
        return p.colorTokens.action.background_default;
      },
      onClick: clearDate
    }, props.fullscreenProps.clearButtonLabel)), _react.default.createElement(_.Button, {
      onClick: onClose,
      size: "l",
      fullWidth: true
    }, props.fullscreenProps.confirmButtonLabel))
  }, _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_.Box, {
    pb: 4
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    gap: 2,
    justifyContent: "center"
  }, _react.default.createElement(_.Flexbox, {
    item: true,
    flex: "1"
  }, _react.default.createElement(_Input.default.Text, {
    id: "fromDate",
    placeholder: inputValue,
    label: (_props$fullscreenProp = props.fullscreenProps.fromLabel) !== null && _props$fullscreenProp !== void 0 ? _props$fullscreenProp : '',
    width: "100%",
    readOnly: true
  })), _react.default.createElement(_.Flexbox, {
    item: true,
    flex: "1"
  }, _react.default.createElement(_Input.default.Text, {
    id: "toDate",
    placeholder: inputValueEnd,
    label: (_props$fullscreenProp2 = props.fullscreenProps.toLabel) !== null && _props$fullscreenProp2 !== void 0 ? _props$fullscreenProp2 : '',
    width: "100%",
    readOnly: true
  })))), datepicker)));
});
var _default = exports.default = DateRangePicker;