"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 = exports.useClickOutside = void 0;
var react_1 = __importStar(require("react"));
var react_dom_1 = __importDefault(require("react-dom"));
var react_popper_1 = require("react-popper");
var GetScrollParent_1 = require("@devesharp/react-utils/lib/JSUtils/GetScrollParent");
var Dropdown_style_1 = __importDefault(require("./Dropdown.style"));
function useClickOutside(fn, ref) {
    (0, react_1.useEffect)(function () {
        function handleClickOutside(event) {
            if (ref.current && !ref.current.contains(event.target)) {
                fn();
            }
        }
        document.addEventListener('mouseup', handleClickOutside);
        return function () {
            document.removeEventListener('mouseup', handleClickOutside);
        };
    }, [fn]);
}
exports.useClickOutside = useClickOutside;
var Dropdown = function (_a) {
    var openProps = _a.open, onChange = _a.onChange, children = _a.children, options = _a.options, onClick = _a.onClick, Header = _a.Header, _b = _a.placement, placement = _b === void 0 ? 'bottom-end' : _b;
    var _c = (0, react_1.useState)(false), _open = _c[0], _setOpen = _c[1];
    var open = openProps != undefined ? openProps : _open;
    var setOpen = onChange != undefined ? onChange : _setOpen;
    var dropdownRef = (0, react_1.useRef)(null);
    var _d = (0, react_1.useState)(null), referenceElement = _d[0], setReferenceElement = _d[1];
    var _e = (0, react_1.useState)(null), popperElement = _e[0], setPopperElement = _e[1];
    var _f = (0, react_popper_1.usePopper)(referenceElement, popperElement, {
        placement: placement,
        modifiers: [
            {
                name: 'preventOverflow',
                options: {
                    altAxis: false,
                },
            },
            {
                name: 'offset',
                options: {
                    offset: [0, 0],
                },
            },
        ],
    }), styles = _f.styles, attributes = _f.attributes;
    useClickOutside(function () {
        if (open) {
            setTimeout(function () {
                setOpen(false);
            }, 100);
        }
    }, dropdownRef);
    function toggleMenu() {
        setOpen(!open);
    }
    function clickItem(item) {
        setOpen(false);
        onClick(item);
    }
    (0, react_1.useEffect)(function () {
        if (open) {
            var el = document.getElementsByClassName('ds-dropdown-menu')[0];
            var scrollParent = (0, GetScrollParent_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-testid": "dropdown", ref: dropdownRef, className: "dropdown-container" },
        react_1.default.createElement("span", { onClick: toggleMenu, "data-testid": "dropdown-btn", ref: setReferenceElement }, children),
        open &&
            react_dom_1.default.createPortal(react_1.default.createElement(Dropdown_style_1.default, { style: { display: 'block' } },
                react_1.default.createElement("div", __assign({ className: "ds-dropdown-menu", "data-testid": "dropmenu-popup", ref: setPopperElement, style: styles.popper }, attributes.popper),
                    Header && react_1.default.createElement(Header, null),
                    options.map(function (e, i) { return (react_1.default.createElement("div", { key: i, onClick: function () { return (e.disabled ? setOpen(false) : clickItem(e)); }, className: "menu-item ".concat(e.disabled ? 'disabled' : ''), "data-testid": "dropmenu-menu-".concat(i) }, e.name)); }))), document.body)));
};
exports.Dropdown = Dropdown;
//# sourceMappingURL=Dropdown.js.map