'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
var DatePicker_tokens = require('../../DatePicker.tokens.js');
var FloatingPopover = require('../../FloatingPopover.js');
var DatePickerBase_styles = require('../../DatePickerBase.styles.js');
var RangeDatePopover_styles = require('./RangeDatePopover.styles.js');
var getSizeValueFromProp = require('../../../../utils/getSizeValueFromProp.js');
var DateShortcutList_styles = require('../../ui/DateShortcut/DateShortcutList.styles.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var RangeDatePopover = function RangeDatePopover(_ref) {
  var Root = _ref.rootWrapper,
    target = _ref.target,
    opened = _ref.opened,
    isDoubleCalendar = _ref.isDoubleCalendar,
    calendarValue = _ref.calendarValue,
    calendarFocusedDate = _ref.calendarFocusedDate,
    min = _ref.min,
    max = _ref.max,
    renderFromDate = _ref.renderFromDate,
    includeEdgeDates = _ref.includeEdgeDates,
    eventTooltipOptions = _ref.eventTooltipOptions,
    eventList = _ref.eventList,
    disabledList = _ref.disabledList,
    eventMonthList = _ref.eventMonthList,
    disabledMonthList = _ref.disabledMonthList,
    eventQuarterList = _ref.eventQuarterList,
    disabledQuarterList = _ref.disabledQuarterList,
    eventYearList = _ref.eventYearList,
    disabledYearList = _ref.disabledYearList,
    _ref$frame = _ref.frame,
    frame = _ref$frame === void 0 ? 'document' : _ref$frame,
    _ref$usePortal = _ref.usePortal,
    usePortal = _ref$usePortal === void 0 ? false : _ref$usePortal,
    zIndex = _ref.zIndex,
    _ref$placement = _ref.placement,
    placement = _ref$placement === void 0 ? ['top', 'bottom'] : _ref$placement,
    _ref$closeOnOverlayCl = _ref.closeOnOverlayClick,
    closeOnOverlayClick = _ref$closeOnOverlayCl === void 0 ? true : _ref$closeOnOverlayCl,
    _ref$closeOnEsc = _ref.closeOnEsc,
    closeOnEsc = _ref$closeOnEsc === void 0 ? true : _ref$closeOnEsc,
    offset = _ref.offset,
    disableFlip = _ref.disableFlip,
    calendarContainerWidth = _ref.calendarContainerWidth,
    calendarContainerHeight = _ref.calendarContainerHeight,
    stretched = _ref.stretched,
    type = _ref.type,
    size = _ref.size,
    _ref$lang = _ref.lang,
    lang = _ref$lang === void 0 ? 'ru' : _ref$lang,
    disabled = _ref.disabled,
    readOnly = _ref.readOnly,
    dateShortcuts = _ref.dateShortcuts,
    _ref$dateShortcutsPla = _ref.dateShortcutsPlacement,
    dateShortcutsPlacement = _ref$dateShortcutsPla === void 0 ? 'left' : _ref$dateShortcutsPla,
    dateShortcutsWidth = _ref.dateShortcutsWidth,
    onShortcutDateSelect = _ref.onShortcutDateSelect,
    setIsInnerOpen = _ref.setIsInnerOpen,
    onChangeValue = _ref.onChangeValue,
    onChangeStartOfRange = _ref.onChangeStartOfRange,
    onChangeSingleValue = _ref.onChangeSingleValue,
    onToggle = _ref.onToggle;
  var innerIsOpen = Boolean(opened);
  var calendarRootRef = React.useRef(null);
  var doubleCalendarRootRef = React.useRef(null);
  var floatingPopoverRef = React.useRef(null);
  var calendarContainerWidthValue = calendarContainerWidth ? getSizeValueFromProp.getSizeValueFromProp(calendarContainerWidth, 'rem') : undefined;
  var calendarContainerHeightValue = calendarContainerHeight ? getSizeValueFromProp.getSizeValueFromProp(calendarContainerHeight, 'rem') : undefined;
  var handleToggle = function handleToggle(isOpen, event) {
    setIsInnerOpen(isOpen);
    onToggle === null || onToggle === void 0 || onToggle(isOpen, event);
  };
  var handleCalendarRootClick = function handleCalendarRootClick(event) {
    if (disabled || readOnly) {
      return;
    }
    var isRootClicked = event.target === (calendarRootRef === null || calendarRootRef === void 0 ? void 0 : calendarRootRef.current) || event.target === (doubleCalendarRootRef === null || doubleCalendarRootRef === void 0 ? void 0 : doubleCalendarRootRef.current);
    if (innerIsOpen && stretched && isRootClicked) {
      handleToggle(false, event);
    }
  };
  if (isDoubleCalendar) {
    return /*#__PURE__*/React__default.default.createElement(FloatingPopover.FloatingPopover, {
      ref: floatingPopoverRef,
      opened: innerIsOpen,
      onToggle: handleToggle,
      offset: offset,
      zIndex: zIndex,
      placement: placement,
      disableFlip: disableFlip,
      closeOnOverlayClick: closeOnOverlayClick,
      closeOnEsc: closeOnEsc,
      portal: usePortal ? frame : undefined,
      target: target
    }, /*#__PURE__*/React__default.default.createElement(Root, {
      ref: doubleCalendarRootRef,
      className: cls__default.default(DatePicker_tokens.classes.datePickerRoot, _rollupPluginBabelHelpers.defineProperty({}, DatePicker_tokens.classes.datePickerstretched, stretched)),
      onClick: handleCalendarRootClick
    }, /*#__PURE__*/React__default.default.createElement(DatePickerBase_styles.StyledCalendarContent, {
      innerWidth: calendarContainerWidthValue,
      innerHeight: calendarContainerHeightValue
    }, dateShortcuts !== null && dateShortcuts !== void 0 && dateShortcuts.length && onShortcutDateSelect ? /*#__PURE__*/React__default.default.createElement(DateShortcutList_styles.StyledShortcutList, {
      items: dateShortcuts,
      setShortcutDate: onShortcutDateSelect,
      dateShortcutsWidth: dateShortcutsWidth,
      calendarContainerHeight: calendarContainerHeight,
      dateShortcutsPlacement: dateShortcutsPlacement
    }) : null, /*#__PURE__*/React__default.default.createElement(RangeDatePopover_styles.StyledCalendarDouble, {
      className: cls__default.default(_rollupPluginBabelHelpers.defineProperty({}, DatePicker_tokens.classes.datePickerCalendarstretched, stretched)),
      innerWidth: calendarContainerWidthValue,
      innerHeight: calendarContainerHeightValue,
      size: size,
      value: calendarValue,
      focusedDate: calendarFocusedDate,
      eventTooltipOptions: eventTooltipOptions,
      eventList: eventList,
      disabledList: disabledList,
      eventMonthList: eventMonthList,
      disabledMonthList: disabledMonthList,
      eventQuarterList: eventQuarterList,
      disabledQuarterList: disabledQuarterList,
      eventYearList: eventYearList,
      disabledYearList: disabledYearList,
      min: min,
      max: max,
      renderFromDate: renderFromDate,
      type: type,
      locale: lang,
      includeEdgeDates: includeEdgeDates,
      onChangeValue: onChangeValue,
      onChangeStartOfRange: onChangeStartOfRange,
      onChangeSingleValue: onChangeSingleValue
    }))));
  }
  return /*#__PURE__*/React__default.default.createElement(FloatingPopover.FloatingPopover, {
    ref: floatingPopoverRef,
    opened: innerIsOpen,
    onToggle: handleToggle,
    offset: offset,
    zIndex: zIndex,
    placement: placement,
    disableFlip: disableFlip,
    closeOnOverlayClick: closeOnOverlayClick,
    closeOnEsc: closeOnEsc,
    portal: usePortal ? frame : undefined,
    target: target
  }, /*#__PURE__*/React__default.default.createElement(Root, {
    ref: calendarRootRef,
    className: cls__default.default(DatePicker_tokens.classes.datePickerRoot, _rollupPluginBabelHelpers.defineProperty({}, DatePicker_tokens.classes.datePickerstretched, stretched)),
    onClick: handleCalendarRootClick
  }, /*#__PURE__*/React__default.default.createElement(DatePickerBase_styles.StyledCalendarContent, {
    innerWidth: calendarContainerWidthValue,
    innerHeight: calendarContainerHeightValue
  }, dateShortcuts !== null && dateShortcuts !== void 0 && dateShortcuts.length && onShortcutDateSelect ? /*#__PURE__*/React__default.default.createElement(DateShortcutList_styles.StyledShortcutList, {
    items: dateShortcuts,
    setShortcutDate: onShortcutDateSelect,
    dateShortcutsWidth: dateShortcutsWidth,
    calendarContainerHeight: calendarContainerHeight,
    dateShortcutsPlacement: dateShortcutsPlacement
  }) : null, /*#__PURE__*/React__default.default.createElement(RangeDatePopover_styles.StyledCalendar, {
    className: cls__default.default(_rollupPluginBabelHelpers.defineProperty({}, DatePicker_tokens.classes.datePickerCalendarstretched, stretched)),
    innerWidth: calendarContainerWidthValue,
    innerHeight: calendarContainerHeightValue,
    size: size,
    value: calendarValue,
    focusedDate: calendarFocusedDate,
    eventTooltipOptions: eventTooltipOptions,
    eventList: eventList,
    disabledList: disabledList,
    eventMonthList: eventMonthList,
    disabledMonthList: disabledMonthList,
    eventQuarterList: eventQuarterList,
    disabledQuarterList: disabledQuarterList,
    eventYearList: eventYearList,
    disabledYearList: disabledYearList,
    renderFromDate: renderFromDate,
    min: min,
    max: max,
    type: type,
    locale: lang,
    includeEdgeDates: includeEdgeDates,
    onChangeValue: onChangeValue,
    onChangeStartOfRange: onChangeStartOfRange,
    onChangeSingleValue: onChangeSingleValue
  }))));
};

exports.RangeDatePopover = RangeDatePopover;
