(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", "react"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.useOnClickOutside = void 0;
    var react_1 = require("react");
    function useOnClickOutside(ref, handler, isOpen) {
        (0, react_1.useEffect)(function () {
            var listener = function (event) {
                var _a, _b;
                var mouseEvent = event;
                var isScrollBarClick = (((_a = window.scrollbars) === null || _a === void 0 ? void 0 : _a.visible) !== undefined
                    ? window.scrollbars.visible
                    : true) &&
                    mouseEvent.clientX > document.documentElement.clientWidth - 20;
                if (!Array.isArray(ref)) {
                    var isModalWindowClick = (_b = ref.current) === null || _b === void 0 ? void 0 : _b.contains(event.target);
                    if (!ref.current || isModalWindowClick || isScrollBarClick) {
                        return;
                    }
                }
                else {
                    var containsArray = ref.filter(function (r) { var _a; return (_a = r.current) === null || _a === void 0 ? void 0 : _a.contains(event.target); });
                    if (containsArray.length || isScrollBarClick) {
                        return;
                    }
                }
                handler && handler();
            };
            var isUseEventListener = isOpen !== undefined ? isOpen : true;
            if (isUseEventListener) {
                document.addEventListener("mousedown", listener);
                document.addEventListener("touchstart", listener);
            }
            return function () {
                if (isUseEventListener) {
                    document.removeEventListener("mousedown", listener);
                    document.removeEventListener("touchstart", listener);
                }
            };
        }, [ref, handler, isOpen]);
    }
    exports.useOnClickOutside = useOnClickOutside;
});
