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 { useTheme } from 'styled-components';
import { useIntl } from 'react-intl';
import { Box, Button, Icon, Input, Modal, useMedia, Flexbox } from '../../..';
import { assert, isUndefined } from '../../../common/utils';
import { useOnClickOutside } from '../../../common/Hooks';
import { getDateFormat } from '../shared/dateUtils';
import Header from '../Single/Header';
import Calendar from '../Single/Calendar';
import { DEFAULT_INPUT_WIDTH } from '../shared/constants';
import { useDatePickerClearable } from './hooks/useDatePickerClearable';
import { StyledDropdownBubble, StyledDropdownBubbleWrapper, StyledInputText } from '../shared/DatePicker.styled';
export var isFullscreenMode = function isFullscreenMode(props, isSmallScreen) {
  return Boolean(props.fullscreenOnMobile) && Boolean(isSmallScreen);
};
var DatePickerClearable = React.forwardRef(function (props, ref) {
  var _props$fullscreenProp2;
  var ariaLabelPrevious = props.ariaLabelPrevious,
    ariaLabelNext = props.ariaLabelNext,
    _props$allowDateUpdat = props.allowDateUpdateOnType,
    allowDateUpdateOnType = _props$allowDateUpdat === void 0 ? false : _props$allowDateUpdat,
    onChange = props.onChange,
    onBlur = props.onBlur,
    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,
    _props$width = props.width,
    width = _props$width === void 0 ? DEFAULT_INPUT_WIDTH : _props$width,
    yearSelectLength = props.yearSelectLength,
    inputSize = props.inputSize,
    fullscreenOnMobile = props.fullscreenOnMobile,
    selectMonthLabel = props.selectMonthLabel,
    selectYearLabel = props.selectYearLabel,
    errorMessage = props.errorMessage,
    clearDateButton = props.clearDateButton;
  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 (fullscreenOnMobile) {
    assert(!isUndefined(props.fullscreenProps), "DatePicker: \"fullscreenProps\" is required when \"fullscreenOnMobile\" is true");
  }
  var theme = useTheme();
  var _useIntl = useIntl(),
    locale = _useIntl.locale;
  var dateFormat = getDateFormat(locale);
  var isSmallScreen = useMedia(function (t) {
    return t.media.lessThan(t.breakpoints.sm);
  });
  var _useState = useState(openProp),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  useEffect(function () {
    setOpen(openProp);
  }, [openProp]);
  var onClose = useCallback(function () {
    return setOpen(false);
  }, []);
  var _useDatePickerClearab = useDatePickerClearable({
      id: id,
      selectedDateProp: selectedDateProp,
      enableDate: enableDate,
      disableDate: disableDate,
      onChange: onChange,
      onBlur: onBlur,
      allowDateUpdateOnType: allowDateUpdateOnType
    }),
    selectedDate = _useDatePickerClearab.selectedDate,
    inputValue = _useDatePickerClearab.inputValue,
    handleInputKeyDown = _useDatePickerClearab.handleInputKeyDown,
    handleInputOnChange = _useDatePickerClearab.handleInputOnChange,
    handleInputOnBlur = _useDatePickerClearab.handleInputOnBlur,
    onMonthChange = _useDatePickerClearab.onMonthChange,
    onYearChange = _useDatePickerClearab.onYearChange,
    onDateClick = _useDatePickerClearab.onDateClick,
    viewedDate = _useDatePickerClearab.viewedDate,
    clearDate = _useDatePickerClearab.clearDate,
    invalidDate = _useDatePickerClearab.invalidDate;
  var fullscreenMode = isFullscreenMode(props, isSmallScreen);
  var handleInputOnFocus = useCallback(function () {
    return setOpen(true);
  }, [setOpen]);
  var handleClick = useCallback(function (date) {
    onDateClick(date);
    if (!fullscreenMode) onClose();
  }, [onDateClick, onClose, fullscreenMode]);
  var datepicker = React.createElement(React.Fragment, null, React.createElement(Header, {
    ariaLabelPrevious: ariaLabelPrevious,
    ariaLabelNext: ariaLabelNext,
    id: id,
    viewedDate: viewedDate,
    locale: locale,
    onMonthChange: onMonthChange,
    onYearChange: onYearChange,
    yearSelectLength: yearSelectLength,
    fullscreenMode: fullscreenMode,
    selectMonthLabel: selectMonthLabel,
    selectYearLabel: selectYearLabel
  }), React.createElement(Calendar, {
    disableDate: disableDate,
    enableDate: enableDate,
    viewedDate: viewedDate,
    onMonthChange: onMonthChange,
    locale: locale,
    onClick: handleClick,
    selectedDate: selectedDate,
    fullscreenMode: fullscreenMode
  }));
  var inputRightAddon = React.createElement(Icon.Calendar24, null);
  var selfRef = useRef(null);
  useOnClickOutside(selfRef, function () {
    if (!fullscreenMode) {
      setOpen(false);
    }
  });
  var clearClickHandler = function clearClickHandler() {
    clearDate();
    if (clearDateButton !== null && clearDateButton !== void 0 && clearDateButton.onClearDate) {
      clearDateButton.onClearDate();
    }
  };
  return React.createElement("div", {
    ref: ref || selfRef
  }, React.createElement(StyledInputText, {
    size: inputSize,
    label: label,
    disabled: disabled,
    id: id,
    placeholder: dateFormat.toLowerCase(),
    value: inputValue,
    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.createElement(StyledDropdownBubbleWrapper, null, React.createElement(StyledDropdownBubble, null, React.createElement(Box, {
    my: 3,
    mx: 2
  }, datepicker, clearDateButton ? React.createElement(Box, {
    pt: 5
  }, React.createElement(Button, {
    variant: "neutral",
    color: function color(p) {
      return p.colorTokens.action.background_default;
    },
    onClick: clearClickHandler
  }, clearDateButton.clearButtonLabel)) : null))), open && isFullscreenMode(props, isSmallScreen) && React.createElement(Modal, {
    title: props.fullscreenProps.title,
    open: open,
    onClose: onClose,
    fullScreenMobile: true,
    footer: React.createElement(React.Fragment, null, React.createElement(Box, {
      py: 5
    }, React.createElement(Button, {
      variant: "neutral",
      color: function color(p) {
        return p.colorTokens.action.background_default;
      },
      onClick: function onClick() {
        var _props$fullscreenProp;
        clearDate();
        if (props !== null && props !== void 0 && (_props$fullscreenProp = props.fullscreenProps) !== null && _props$fullscreenProp !== void 0 && _props$fullscreenProp.onClearDate) {
          props.fullscreenProps.onClearDate();
        }
      }
    }, props.fullscreenProps.clearButtonLabel)), React.createElement(Button, {
      onClick: onClose,
      size: "l",
      fullWidth: true
    }, props.fullscreenProps.confirmButtonLabel))
  }, React.createElement(React.Fragment, null, React.createElement(Box, {
    pb: 4
  }, React.createElement(Flexbox, {
    container: true,
    gap: 2,
    justifyContent: "center"
  }, React.createElement(Flexbox, {
    item: true,
    flex: "1"
  }, React.createElement(Input.Text, {
    id: "fromDate",
    placeholder: inputValue,
    label: (_props$fullscreenProp2 = props.fullscreenProps.dateLabel) !== null && _props$fullscreenProp2 !== void 0 ? _props$fullscreenProp2 : props.fullscreenProps.title,
    width: "100%",
    readOnly: true
  })))), datepicker)));
});
export default DatePickerClearable;