import Quickshell
import Quickshell.Io
import QtQuick

import qs.config

ShellRoot {
    IpcHandler {
        target: "preference-schema-test"

        // Values arrive as a JSON object so the contract can exercise real
        // types -- booleans, integers, and strings -- through one entry point.
        // Returns the per-key result of set(), so a rejection is observable
        // rather than inferred from the value not changing.
        function applyJson(payload: string): string {
            const requested = JSON.parse(payload);
            const accepted = {};
            for (const key in requested)
                accepted[key] = DesktopPreferences.set(key, requested[key]);
            return JSON.stringify(accepted);
        }

        // Every schema key and its effective value.
        function dump(): string {
            const out = {};
            for (const entry of PreferenceSchema.entries)
                out[entry.key] = DesktopPreferences.get(entry.key);
            return JSON.stringify(out);
        }

        // The raw in-memory store, including keys this build does not know.
        function raw(): string {
            return JSON.stringify(DesktopPreferences.values);
        }

        function defaults(): string {
            return JSON.stringify(PreferenceSchema.defaults());
        }

        function reset(): void {
            DesktopPreferences.resetDesktopDefaults();
        }

        function keyCount(): int {
            return PreferenceSchema.entries.length;
        }
    }
}
