// Output / input device picker. Writing Pipewire.preferredDefaultAudioSink is
// what actually moves audio; the default* properties are read-only reflections
// of what the session manager settled on.

import QtQuick
import Quickshell
import Quickshell.Services.Pipewire
import qs.config
import qs.services

Item {
    id: root

    // false → input devices.
    property bool output: true
    property alias maxHeight: list.maxHeight

    implicitHeight: list.implicitHeight

    readonly property var nodes: root.output ? AudioDevices.outputs : AudioDevices.inputs

    readonly property var current: AudioDevices.current(root.output)

    function select(node): void {
        AudioDevices.select(root.output, node)
    }

    ScrollColumn {
        id: list
        anchors.fill: parent
        maxHeight: 220

        Repeater {
            model: root.nodes

            RowButton {
                id: nodeRow

                required property var modelData

                width: parent.width
                implicitHeight: 38
                icon: root.output ? "audio-speakers-symbolic" : "audio-input-microphone-symbolic"
                iconFallback: "audio-card-symbolic"
                label: AudioDevices.label(nodeRow.modelData)
                selected: nodeRow.modelData === root.current
                onClicked: root.select(nodeRow.modelData)
            }
        }
    }
}
