// The dock's app menu. Desktop-entry actions stay first; the shell-owned
// configuration route is deliberately last so it never displaces app actions.

import Quickshell
import QtQuick
import qs.config
import qs.modules.bar
import qs.services
import qs.widgets

PopupWindow {
    id: root

    property Item anchorItem: null
    property var entry: null

    anchor.item: root.anchorItem
    anchor.edges: Edges.Top | Edges.Left
    anchor.gravity: Edges.Top | Edges.Right
    anchor.margins.bottom: 8

    implicitWidth: Math.max(menu.implicitWidth + Theme.popoverPadding * 2, 240)
    implicitHeight: menu.implicitHeight + Theme.popoverPadding * 2
    color: "transparent"
    visible: false
    grabFocus: true

    Rectangle {
        anchors.fill: parent
        radius: Theme.popoverRadius
        color: Theme.alpha(Theme.bgPopover, Theme.popoverAlpha)
        border.width: 1
        border.color: Theme.alpha(Theme.fg, 0.08)

        PrismEdge {
            anchors.top: parent.top
            anchors.topMargin: 1
            anchors.left: parent.left
            anchors.right: parent.right
            inset: parent.radius
        }

        Column {
            id: menu
            anchors.fill: parent
            anchors.margins: Theme.popoverPadding
            spacing: 2

            Repeater {
                id: applicationActions
                model: root.entry ? root.entry.actions : []

                delegate: TrayMenuRow {
                    required property var modelData

                    width: parent.width
                    label: modelData.name
                    onActivated: {
                        modelData.execute();
                        root.visible = false;
                    }
                }
            }

            Rectangle {
                width: parent.width
                height: 1
                anchors.margins: 3
                visible: applicationActions.count > 0
                border.width: 0
                color: Theme.alpha(Theme.fg, 0.1)
            }

            TrayMenuRow {
                width: parent.width
                label: "Dock settings"
                onActivated: {
                    ShellState.openSettings("desktop");
                    root.visible = false;
                }
            }
        }
    }
}
