import { z } from 'zod/v4';
import { CoderCreateAgentBuilderSessionRequestSchema, CoderCreateSessionRequestSchema, CoderListSessionsParamsSchema, CoderSessionListResponseSchema, CoderSessionListItemSchema, CoderSessionSchema, CoderUpdateSessionRequestSchema, } from "./types.js";
const CoderHubSessionListResponseSchema = z.object({
    sessions: z.object({
        websocket: z.array(CoderSessionListItemSchema),
        sandbox: z.array(z.unknown()),
    }),
    total: z.number(),
});
export const CoderLifecycleResponseSchema = z
    .object({
    sessionId: z.string().describe('Session identifier'),
    status: z.string().optional().describe('New session status'),
})
    .passthrough();
export const CoderSessionIdParamsSchema = z
    .object({
    sessionId: z.string().describe('Coder session identifier'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
})
    .describe('Common parameters for single-session operations');
export const CoderCreateSessionParamsSchema = z
    .object({
    body: CoderCreateSessionRequestSchema.describe('Create-session request body'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
})
    .describe('Parameters for creating a coder session');
export const CoderCreateAgentBuilderSessionParamsSchema = z
    .object({
    body: CoderCreateAgentBuilderSessionRequestSchema.describe('Create-agent-builder-session request body'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
})
    .describe('Parameters for creating an agent-builder session');
export const CoderGetSessionParamsSchema = CoderSessionIdParamsSchema.describe('Parameters for retrieving a coder session');
export const CoderUpdateSessionParamsSchema = z
    .object({
    sessionId: z.string().describe('Coder session identifier to update'),
    body: CoderUpdateSessionRequestSchema.describe('Update-session request body'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
})
    .describe('Parameters for updating a coder session');
export const CoderListSessionsParamsWithOrgSchema = CoderListSessionsParamsSchema.describe('Parameters for listing coder sessions');
export const CoderListConnectableSessionsParamsSchema = z
    .object({
    search: z.string().optional().describe('Search query for connectable sessions'),
    limit: z.number().int().optional().describe('Maximum number of sessions to return'),
    offset: z.number().int().optional().describe('Number of sessions to skip for pagination'),
    orgId: z.string().optional().describe('Optional org id for CLI auth context'),
})
    .describe('Parameters for listing connectable coder sessions');
function normalizeSessionList(payload) {
    if (Array.isArray(payload)) {
        return {
            sessions: payload,
            total: payload.length,
        };
    }
    return CoderSessionListResponseSchema.parse(payload);
}
function buildListQuery(params) {
    const query = new URLSearchParams();
    if (params?.search) {
        query.set('search', params.search);
    }
    if (params && 'includeArchived' in params && params.includeArchived !== undefined) {
        query.set('includeArchived', String(params.includeArchived));
    }
    if (params?.limit !== undefined) {
        query.set('limit', String(params.limit));
    }
    if (params?.offset !== undefined) {
        query.set('offset', String(params.offset));
    }
    const queryString = query.toString();
    return queryString ? `?${queryString}` : '';
}
const CoderCreateSessionResponseSchema = z
    .object({
    sessionId: z.string().describe('Created session identifier'),
    sandboxId: z.string().nullable().optional().describe('Associated sandbox identifier'),
    status: z.string().describe('Initial session status'),
    mode: z.string().optional().describe('Session mode'),
    visibility: z.string().optional().describe('Session visibility'),
})
    .passthrough();
export async function coderCreateSession(client, params) {
    return client.post('/hub/session', params.body, CoderCreateSessionResponseSchema, CoderCreateSessionRequestSchema);
}
export async function coderCreateAgentBuilderSession(client, params) {
    return client.post('/hub/session/builder', params.body, CoderCreateSessionResponseSchema, CoderCreateAgentBuilderSessionRequestSchema);
}
export async function coderGetSession(client, params) {
    const path = `/hub/session/${encodeURIComponent(params.sessionId)}`;
    return client.get(path, CoderSessionSchema);
}
const CoderUpdateSessionResponseSchema = z
    .object({
    sessionId: z.string().describe('Updated session identifier'),
    label: z.string().optional().describe('Updated label'),
    visibility: z.string().optional().describe('Updated visibility'),
    tags: z.array(z.string()).optional().describe('Updated tags'),
    skills: z.array(z.unknown()).optional().describe('Updated skills'),
    defaultAgent: z.string().nullable().optional().describe('Updated default agent'),
})
    .passthrough();
export async function coderUpdateSession(client, params) {
    const path = `/hub/session/${encodeURIComponent(params.sessionId)}`;
    return client.patch(path, params.body, CoderUpdateSessionResponseSchema, CoderUpdateSessionRequestSchema);
}
export async function coderListSessions(client, params) {
    const path = `/hub/sessions${buildListQuery(params)}`;
    const raw = await client.get(path, CoderHubSessionListResponseSchema);
    return normalizeSessionList({
        sessions: raw.sessions.websocket,
        total: raw.total,
    });
}
export async function coderDeleteSession(client, params) {
    const path = `/hub/session/${encodeURIComponent(params.sessionId)}`;
    await client.delete(path);
}
function isConnectableSession(session) {
    if (session.mode !== 'sandbox' || session.historyOnly === true) {
        return false;
    }
    if (session.bucket === 'running') {
        return true;
    }
    if (session.bucket === 'paused') {
        return session.runtimeAvailable === true || session.wakeAvailable === true;
    }
    return false;
}
export async function coderArchiveSession(client, params) {
    const path = `/hub/session/${encodeURIComponent(params.sessionId)}/archive`;
    return client.post(path, undefined, CoderLifecycleResponseSchema);
}
export async function coderResumeSession(client, params) {
    const path = `/hub/session/${encodeURIComponent(params.sessionId)}/resume`;
    return client.post(path, undefined, CoderLifecycleResponseSchema);
}
export async function coderListConnectableSessions(client, params) {
    const raw = await coderListSessions(client, {
        search: params?.search,
        includeArchived: true,
        orgId: params?.orgId,
    });
    const connectable = raw.sessions.filter(isConnectableSession);
    const normalizedOffset = Math.max(0, params?.offset ?? 0);
    const normalizedLimit = Math.max(0, params?.limit ?? connectable.length);
    return {
        sessions: connectable.slice(normalizedOffset, normalizedOffset + normalizedLimit),
        total: connectable.length,
        ...(params?.limit !== undefined ? { limit: normalizedLimit } : {}),
        ...(params?.offset !== undefined ? { offset: normalizedOffset } : {}),
    };
}
//# sourceMappingURL=sessions.js.map