"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SettingsHandler = exports.EDEN_SETTINGS_APP_ID = void 0;
const ipc_1 = require("../ipc");
/**
 * Reserved app ID for Eden system settings.
 * No external app can use this ID.
 */
exports.EDEN_SETTINGS_APP_ID = "com.eden";
/**
 * SettingsHandler - IPC layer for settings operations
 *
 * Provides multiple sets of endpoints:
 * 1. Regular endpoints - require "settings/rw" permission, scoped to caller's appId
 * 2. Superuser endpoints - require "settings/superuser" permission for cross-namespace access
 *
 * Eden system settings use appId "com.eden" via superuser endpoints.
 */
let SettingsHandler = class SettingsHandler {
    settingsManager;
    constructor(settingsManager) {
        this.settingsManager = settingsManager;
    }
    // ===================================================================
    // Regular Endpoints - Scoped to caller's app
    // ===================================================================
    /**
     * Get a setting value (scoped to caller's app)
     */
    async handleGet(args) {
        const { key } = args;
        const value = await this.settingsManager.get(args._callerAppId, key);
        return { value };
    }
    /**
     * Set a setting value (scoped to caller's app)
     */
    async handleSet(args) {
        const { key, value } = args;
        await this.settingsManager.set(args._callerAppId, key, value);
        return { success: true };
    }
    /**
     * List all settings keys (scoped to caller's app)
     */
    async handleList(args) {
        const keys = await this.settingsManager.list(args._callerAppId);
        return { keys };
    }
    /**
     * Get all settings with values (scoped to caller's app)
     */
    async handleGetAll(args) {
        const settings = await this.settingsManager.getAll(args._callerAppId);
        return { settings };
    }
    /**
     * Reset a setting to default (scoped to caller's app)
     */
    async handleReset(args) {
        const { key, schema } = args;
        await this.settingsManager.reset(args._callerAppId, key, schema);
        return { success: true };
    }
    // ===================================================================
    // Superuser Endpoints - Cross-namespace access
    // ===================================================================
    /**
     * Get a setting from any app's namespace (superuser only)
     */
    async handleGetSuperuser(args) {
        const { appId, key } = args;
        const value = await this.settingsManager.get(appId, key);
        return { value };
    }
    /**
     * Set a setting in any app's namespace (superuser only)
     */
    async handleSetSuperuser(args) {
        const { appId, key, value } = args;
        await this.settingsManager.set(appId, key, value);
        return { success: true };
    }
    /**
     * List all settings in any app's namespace (superuser only)
     */
    async handleListSuperuser(args) {
        const { appId } = args;
        const keys = await this.settingsManager.list(appId);
        return { keys };
    }
    /**
     * Get all settings with values for any app (superuser only)
     */
    async handleGetAllSuperuser(args) {
        const { appId } = args;
        const settings = await this.settingsManager.getAll(appId);
        return { settings };
    }
    /**
     * Reset a setting for any app (superuser only)
     */
    async handleResetSuperuser(args) {
        const { appId, key, schema } = args;
        await this.settingsManager.reset(appId, key, schema);
        return { success: true };
    }
    // ===================================================================
    // Eden Schema Endpoint
    // ===================================================================
    /**
     * Get the Eden settings schema
     */
    async handleSchema() {
        const schema = this.settingsManager.getEdenSchema();
        return { schema };
    }
};
exports.SettingsHandler = SettingsHandler;
__decorate([
    (0, ipc_1.EdenHandler)("get", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleGet", null);
__decorate([
    (0, ipc_1.EdenHandler)("set", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleSet", null);
__decorate([
    (0, ipc_1.EdenHandler)("list", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleList", null);
__decorate([
    (0, ipc_1.EdenHandler)("get-all", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleGetAll", null);
__decorate([
    (0, ipc_1.EdenHandler)("reset", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleReset", null);
__decorate([
    (0, ipc_1.EdenHandler)("get/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleGetSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("set/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleSetSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("list/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleListSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("get-all/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleGetAllSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("reset/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleResetSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("schema"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], SettingsHandler.prototype, "handleSchema", null);
exports.SettingsHandler = SettingsHandler = __decorate([
    (0, ipc_1.EdenNamespace)("settings"),
    __metadata("design:paramtypes", [Function])
], SettingsHandler);
//# sourceMappingURL=SettingsHandler.js.map