"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.DbSnapshotStorage = void 0;
const di_1 = require("@n8n/di");
const n8n_workflow_1 = require("n8n-workflow");
const instance_ai_run_snapshot_repository_1 = require("../repositories/instance-ai-run-snapshot.repository");
let DbSnapshotStorage = class DbSnapshotStorage {
    constructor(repo) {
        this.repo = repo;
    }
    async getLatest(threadId, options = {}) {
        const { messageGroupId, runId } = options;
        const row = messageGroupId
            ? await this.repo.findOne({
                where: { threadId, messageGroupId },
                order: { createdAt: 'DESC' },
            })
            : runId
                ? await this.repo.findOne({
                    where: { threadId, runId },
                    order: { createdAt: 'DESC' },
                })
                : await this.repo.findOne({
                    where: { threadId },
                    order: { createdAt: 'DESC' },
                });
        if (!row)
            return undefined;
        return {
            tree: (0, n8n_workflow_1.jsonParse)(row.tree),
            runId: row.runId,
            messageGroupId: row.messageGroupId ?? undefined,
            runIds: row.runIds ?? undefined,
        };
    }
    async save(threadId, agentTree, runId, messageGroupId, runIds) {
        await this.repo.upsert({
            threadId,
            runId,
            messageGroupId: messageGroupId ?? null,
            runIds: runIds ?? null,
            tree: JSON.stringify(agentTree),
        }, ['threadId', 'runId']);
    }
    async updateLast(threadId, agentTree, runId, messageGroupId, runIds) {
        if (messageGroupId) {
            const existing = await this.repo.findOne({
                where: { threadId, messageGroupId },
                order: { createdAt: 'DESC' },
            });
            if (existing) {
                await this.repo.update({ threadId: existing.threadId, runId: existing.runId }, {
                    runId,
                    tree: JSON.stringify(agentTree),
                    messageGroupId,
                    runIds: runIds ?? existing.runIds,
                });
                return;
            }
        }
        const byRunId = await this.repo.findOneBy({ threadId, runId });
        if (byRunId) {
            await this.repo.update({ threadId, runId }, {
                tree: JSON.stringify(agentTree),
                messageGroupId: messageGroupId ?? byRunId.messageGroupId,
                runIds: runIds ?? byRunId.runIds,
            });
            return;
        }
        await this.save(threadId, agentTree, runId, messageGroupId, runIds);
    }
    async getAll(threadId) {
        const rows = await this.repo.find({
            where: { threadId },
            order: { createdAt: 'ASC' },
        });
        return rows.map((r) => ({
            tree: (0, n8n_workflow_1.jsonParse)(r.tree),
            runId: r.runId,
            messageGroupId: r.messageGroupId ?? undefined,
            runIds: r.runIds ?? undefined,
        }));
    }
};
exports.DbSnapshotStorage = DbSnapshotStorage;
exports.DbSnapshotStorage = DbSnapshotStorage = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [instance_ai_run_snapshot_repository_1.InstanceAiRunSnapshotRepository])
], DbSnapshotStorage);
//# sourceMappingURL=db-snapshot-storage.js.map