"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncerService = void 0;
const AgentSyncFs_1 = require("./AgentSyncFs");
const AgentSyncKnowhowWeb_1 = require("./AgentSyncKnowhowWeb");
class SyncerService {
    fsSync;
    webSync;
    active = false;
    useWebSync = false;
    createdTaskId;
    constructor() {
        this.fsSync = new AgentSyncFs_1.AgentSyncFs();
        this.webSync = new AgentSyncKnowhowWeb_1.AgentSyncKnowhowWeb();
    }
    async createTask(options) {
        this.active = true;
        this.useWebSync = false;
        const shouldUseWebSync = !!options.messageId &&
            !options.syncFs &&
            !options.existingKnowhowTaskId;
        console.log(`📁 Using filesystem-based synchronization for task: ${options.taskId}`);
        const fsTaskId = await this.fsSync.createTask({
            taskId: options.taskId,
            prompt: options.prompt,
            agentName: options.agentName,
        });
        if (shouldUseWebSync) {
            const knowhowTaskId = await this.webSync.createChatTask({
                messageId: options.messageId,
                prompt: options.prompt,
            });
            if (knowhowTaskId) {
                this.useWebSync = true;
                this.createdTaskId = knowhowTaskId;
                console.log(`🌐 Web sync task created: ${knowhowTaskId}`);
            }
        }
        return fsTaskId;
    }
    async setupAgentSync(agent, taskId) {
        await this.fsSync.setupAgentSync(agent, taskId);
        if (this.useWebSync && this.createdTaskId) {
            await this.webSync.setupAgentSync(agent, this.createdTaskId);
        }
    }
    async waitForFinalization() {
        if (this.useWebSync) {
            console.log("🎯 [SyncerService] Waiting for web sync finalization...");
            await this.webSync.waitForFinalization();
            console.log("🎯 [SyncerService] Web sync finalization complete");
        }
        console.log("🎯 [SyncerService] Waiting for fs sync finalization...");
        await this.fsSync.waitForFinalization();
        console.log("🎯 [SyncerService] Fs sync finalization complete");
    }
    reset() {
        this.webSync.reset();
        this.fsSync.reset();
        this.active = false;
        this.useWebSync = false;
        this.createdTaskId = undefined;
    }
    isActive() {
        return this.active;
    }
    getCreatedWebTaskId() {
        return this.useWebSync ? this.createdTaskId : undefined;
    }
}
exports.SyncerService = SyncerService;
//# sourceMappingURL=SyncerService.js.map