// 聊天室管理菜单事件绑定，替代 input-bar 中的管理类内联 onclick。
// 管理动作业务逻辑已迁至 admin-commands.js。

import "./admin-commands.js";

let adminMenuEventsBound = false;

/**
 * 绑定管理菜单、管理动作与全屏特效选择事件。
 */
export function bindAdminMenuControls() {
    if (adminMenuEventsBound || typeof document === "undefined") {
        return;
    }

    adminMenuEventsBound = true;
    document.addEventListener("click", (event) => {
        if (!(event.target instanceof Element)) {
            return;
        }

        const menuToggle = event.target.closest("[data-chat-admin-menu-toggle]");
        if (menuToggle) {
            event.preventDefault();
            window.toggleAdminMenu?.(event);
            return;
        }

        const adminAction = event.target.closest("[data-chat-admin-action]");
        if (adminAction) {
            event.preventDefault();
            const action = adminAction.getAttribute("data-chat-admin-action") || "";
            if (action && typeof window.runAdminAction === "function") {
                window.runAdminAction(action);
            }
            return;
        }

        const effectButton = event.target.closest("[data-chat-admin-effect]");
        if (effectButton) {
            event.preventDefault();
            const effect = effectButton.getAttribute("data-chat-admin-effect") || "";
            if (effect && typeof window.selectEffect === "function") {
                window.selectEffect(effect);
            }
            return;
        }

        const effectPreviewButton = event.target.closest("[data-chat-admin-effect-preview]");
        if (effectPreviewButton) {
            event.preventDefault();
            const effect = effectPreviewButton.getAttribute("data-chat-admin-effect-preview") || "";
            const menu = document.getElementById("admin-menu");
            if (menu) {
                menu.style.display = "none";
            }
            // 预览按钮仅在当前浏览器播放，方便测试新特效时不打扰房间其他用户。
            window.EffectManager?.play?.(effect);
            return;
        }

        if (event.target.closest("[data-chat-admin-menu]")) {
            event.stopPropagation();
        }
    });
}
