import { createSession as apiCreateSession, listSessions as apiListSessions, listMessages as apiListMessages, createMessage as apiCreateMessage, abortSession as apiAbortSession, deleteSession as apiDeleteSession, updateSession as apiUpdateSession, getSessionQueue as apiGetSessionQueue, removeFromQueue as apiRemoveFromQueue, retryMessage as apiRetryMessage, } from '@ottocode/api';
import { extractErrorMessage, convertSession, convertMessage, getBaseUrl, } from './utils';
export const sessionsMixin = {
    async getSessions() {
        const page = await this.getSessionsPage({ limit: 200 });
        return page.items;
    },
    async getSessionsPage(params = {}) {
        const { limit = 50, offset = 0 } = params;
        const response = await apiListSessions({
            // biome-ignore lint/suspicious/noExplicitAny: API query type mismatch
            query: { limit, offset },
        });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        // biome-ignore lint/suspicious/noExplicitAny: API response structure
        const data = response.data;
        return {
            items: (data?.items || data || []).map((s) => convertSession(s)),
            hasMore: data?.hasMore ?? false,
            nextOffset: data?.nextOffset ?? null,
        };
    },
    async createSession(data) {
        const response = await apiCreateSession({
            body: data,
        });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        if (!response.data)
            throw new Error('No data returned from create session');
        return convertSession(response.data);
    },
    async updateSession(sessionId, data) {
        const response = await apiUpdateSession({
            path: { sessionId },
            // biome-ignore lint/suspicious/noExplicitAny: API type mismatch
            body: data,
        });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        return convertSession(response.data);
    },
    async deleteSession(sessionId) {
        const response = await apiDeleteSession({ path: { sessionId } });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        return response.data;
    },
    async abortSession(sessionId) {
        const response = await apiAbortSession({ path: { sessionId } });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        return response.data;
    },
    async abortMessage(sessionId, _messageId) {
        const response = await apiAbortSession({
            path: { sessionId },
        });
        if (response.error)
            throw new Error('Failed to abort message');
        return response.data;
    },
    async getQueueState(sessionId) {
        const response = await apiGetSessionQueue({ path: { sessionId } });
        if (response.error)
            throw new Error('Failed to get queue state');
        // biome-ignore lint/suspicious/noExplicitAny: API response structure
        return response.data;
    },
    async removeFromQueue(sessionId, messageId) {
        const response = await apiRemoveFromQueue({
            path: { sessionId, messageId },
        });
        if (response.error)
            throw new Error('Failed to remove from queue');
        // biome-ignore lint/suspicious/noExplicitAny: API response structure
        return response.data;
    },
    async getMessages(sessionId) {
        const response = await apiListMessages({ path: { id: sessionId } });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        return (response.data || []).map(convertMessage);
    },
    async sendMessage(sessionId, data) {
        const response = await apiCreateMessage({
            path: { id: sessionId },
            body: data,
        });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        return response.data;
    },
    getStreamUrl(sessionId) {
        return `${getBaseUrl()}/v1/sessions/${sessionId}/stream`;
    },
    async retryMessage(sessionId, messageId) {
        const response = await apiRetryMessage({
            path: { sessionId, messageId },
        });
        if (response.error)
            throw new Error(extractErrorMessage(response.error));
        // biome-ignore lint/suspicious/noExplicitAny: API response structure
        return response.data;
    },
};
//# sourceMappingURL=sessions.js.map