"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = require("react");
const useProperty_1 = tslib_1.__importDefault(require("../utils/useProperty"));
const getAvailableSizeInfo_1 = tslib_1.__importDefault(require("../utils/getAvailableSizeInfo"));
const OverlayTrigger_1 = require("../OverlayTrigger");
exports.default = (props, positionerRef) => {
    const positionInfoRef = (0, react_1.useRef)({
        maxHeight: '50vh',
        maxWidth: '50vw',
        verticalPosition: 'bottom',
        horizontalPosition: 'right',
    });
    const updatePosition = () => {
        const positionRect = positionerRef.current.getBoundingClientRect();
        positionInfoRef.current = (0, getAvailableSizeInfo_1.default)({
            constrainRect: (0, OverlayTrigger_1.getConstrainRect)(positionerRef.current, props.constrainTo),
            targetRect: positionRect,
            maxSizeOffset: 30,
        });
    };
    const [expanded, doSetExpanded] = (0, useProperty_1.default)(props, 'expanded', false, {
        onChange: (expanded) => {
            if (expanded) {
                if (props.onExpand) {
                    props.onExpand();
                }
            }
            else if (props.onCollapse) {
                props.onCollapse();
            }
            if (props.onToggle) {
                props.onToggle(expanded);
            }
            if (props.onExpandedChange) {
                props.onExpandedChange(expanded);
            }
        },
    });
    const setExpanded = (newExpanded) => {
        // if (!newExpanded) {
        //   return;
        // }
        if (!expanded && newExpanded) {
            updatePosition();
        }
        doSetExpanded(newExpanded);
    };
    const toggle = () => {
        setExpanded(!expanded);
    };
    return {
        expanded,
        setExpanded,
        toggle,
        positionInfo: positionInfoRef.current,
    };
};
