"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RangeDatePopover", {
    enumerable: true,
    get: function() {
        return RangeDatePopover;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _utils = require("../../../../utils");
const _DatePickertokens = require("../../DatePicker.tokens");
const _ui = require("../../ui");
const _FloatingPopover = require("../../FloatingPopover");
const _DatePickerBasestyles = require("../../DatePickerBase.styles");
const _RangeDatePopoverstyles = require("./RangeDatePopover.styles");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const RangeDatePopover = ({ rootWrapper: Root, target, opened, isDoubleCalendar, calendarValue, calendarFocusedDate, min, max, renderFromDate, includeEdgeDates, eventTooltipOptions, eventList, disabledList, eventMonthList, disabledMonthList, eventQuarterList, disabledQuarterList, eventYearList, disabledYearList, frame = 'document', usePortal = false, zIndex, placement = [
    'top',
    'bottom'
], closeOnOverlayClick = true, closeOnEsc = true, offset, disableFlip, calendarContainerWidth, calendarContainerHeight, stretched, type, size, lang = 'ru', disabled, readOnly, dateShortcuts, dateShortcutsPlacement = 'left', dateShortcutsWidth, onShortcutDateSelect, setIsInnerOpen, onChangeValue, onChangeStartOfRange, onChangeSingleValue, onToggle })=>{
    const innerIsOpen = Boolean(opened);
    const calendarRootRef = (0, _react.useRef)(null);
    const doubleCalendarRootRef = (0, _react.useRef)(null);
    const floatingPopoverRef = (0, _react.useRef)(null);
    const calendarContainerWidthValue = calendarContainerWidth ? (0, _utils.getSizeValueFromProp)(calendarContainerWidth, 'rem') : undefined;
    const calendarContainerHeightValue = calendarContainerHeight ? (0, _utils.getSizeValueFromProp)(calendarContainerHeight, 'rem') : undefined;
    const handleToggle = (isOpen, event)=>{
        setIsInnerOpen(isOpen);
        onToggle?.(isOpen, event);
    };
    const handleCalendarRootClick = (event)=>{
        if (disabled || readOnly) {
            return;
        }
        const isRootClicked = event.target === calendarRootRef?.current || event.target === doubleCalendarRootRef?.current;
        if (innerIsOpen && stretched && isRootClicked) {
            handleToggle(false, event);
        }
    };
    if (isDoubleCalendar) {
        return /*#__PURE__*/ _react.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.createElement(Root, {
            ref: doubleCalendarRootRef,
            className: (0, _classnames.default)(_DatePickertokens.classes.datePickerRoot, {
                [_DatePickertokens.classes.datePickerstretched]: stretched
            }),
            onClick: handleCalendarRootClick
        }, /*#__PURE__*/ _react.default.createElement(_DatePickerBasestyles.StyledCalendarContent, {
            innerWidth: calendarContainerWidthValue,
            innerHeight: calendarContainerHeightValue
        }, dateShortcuts?.length && onShortcutDateSelect ? /*#__PURE__*/ _react.default.createElement(_ui.StyledShortcutList, {
            items: dateShortcuts,
            setShortcutDate: onShortcutDateSelect,
            dateShortcutsWidth: dateShortcutsWidth,
            calendarContainerHeight: calendarContainerHeight,
            dateShortcutsPlacement: dateShortcutsPlacement
        }) : null, /*#__PURE__*/ _react.default.createElement(_RangeDatePopoverstyles.StyledCalendarDouble, {
            className: (0, _classnames.default)({
                [_DatePickertokens.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.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.createElement(Root, {
        ref: calendarRootRef,
        className: (0, _classnames.default)(_DatePickertokens.classes.datePickerRoot, {
            [_DatePickertokens.classes.datePickerstretched]: stretched
        }),
        onClick: handleCalendarRootClick
    }, /*#__PURE__*/ _react.default.createElement(_DatePickerBasestyles.StyledCalendarContent, {
        innerWidth: calendarContainerWidthValue,
        innerHeight: calendarContainerHeightValue
    }, dateShortcuts?.length && onShortcutDateSelect ? /*#__PURE__*/ _react.default.createElement(_ui.StyledShortcutList, {
        items: dateShortcuts,
        setShortcutDate: onShortcutDateSelect,
        dateShortcutsWidth: dateShortcutsWidth,
        calendarContainerHeight: calendarContainerHeight,
        dateShortcutsPlacement: dateShortcutsPlacement
    }) : null, /*#__PURE__*/ _react.default.createElement(_RangeDatePopoverstyles.StyledCalendar, {
        className: (0, _classnames.default)({
            [_DatePickertokens.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
    }))));
};
