export class MemoryRunStore {
    runs = new Map();
    async createRun(data) {
        const now = new Date().toISOString();
        const runId = data.id || `run-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
        const run = {
            id: runId,
            name: data.name || 'Test Run',
            status: 'created',
            input: data.input || null,
            output: null,
            logs: [],
            clientToken: data.clientToken || null,
            tags: data.tags || [],
            startedAt: null,
            completedAt: null,
            createdAt: now,
            updatedAt: now,
        };
        this.runs.set(runId, run);
        return run;
    }
    async getRunById(id) {
        return this.runs.get(id) || null;
    }
    async getRunByClientToken(clientToken) {
        for (const run of this.runs.values()) {
            if (run.clientToken === clientToken) {
                return run;
            }
        }
        return null;
    }
    async listRuns(filter) {
        let runs = Array.from(this.runs.values());
        if (filter?.tag) {
            runs = runs.filter(run => run.tags?.includes(filter.tag));
        }
        if (filter?.status) {
            runs = runs.filter(run => run.status === filter.status);
        }
        runs.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
        if (filter?.limit) {
            runs = runs.slice(0, filter.limit);
        }
        return runs;
    }
    async updateRunStatus(id, status, extra) {
        const run = this.runs.get(id);
        if (!run) {
            throw new Error(`Run not found: ${id}`);
        }
        run.status = status;
        run.updatedAt = new Date().toISOString();
        if (extra) {
            if (extra.input !== undefined)
                run.input = extra.input;
            if (extra.output !== undefined)
                run.output = extra.output;
            if (extra.logs !== undefined)
                run.logs = extra.logs;
            if (extra.startedAt !== undefined)
                run.startedAt = extra.startedAt;
            if (extra.completedAt !== undefined)
                run.completedAt = extra.completedAt;
        }
        if (status === 'running' && !run.startedAt) {
            run.startedAt = new Date().toISOString();
        }
        if ((status === 'completed' || status === 'failed') && !run.completedAt) {
            run.completedAt = new Date().toISOString();
        }
    }
    async deleteRun(id) {
        this.runs.delete(id);
    }
}
//# sourceMappingURL=memoryRunStore.js.map