"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.DbHandler = void 0;
const ipc_1 = require("../ipc");
/**
 * DbHandler - IPC layer for database operations
 *
 * Provides two sets of endpoints:
 * 1. Regular endpoints - require "db/rw" permission, automatically scoped to caller's appId
 * 2. Superuser endpoints - require "db/superuser" permission for cross-namespace access
 */
let DbHandler = class DbHandler {
    dbManager;
    constructor(dbManager) {
        this.dbManager = dbManager;
    }
    // ===================================================================
    // Regular Endpoints - Scoped to caller's app
    // ===================================================================
    /**
     * Get a value from database (scoped to caller's app)
     */
    async handleGet(args) {
        const { key } = args;
        const value = await this.dbManager.get(args._callerAppId, key);
        return { value };
    }
    /**
     * Set a value in database (scoped to caller's app)
     */
    async handleSet(args) {
        const { key, value } = args;
        await this.dbManager.set(args._callerAppId, key, value);
        return { success: true };
    }
    /**
     * Delete a key from database (scoped to caller's app)
     */
    async handleDelete(args) {
        const { key } = args;
        const deleted = await this.dbManager.delete(args._callerAppId, key);
        return { success: deleted };
    }
    /**
     * Check if a key exists (scoped to caller's app)
     */
    async handleHas(args) {
        const { key } = args;
        const exists = await this.dbManager.has(args._callerAppId, key);
        return { exists };
    }
    /**
     * Clear all keys (scoped to caller's app)
     */
    async handleClear(args) {
        await this.dbManager.clear(args._callerAppId);
        return { success: true };
    }
    /**
     * List all keys (scoped to caller's app)
     */
    async handleList(args) {
        const keys = await this.dbManager.list(args._callerAppId);
        return { keys };
    }
    // ===================================================================
    // Superuser Endpoints - Cross-namespace access
    // ===================================================================
    /**
     * Get a value from any app's namespace (superuser only)
     */
    async handleGetSuperuser(args) {
        const { appId, key } = args;
        const value = await this.dbManager.get(appId, key);
        return { value };
    }
    /**
     * Set a value in any app's namespace (superuser only)
     */
    async handleSetSuperuser(args) {
        const { appId, key, value } = args;
        await this.dbManager.set(appId, key, value);
        return { success: true };
    }
    /**
     * Delete a key from any app's namespace (superuser only)
     */
    async handleDeleteSuperuser(args) {
        const { appId, key } = args;
        const deleted = await this.dbManager.delete(appId, key);
        return { success: deleted };
    }
    /**
     * Check if a key exists in any app's namespace (superuser only)
     */
    async handleHasSuperuser(args) {
        const { appId, key } = args;
        const exists = await this.dbManager.has(appId, key);
        return { exists };
    }
    /**
     * Clear all keys in any app's namespace (superuser only)
     */
    async handleClearSuperuser(args) {
        const { appId } = args;
        await this.dbManager.clear(appId);
        return { success: true };
    }
    /**
     * List all keys in any app's namespace (superuser only)
     */
    async handleListSuperuser(args) {
        const { appId } = args;
        const keys = await this.dbManager.list(appId);
        return { keys };
    }
};
exports.DbHandler = DbHandler;
__decorate([
    (0, ipc_1.EdenHandler)("get", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleGet", null);
__decorate([
    (0, ipc_1.EdenHandler)("set", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleSet", null);
__decorate([
    (0, ipc_1.EdenHandler)("delete", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleDelete", null);
__decorate([
    (0, ipc_1.EdenHandler)("has", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleHas", null);
__decorate([
    (0, ipc_1.EdenHandler)("clear", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleClear", null);
__decorate([
    (0, ipc_1.EdenHandler)("list", { permission: "rw" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleList", null);
__decorate([
    (0, ipc_1.EdenHandler)("get/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleGetSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("set/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleSetSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("delete/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleDeleteSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("has/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleHasSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("clear/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleClearSuperuser", null);
__decorate([
    (0, ipc_1.EdenHandler)("list/su", { permission: "superuser" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], DbHandler.prototype, "handleListSuperuser", null);
exports.DbHandler = DbHandler = __decorate([
    (0, ipc_1.EdenNamespace)("db"),
    __metadata("design:paramtypes", [Function])
], DbHandler);
//# sourceMappingURL=DbHandler.js.map