function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import React, { useRef } from "react";
import cls from "classnames";
import { getPlacements, getSizeValueFromProp } from "../../../../utils";
import { StyledPopover } from "../RangeDate.styles";
import { classes } from "../../DatePicker.tokens";
import { StyledShortcutList } from "../../ui";
import { StyledCalendar, StyledCalendarDouble } from "./RangeDatePopover.styles";
export var RangeDatePopover = function RangeDatePopover(param) {
    var Root = param.rootWrapper, target = param.target, opened = param.opened, isDoubleCalendar = param.isDoubleCalendar, calendarValue = param.calendarValue, min = param.min, max = param.max, renderFromDate = param.renderFromDate, includeEdgeDates = param.includeEdgeDates, eventTooltipOptions = param.eventTooltipOptions, eventList = param.eventList, disabledList = param.disabledList, eventMonthList = param.eventMonthList, disabledMonthList = param.disabledMonthList, eventQuarterList = param.eventQuarterList, disabledQuarterList = param.disabledQuarterList, eventYearList = param.eventYearList, disabledYearList = param.disabledYearList, _param_frame = param.frame, frame = _param_frame === void 0 ? 'document' : _param_frame, _param_usePortal = param.usePortal, usePortal = _param_usePortal === void 0 ? false : _param_usePortal, zIndex = param.zIndex, _param_placement = param.placement, placement = _param_placement === void 0 ? [
        'top',
        'bottom'
    ] : _param_placement, _param_closeOnOverlayClick = param.closeOnOverlayClick, closeOnOverlayClick = _param_closeOnOverlayClick === void 0 ? true : _param_closeOnOverlayClick, closeOnEsc = param.closeOnEsc, offset = param.offset, calendarContainerWidth = param.calendarContainerWidth, calendarContainerHeight = param.calendarContainerHeight, stretched = param.stretched, type = param.type, size = param.size, _param_lang = param.lang, lang = _param_lang === void 0 ? 'ru' : _param_lang, disabled = param.disabled, readOnly = param.readOnly, dateShortcuts = param.dateShortcuts, _param_dateShortcutsPlacement = param.dateShortcutsPlacement, dateShortcutsPlacement = _param_dateShortcutsPlacement === void 0 ? 'left' : _param_dateShortcutsPlacement, dateShortcutsWidth = param.dateShortcutsWidth, onShortcutDateSelect = param.onShortcutDateSelect, setIsInnerOpen = param.setIsInnerOpen, onChangeValue = param.onChangeValue, onChangeStartOfRange = param.onChangeStartOfRange, onToggle = param.onToggle;
    var innerIsOpen = Boolean(opened);
    var calendarRootRef = useRef(null);
    var doubleCalendarRootRef = useRef(null);
    var calendarContainerWidthValue = calendarContainerWidth ? getSizeValueFromProp(calendarContainerWidth, 'rem') : undefined;
    var calendarContainerHeightValue = calendarContainerHeight ? getSizeValueFromProp(calendarContainerHeight, 'rem') : undefined;
    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) {
            setIsInnerOpen(false);
            if (onToggle) {
                onToggle(false, event);
            }
        }
    };
    if (isDoubleCalendar) {
        return /*#__PURE__*/ React.createElement(StyledPopover, {
            opened: innerIsOpen,
            frame: frame,
            usePortal: usePortal,
            onToggle: onToggle,
            offset: offset,
            placement: getPlacements(placement, false),
            trigger: "click",
            closeOnOverlayClick: closeOnOverlayClick,
            isFocusTrapped: false,
            target: target,
            preventOverflow: false,
            zIndex: zIndex,
            innerWidth: calendarContainerWidthValue,
            innerHeight: calendarContainerHeightValue
        }, /*#__PURE__*/ React.createElement(Root, {
            ref: doubleCalendarRootRef,
            className: cls(classes.datePickerRoot, _define_property({}, classes.datePickerstretched, stretched)),
            onClick: handleCalendarRootClick
        }, (dateShortcuts === null || dateShortcuts === void 0 ? void 0 : dateShortcuts.length) && onShortcutDateSelect ? /*#__PURE__*/ React.createElement(StyledShortcutList, {
            items: dateShortcuts,
            setShortcutDate: onShortcutDateSelect,
            dateShortcutsWidth: dateShortcutsWidth,
            calendarContainerHeight: calendarContainerHeight,
            dateShortcutsPlacement: dateShortcutsPlacement
        }) : null, /*#__PURE__*/ React.createElement(StyledCalendarDouble, {
            className: cls(_define_property({}, classes.datePickerCalendarstretched, stretched)),
            innerWidth: calendarContainerWidthValue,
            innerHeight: calendarContainerHeightValue,
            size: size,
            value: calendarValue,
            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
        })));
    }
    return /*#__PURE__*/ React.createElement(StyledPopover, {
        opened: innerIsOpen,
        frame: frame,
        usePortal: usePortal,
        onToggle: onToggle,
        offset: offset,
        placement: getPlacements(placement),
        trigger: "click",
        closeOnOverlayClick: closeOnOverlayClick,
        isFocusTrapped: false,
        target: target,
        preventOverflow: false,
        closeOnEsc: closeOnEsc,
        zIndex: zIndex,
        innerWidth: calendarContainerWidthValue,
        innerHeight: calendarContainerHeightValue
    }, /*#__PURE__*/ React.createElement(Root, {
        ref: calendarRootRef,
        className: cls(classes.datePickerRoot, _define_property({}, classes.datePickerstretched, stretched)),
        onClick: handleCalendarRootClick
    }, (dateShortcuts === null || dateShortcuts === void 0 ? void 0 : dateShortcuts.length) && onShortcutDateSelect ? /*#__PURE__*/ React.createElement(StyledShortcutList, {
        items: dateShortcuts,
        setShortcutDate: onShortcutDateSelect,
        dateShortcutsWidth: dateShortcutsWidth,
        calendarContainerHeight: calendarContainerHeight,
        dateShortcutsPlacement: dateShortcutsPlacement
    }) : null, /*#__PURE__*/ React.createElement(StyledCalendar, {
        className: cls(_define_property({}, classes.datePickerCalendarstretched, stretched)),
        innerWidth: calendarContainerWidthValue,
        innerHeight: calendarContainerHeightValue,
        size: size,
        value: calendarValue,
        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
    })));
};
