(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "react-dom", "react-transition-group", "./styles", "./components/DropdownPortal", "./helpers"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_dom_1 = require("react-dom");
    var react_transition_group_1 = require("react-transition-group");
    var styles_1 = require("./styles");
    var DropdownPortal_1 = (0, tslib_1.__importDefault)(require("./components/DropdownPortal"));
    var helpers_1 = require("./helpers");
    var Dropdown = function (_a) {
        var children = _a.children, _b = _a.direction, direction = _b === void 0 ? "bottom" : _b, onItemClick = _a.onItemClick, onClickClose = _a.onClickClose, onClickOutside = _a.onClickOutside, isOpen = _a.isOpen, props = (0, tslib_1.__rest)(_a, ["children", "direction", "onItemClick", "onClickClose", "onClickOutside", "isOpen"]);
        var _c = (0, tslib_1.__read)((0, react_1.useState)(isOpen || false), 2), _isOpen = _c[0], setIsOpen = _c[1];
        var _d = (0, tslib_1.__read)((0, react_1.useState)(null), 2), containerDropdown = _d[0], setContainerDropdown = _d[1];
        var anchorRef = (0, react_1.useRef)(null);
        var _e = (0, tslib_1.__read)((0, react_1.useState)(helpers_1.DEFAULT_RECT_ELEMENT), 2), anchorRect = _e[0], setAnchorRect = _e[1];
        var handleClickOutsideClose = function () {
            setIsOpen(false);
            if (_isOpen) {
                onClickOutside === null || onClickOutside === void 0 ? void 0 : onClickOutside();
                onClickClose === null || onClickClose === void 0 ? void 0 : onClickClose();
            }
        };
        var calculateAnchorRect = function () {
            setAnchorRect((0, helpers_1.calculateRectElement)(anchorRef.current));
        };
        (0, react_1.useLayoutEffect)(function () {
            calculateAnchorRect();
        }, [_isOpen]);
        (0, react_1.useEffect)(function () {
            window.addEventListener("resize", calculateAnchorRect);
            return function () { return window.removeEventListener("resize", calculateAnchorRect); };
        }, []);
        (0, react_1.useEffect)(function () {
            var container = document.getElementsByTagName("body").item(0);
            setContainerDropdown(container);
        }, []);
        (0, react_1.useEffect)(function () {
            if (typeof isOpen !== "undefined") {
                setIsOpen(isOpen);
            }
        }, [isOpen]);
        var handleClick = function () {
            setIsOpen(function (prevState) { return !prevState; });
            if (_isOpen) {
                onClickClose === null || onClickClose === void 0 ? void 0 : onClickClose();
            }
        };
        return (react_1.default.createElement(styles_1.DropdownWrapper, { ref: anchorRef },
            (0, react_1.cloneElement)(children, {
                onClick: function () {
                    var _a, _b;
                    handleClick();
                    (_b = (_a = children.props) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a);
                }
            }),
            _isOpen &&
                containerDropdown &&
                (0, react_dom_1.createPortal)(react_1.default.createElement(react_transition_group_1.Transition, { in: _isOpen, unmountOnExit: true, timeout: 500 },
                    react_1.default.createElement(DropdownPortal_1.default, (0, tslib_1.__assign)({}, props, { direction: direction, anchorRect: anchorRect, onItemClick: onItemClick, anchorRef: anchorRef, onClickOutsideClose: handleClickOutsideClose }))), containerDropdown)));
    };
    Dropdown.displayName = "Dropdown";
    exports.default = Dropdown;
});
