// Adds an installed application to the Dock.
//
// Filtered rather than a full list: there are several hundred desktop entries
// on a normal system, and rendering them all into a page that is already
// scrolling is both slow and useless. Typing narrows; nothing shows until you
// do, which also keeps the card short when you are not using it.

import QtQuick
import Quickshell
import qs.config
import qs.modules.clipboard

Column {
    id: root

    spacing: 0

    required property var pinned

    signal picked(string id)

    readonly property var matches: {
        const needle = search.text.trim().toLowerCase();
        if (needle === "")
            return [];
        const out = [];
        for (const entry of DesktopEntries.applications.values) {
            if (entry.noDisplay)
                continue;
            if (root.pinned.indexOf(entry.id) >= 0)
                continue;
            if (String(entry.name).toLowerCase().indexOf(needle) >= 0)
                out.push(entry);
            if (out.length >= 8)
                break;
        }
        return out;
    }

    SearchField {
        id: search
        width: parent.width
        placeholder: "Search installed applications"
    }

    Repeater {
        model: root.matches

        SettingRow {
            id: candidate

            required property var modelData
            required property int index

            label: candidate.modelData.name
            detail: candidate.modelData.id
            divider: candidate.index < root.matches.length - 1
            controlWidth: 86

            SettingsButton {
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                text: "Pin"
                onClicked: {
                    root.picked(candidate.modelData.id);
                    search.text = "";
                }
            }
        }
    }
}
