"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.AppChannelHandler = void 0;
const ipc_1 = require("../ipc");
/**
 * AppChannelHandler
 *
 * IPC handler for AppBus operations.
 * Exposes service registration and channel connection
 * via Eden's decorator-based IPC system.
 */
let AppChannelHandler = class AppChannelHandler {
    channelManager;
    constructor(channelManager) {
        this.channelManager = channelManager;
    }
    // ===================================================================
    // Service Registration
    // ===================================================================
    /**
     * Register a service that this app exposes
     * Requires "appbus/expose" permission
     */
    async handleRegister(args) {
        try {
            this.channelManager.registerService(args._callerAppId, args.serviceName, args._callerWebContentsId, {
                description: args.description,
                allowedClients: args.allowedClients,
            });
            return { success: true };
        }
        catch (error) {
            return {
                success: false,
                error: error instanceof Error ? error.message : String(error),
            };
        }
    }
    /**
     * Unregister a service
     * Requires "appbus/expose" permission
     */
    async handleUnregister(args) {
        const deleted = this.channelManager.unregisterService(args._callerAppId, args.serviceName);
        return { success: deleted };
    }
    // ===================================================================
    // Service Discovery
    // ===================================================================
    /**
     * List all available services
     * No permission required
     */
    async handleList() {
        return { services: this.channelManager.listServices() };
    }
    /**
     * List services by app ID
     * No permission required
     */
    async handleListByApp(args) {
        return { services: this.channelManager.getServicesByApp(args.appId) };
    }
    // ===================================================================
    // Channel Connection
    // ===================================================================
    /**
     * Connect to another app's service
     * Creates MessageChannel and transfers ports directly to both apps
     * Requires "appbus/connect" permission
     */
    async handleConnect(args) {
        return this.channelManager.connect(args._callerAppId, args._callerWebContentsId, args.targetAppId, args.serviceName);
    }
};
exports.AppChannelHandler = AppChannelHandler;
__decorate([
    (0, ipc_1.EdenHandler)("register", { permission: "expose" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], AppChannelHandler.prototype, "handleRegister", null);
__decorate([
    (0, ipc_1.EdenHandler)("unregister", { permission: "expose" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], AppChannelHandler.prototype, "handleUnregister", null);
__decorate([
    (0, ipc_1.EdenHandler)("list"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], AppChannelHandler.prototype, "handleList", null);
__decorate([
    (0, ipc_1.EdenHandler)("list-by-app"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], AppChannelHandler.prototype, "handleListByApp", null);
__decorate([
    (0, ipc_1.EdenHandler)("connect", { permission: "connect" }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], AppChannelHandler.prototype, "handleConnect", null);
exports.AppChannelHandler = AppChannelHandler = __decorate([
    (0, ipc_1.EdenNamespace)("appbus"),
    __metadata("design:paramtypes", [Function])
], AppChannelHandler);
//# sourceMappingURL=AppChannelHandler.js.map