"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RangeDatePopover", {
    enumerable: true,
    get: function() {
        return RangeDatePopover;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _utils = require("../../../../utils");
var _RangeDatestyles = require("../RangeDate.styles");
var _DatePickertokens = require("../../DatePicker.tokens");
var _ui = require("../../ui");
var _RangeDatePopoverstyles = require("./RangeDatePopover.styles");
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;
}
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;
}
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 = (0, _react.useRef)(null);
    var doubleCalendarRootRef = (0, _react.useRef)(null);
    var calendarContainerWidthValue = calendarContainerWidth ? (0, _utils.getSizeValueFromProp)(calendarContainerWidth, 'rem') : undefined;
    var calendarContainerHeightValue = calendarContainerHeight ? (0, _utils.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.default.createElement(_RangeDatestyles.StyledPopover, {
            opened: innerIsOpen,
            frame: frame,
            usePortal: usePortal,
            onToggle: onToggle,
            offset: offset,
            placement: (0, _utils.getPlacements)(placement, false),
            trigger: "click",
            closeOnOverlayClick: closeOnOverlayClick,
            isFocusTrapped: false,
            target: target,
            preventOverflow: false,
            zIndex: zIndex,
            innerWidth: calendarContainerWidthValue,
            innerHeight: calendarContainerHeightValue
        }, /*#__PURE__*/ _react.default.createElement(Root, {
            ref: doubleCalendarRootRef,
            className: (0, _classnames.default)(_DatePickertokens.classes.datePickerRoot, _define_property({}, _DatePickertokens.classes.datePickerstretched, stretched)),
            onClick: handleCalendarRootClick
        }, (dateShortcuts === null || dateShortcuts === void 0 ? void 0 : 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)(_define_property({}, _DatePickertokens.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.default.createElement(_RangeDatestyles.StyledPopover, {
        opened: innerIsOpen,
        frame: frame,
        usePortal: usePortal,
        onToggle: onToggle,
        offset: offset,
        placement: (0, _utils.getPlacements)(placement),
        trigger: "click",
        closeOnOverlayClick: closeOnOverlayClick,
        isFocusTrapped: false,
        target: target,
        preventOverflow: false,
        closeOnEsc: closeOnEsc,
        zIndex: zIndex,
        innerWidth: calendarContainerWidthValue,
        innerHeight: calendarContainerHeightValue
    }, /*#__PURE__*/ _react.default.createElement(Root, {
        ref: calendarRootRef,
        className: (0, _classnames.default)(_DatePickertokens.classes.datePickerRoot, _define_property({}, _DatePickertokens.classes.datePickerstretched, stretched)),
        onClick: handleCalendarRootClick
    }, (dateShortcuts === null || dateShortcuts === void 0 ? void 0 : 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)(_define_property({}, _DatePickertokens.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
    })));
};
