export class RelayQueueService {
    promptStore;
    contextKey;
    constructor(promptStore, contextKey) {
        this.promptStore = promptStore;
        this.contextKey = contextKey;
    }
    list() {
        return this.rawList().filter(isQueuedPromptWaiting).map(queueItemDto);
    }
    rawList() {
        return this.promptStore.list(this.contextKey);
    }
    length() {
        return this.rawList().filter(isQueuedPromptWaiting).length;
    }
    isPaused() {
        return this.promptStore.isPaused(this.contextKey);
    }
    acquireDrainLock(owner, ttlMs) {
        return this.promptStore.acquireDrainLock(this.contextKey, owner, ttlMs);
    }
    renewDrainLock(owner, ttlMs) {
        return this.promptStore.renewDrainLock(this.contextKey, owner, ttlMs);
    }
    releaseDrainLock(owner) {
        return this.promptStore.releaseDrainLock(this.contextKey, owner);
    }
    enqueue(envelope) {
        return this.promptStore.enqueue(this.contextKey, envelope);
    }
    enqueueFront(item) {
        this.promptStore.enqueueFront(this.contextKey, item);
    }
    dequeue() {
        return this.promptStore.dequeue(this.contextKey);
    }
    leaseNext(owner, ttlMs) {
        return this.promptStore.leaseNext(this.contextKey, owner, ttlMs);
    }
    renewLease(item, owner, ttlMs) {
        return this.promptStore.renewLease(this.contextKey, item, owner, ttlMs);
    }
    completeLease(item, owner) {
        return this.promptStore.completeLease(this.contextKey, item, owner);
    }
    failLease(item, owner, error) {
        return this.promptStore.failLease(this.contextKey, item, owner, error);
    }
    setLastPrompt(envelope) {
        this.promptStore.setLastPrompt(this.contextKey, envelope);
    }
    getLastPrompt() {
        return this.promptStore.getLastPrompt(this.contextKey);
    }
    apply(action, id) {
        if (action === "pause")
            this.promptStore.pause(this.contextKey);
        if (action === "resume")
            this.promptStore.resume(this.contextKey);
        if (action === "clear")
            this.promptStore.clear(this.contextKey);
        if (id && action === "cancel")
            this.promptStore.remove(this.contextKey, id);
        if (id && action === "top")
            this.promptStore.moveToTop(this.contextKey, id);
        if (id && action === "up")
            this.promptStore.moveUp(this.contextKey, id);
        if (id && action === "down")
            this.promptStore.moveDown(this.contextKey, id);
        if (id && action === "run") {
            const item = this.promptStore.remove(this.contextKey, id);
            if (item)
                this.promptStore.enqueueFront(this.contextKey, item);
        }
    }
}
export function isQueuedPromptWaiting(item) {
    return (item.status ?? "queued") === "queued";
}
export function queueItemDto(item) {
    return {
        id: item.id,
        description: item.description,
        status: item.status ?? "queued",
        createdAt: new Date(item.createdAt).toISOString(),
        updatedAt: item.updatedAt ? new Date(item.updatedAt).toISOString() : undefined,
        attempts: item.attempts ?? 0,
        correlationId: item.correlationId,
        idempotencyKey: item.idempotencyKey,
        notBefore: item.notBefore ? new Date(item.notBefore).toISOString() : undefined,
        lastError: item.lastError,
        leaseOwner: item.leaseOwner,
        leaseStartedAt: item.leaseStartedAt ? new Date(item.leaseStartedAt).toISOString() : undefined,
        leaseExpiresAt: item.leaseExpiresAt ? new Date(item.leaseExpiresAt).toISOString() : undefined,
    };
}
