"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dropdown = void 0;
var react_1 = __importStar(require("react"));
var react_hooks_1 = require("@devesharp/react-hooks");
var react_utils_1 = require("@devesharp/react-utils");
var Dropdown_style_1 = __importDefault(require("./Dropdown.style"));
var Dropdown = function (_a) {
    var children = _a.children, options = _a.options, onClick = _a.onClick;
    var _b = react_1.useState(false), open = _b[0], setOpen = _b[1];
    var dropdownRef = react_1.useRef(null);
    react_hooks_1.useClickOutside(function () { return setOpen(false); }, dropdownRef);
    function toggleMenu() {
        setOpen(!open);
    }
    function clickItem(item) {
        setOpen(false);
        onClick(item);
    }
    react_1.useEffect(function () {
        if (open) {
            var el = document.getElementsByClassName('ds-dropdown-menu')[0];
            var scrollParent = react_utils_1.GetScrollParent(dropdownRef.current.parentNode);
            var _a = dropdownRef.current.getBoundingClientRect(), topMain = _a.top, heightMain = _a.height;
            var heightScroll = scrollParent.getBoundingClientRect().height;
            if (topMain + heightMain + el.getBoundingClientRect().height > heightScroll) {
                if (el.scrollIntoView)
                    el.scrollIntoView({ block: 'end', behavior: 'smooth' });
            }
        }
    }, [open]);
    return (react_1.default.createElement(Dropdown_style_1.default, { "data-cy": "dropdown", ref: dropdownRef, className: "dropdown-container" },
        react_1.default.createElement("span", { onClick: toggleMenu, "data-cy": "dropdown-btn" }, children),
        open && (react_1.default.createElement("div", { className: "ds-dropdown-menu", "data-testid": "dropmenu-popup" }, options.map(function (e, i) { return (react_1.default.createElement("div", { key: i, onClick: function () { return clickItem(e); }, className: "menu-item", "data-testid": "dropmenu-menu-" + i }, e.name)); })))));
};
exports.Dropdown = Dropdown;
//# sourceMappingURL=Dropdown.js.map