"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useClickAway = void 0;
const react_1 = __importDefault(require("react"));
/**
 * Detects clicks outside of a specified component and triggers a callback function.
 *
 * @param ref Component reference
 * @param onClickAway Callback function to be called when a click is detected outside the component
 */
const useClickAway = (ref, onClickAway) => {
    const handleClick = react_1.default.useCallback((event) => {
        if (ref.current && !ref.current.contains(event.target)) {
            onClickAway(event);
        }
    }, [ref, onClickAway]);
    react_1.default.useEffect(() => {
        document.addEventListener('mousedown', handleClick);
        document.addEventListener('touchend', handleClick);
        return () => {
            document.removeEventListener('mousedown', handleClick);
            document.removeEventListener('touchend', handleClick);
        };
    }, [ref, handleClick]);
};
exports.useClickAway = useClickAway;
