// retoor <retoor@molodetz.nl>

export class ProjectActionsMenu {
    constructor(root) {
        this.root = root;
        this.trigger = root.querySelector(".project-actions-more");
        this.overflow = root.querySelector(".project-actions-overflow");
        if (!this.trigger || !this.overflow) {
            return;
        }
        if (!this.overflow.querySelector("[data-menu-action]")) {
            this.trigger.hidden = true;
            return;
        }
        this.trigger.addEventListener("click", (event) => this.open(event));
    }

    open(event) {
        event.preventDefault();
        event.stopPropagation();
        const items = Array.from(
            this.overflow.querySelectorAll("[data-menu-action]")
        ).map((element) => ({
            icon: element.dataset.menuIcon || "",
            label: element.dataset.menuLabel || element.textContent.trim(),
            danger: element.hasAttribute("data-confirm-danger"),
            onSelect: () => element.click(),
        }));
        const rect = this.trigger.getBoundingClientRect();
        window.app.contextMenu.open(rect.left, rect.bottom + 4, items);
    }
}
