window.kebabMenu = {
    /**
     * Registers the kebab menu event handler for closing on outside clicks.
     * @param {HTMLElement} element - The menu element.
     * @param {object} dotNetRef - The .NET object reference.
     * @returns {object} An object with a dispose method.
     */
    register: function (element, dotNetRef) {
        if (!element) {
            return null;
        }

        const handler = (event) => {
            if (!element.contains(event.target)) {
                dotNetRef.invokeMethodAsync('Close');
            }
        };

        document.addEventListener('click', handler, true);

        return {
            // Disposes the event listener.
            dispose: function () {
                document.removeEventListener('click', handler, true);
            }
        };
    }
};
