import { createDocumentStore } from "../state/state-backend.js";
export class SessionWorktreeStore {
    store;
    records = new Map();
    integrations = new Map();
    constructor(workspace, backend = "json") {
        this.store = createDocumentStore({
            workspace,
            backend,
            fileName: "session-worktrees.json",
            sqliteKey: "session-worktrees",
        });
        this.load();
    }
    list() {
        return [...this.records.values()].sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
    }
    listIntegrations() {
        return [...this.integrations.values()].sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
    }
    get(id) {
        return this.records.get(id);
    }
    getIntegration(id) {
        return this.integrations.get(id);
    }
    delete(id) {
        const removed = this.records.delete(id);
        if (removed)
            this.persist();
        return removed;
    }
    deleteIntegration(id) {
        const removed = this.integrations.delete(id);
        if (removed)
            this.persist();
        return removed;
    }
    findByThreadId(threadId) {
        if (!threadId) {
            return undefined;
        }
        return this.list().find((record) => record.threadId === threadId);
    }
    findByWorkspace(workspace) {
        if (!workspace) {
            return undefined;
        }
        const normalized = normalizePath(workspace);
        return this.list().find((record) => normalizePath(record.worktreePath) === normalized);
    }
    upsert(record) {
        this.records.set(record.id, { ...record, updatedAt: record.updatedAt || new Date().toISOString() });
        this.persist();
        return this.records.get(record.id);
    }
    patch(id, patch) {
        const existing = this.get(id);
        if (!existing) {
            throw new Error(`Unknown session worktree: ${id}`);
        }
        const next = {
            ...existing,
            ...patch,
            updatedAt: new Date().toISOString(),
        };
        this.records.set(id, next);
        this.persist();
        return next;
    }
    upsertIntegration(run) {
        this.integrations.set(run.id, { ...run, updatedAt: run.updatedAt || new Date().toISOString() });
        this.persist();
        return this.integrations.get(run.id);
    }
    patchIntegration(id, patch) {
        const existing = this.getIntegration(id);
        if (!existing) {
            throw new Error(`Unknown worktree integration run: ${id}`);
        }
        const next = {
            ...existing,
            ...patch,
            updatedAt: new Date().toISOString(),
        };
        this.integrations.set(id, next);
        this.persist();
        return next;
    }
    load() {
        const data = this.store.read();
        if (!data || typeof data !== "object") {
            return;
        }
        if (Array.isArray(data.records)) {
            for (const record of data.records) {
                if (record?.id) {
                    this.records.set(record.id, record);
                }
            }
        }
        if (Array.isArray(data.integrations)) {
            for (const run of data.integrations) {
                if (run?.id) {
                    this.integrations.set(run.id, run);
                }
            }
        }
    }
    persist() {
        this.store.write({
            records: this.list(),
            integrations: this.listIntegrations(),
        });
    }
}
function normalizePath(value) {
    return value.replace(/\\/g, "/").replace(/\/+$/, "");
}
