import { normalizeAgentPatchEnvelope, validateAgentPatchEnvelope } from './agent-patch-schema.js';
export const AGENT_PATCH_QUEUE_SCHEMA = 'sks.agent-patch-queue.v1';
export class InMemoryAgentPatchQueue {
    entries = [];
    events = [];
    enqueue(input, context = {}) {
        const envelope = normalizeAgentPatchEnvelope(input);
        const validation = validateAgentPatchEnvelope(envelope);
        const now = new Date().toISOString();
        const leaseId = envelope.lease_id || envelope.lease_proof?.lease_id;
        const entry = {
            id: `${envelope.agent_id}-${String(this.entries.length + 1).padStart(4, '0')}`,
            ...(context.mission_id ? { mission_id: context.mission_id } : {}),
            ...(context.route ? { route: context.route } : {}),
            agent_id: envelope.agent_id,
            ...(envelope.session_id ? { session_id: envelope.session_id } : {}),
            ...(envelope.slot_id ? { slot_id: envelope.slot_id } : {}),
            ...(envelope.generation_index === undefined ? {} : { generation_index: envelope.generation_index }),
            ...(leaseId ? { lease_id: leaseId } : {}),
            write_paths: envelope.operations.map((operation) => operation.path),
            envelope,
            status: validation.ok ? 'pending' : 'rejected',
            violations: validation.violations,
            created_at: now,
            updated_at: now
        };
        this.entries.push(entry);
        this.record(entry, 'enqueue');
        return entry;
    }
    queued() {
        return this.entries.filter((entry) => entry.status === 'pending');
    }
    markApplying(id) {
        this.transition(id, 'applying');
    }
    markApplied(id) {
        this.transition(id, 'applied');
    }
    markVerified(id) {
        this.transition(id, 'verified');
    }
    markConflicted(id, violations = []) {
        const entry = this.entries.find((item) => item.id === id);
        if (entry) {
            entry.status = 'conflicted';
            entry.violations.push(...violations);
            entry.updated_at = new Date().toISOString();
            this.record(entry, 'conflict');
        }
    }
    markRolledBack(id) {
        this.transition(id, 'rolled_back');
    }
    toJSON() {
        return {
            schema: AGENT_PATCH_QUEUE_SCHEMA,
            entries: this.entries,
            queued_count: this.queued().length,
            events: this.events,
            ownership_ledger: buildAgentPatchOwnershipLedger(this.entries)
        };
    }
    transition(id, status) {
        const entry = this.entries.find((item) => item.id === id);
        if (!entry)
            return;
        entry.status = status;
        entry.updated_at = new Date().toISOString();
        this.record(entry, status);
    }
    record(entry, eventType) {
        this.events.push({
            ts: new Date().toISOString(),
            entry_id: entry.id,
            event_type: eventType,
            status: entry.status,
            violations: [...entry.violations]
        });
    }
}
export function buildAgentPatchOwnershipLedger(entries) {
    return entries.map((entry) => ({
        entry_id: entry.id,
        mission_id: entry.mission_id || null,
        route: entry.route || null,
        agent_id: entry.agent_id,
        session_id: entry.session_id || null,
        slot_id: entry.slot_id || null,
        generation_index: entry.generation_index ?? null,
        lease_id: entry.lease_id || null,
        write_paths: [...entry.write_paths],
        status: entry.status,
        created_at: entry.created_at,
        updated_at: entry.updated_at,
        violations: [...entry.violations]
    }));
}
//# sourceMappingURL=agent-patch-queue.js.map