import Quickshell
import Quickshell.Io
import QtQuick
import qs.config

ShellRoot {
    // DesktopEntries populates asynchronously. Reading `applications.values`
    // here rather than calling byId() blind is the documented way to wait for
    // it -- and is exactly the trap noted in modules/settings/README.md.
    readonly property var ids: {
        const out = {};
        for (const entry of DesktopEntries.applications.values)
            out[entry.id] = entry;
        return out;
    }

    IpcHandler {
        target: "dock-pin-test"

        function count(): int { return DesktopEntries.applications.values.length; }

        function resolve(): string {
            const pinned = DesktopPreferences.get("dockPinned");
            const missing = [];
            let first = null;
            for (const id of pinned) {
                const entry = DesktopEntries.byId(id);
                if (!entry) missing.push(id);
                if (first === null)
                    first = { id: id, found: !!entry, name: entry ? entry.name : "", icon: entry ? entry.icon : "" };
            }
            return JSON.stringify({ total: pinned.length, first: first, missing: missing });
        }
    }
}
