import { z } from "zod";
import { runtimeConfigSaveRequestSchema, runtimeGitCheckoutRequestSchema, runtimeHookIngestRequestSchema, runtimeProjectAddRequestSchema, runtimeProjectRemoveRequestSchema, runtimeCommandRunRequestSchema, runtimeShellSessionStartRequestSchema, runtimeTaskSessionInputRequestSchema, runtimeTaskSessionStartRequestSchema, runtimeTaskSessionStopRequestSchema, runtimeTaskWorkspaceInfoRequestSchema, runtimeTerminalWsClientMessageSchema, runtimeWorkspaceChangesRequestSchema, runtimeWorkspaceFileSearchRequestSchema, runtimeWorkspaceStateSaveRequestSchema, runtimeWorktreeDeleteRequestSchema, runtimeWorktreeEnsureRequestSchema, } from "./api-contract.js";
const trimmedStringSchema = z.string().transform((value) => value.trim());
const positiveIntegerFromQuerySchema = z.coerce.number().int().positive();
const requiredTrimmedStringSchema = (message) => trimmedStringSchema.pipe(z.string().min(1, message));
function parseWithSchema(schema, value) {
    const parsed = schema.safeParse(value);
    if (!parsed.success) {
        throw new Error(parsed.error.issues[0]?.message ?? "Invalid request payload.");
    }
    return parsed.data;
}
export function parseWorkspaceChangesRequest(query) {
    const taskId = parseWithSchema(requiredTrimmedStringSchema("Missing taskId query parameter."), query.get("taskId") ?? "");
    const baseRef = parseWithSchema(requiredTrimmedStringSchema("Missing baseRef query parameter."), query.get("baseRef") ?? "");
    return parseWithSchema(runtimeWorkspaceChangesRequestSchema, { taskId, baseRef });
}
export function parseTaskWorkspaceInfoRequest(query) {
    const taskId = parseWithSchema(requiredTrimmedStringSchema("Missing taskId query parameter."), query.get("taskId") ?? "");
    const baseRef = parseWithSchema(requiredTrimmedStringSchema("Missing baseRef query parameter."), query.get("baseRef") ?? "");
    return parseWithSchema(runtimeTaskWorkspaceInfoRequestSchema, { taskId, baseRef });
}
export function parseOptionalTaskWorkspaceInfoRequest(query) {
    if (!query.has("taskId")) {
        if (query.has("baseRef")) {
            throw new Error("baseRef query parameter requires taskId.");
        }
        return null;
    }
    return parseTaskWorkspaceInfoRequest(query);
}
export function parseWorkspaceFileSearchRequest(query) {
    const normalizedQuery = parseWithSchema(trimmedStringSchema, query.get("q") ?? "");
    if (!normalizedQuery) {
        return { query: "" };
    }
    const rawLimit = query.get("limit");
    if (rawLimit == null || rawLimit.trim() === "") {
        return parseWithSchema(runtimeWorkspaceFileSearchRequestSchema, {
            query: normalizedQuery,
        });
    }
    const parsedLimit = positiveIntegerFromQuerySchema.safeParse(rawLimit);
    if (!parsedLimit.success) {
        throw new Error("Invalid file search limit parameter.");
    }
    return parseWithSchema(runtimeWorkspaceFileSearchRequestSchema, {
        query: normalizedQuery,
        limit: parsedLimit.data,
    });
}
export function parseGitCheckoutRequest(value) {
    const parsed = parseWithSchema(runtimeGitCheckoutRequestSchema, value);
    const branch = parsed.branch.trim();
    if (!branch) {
        throw new Error("Branch cannot be empty.");
    }
    return {
        branch,
    };
}
export function parseWorktreeEnsureRequest(value) {
    const parsed = parseWithSchema(runtimeWorktreeEnsureRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid worktree ensure payload.");
    }
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Invalid worktree ensure payload.");
    }
    return {
        taskId,
        baseRef,
    };
}
export function parseWorktreeDeleteRequest(value) {
    const parsed = parseWithSchema(runtimeWorktreeDeleteRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid worktree delete payload.");
    }
    return {
        taskId,
    };
}
export function parseWorkspaceStateSaveRequest(value) {
    return parseWithSchema(runtimeWorkspaceStateSaveRequestSchema, value);
}
export function parseProjectAddRequest(value) {
    const parsed = parseWithSchema(runtimeProjectAddRequestSchema, value);
    const path = parsed.path.trim();
    if (!path) {
        throw new Error("Project path cannot be empty.");
    }
    return {
        path,
    };
}
export function parseProjectRemoveRequest(value) {
    const parsed = parseWithSchema(runtimeProjectRemoveRequestSchema, value);
    const projectId = parsed.projectId.trim();
    if (!projectId) {
        throw new Error("Project ID cannot be empty.");
    }
    return {
        projectId,
    };
}
export function parseRuntimeConfigSaveRequest(value) {
    return parseWithSchema(runtimeConfigSaveRequestSchema, value);
}
export function parseCommandRunRequest(value) {
    const parsed = parseWithSchema(runtimeCommandRunRequestSchema, value);
    const command = parsed.command.trim();
    if (!command) {
        throw new Error("Command cannot be empty.");
    }
    return {
        command,
    };
}
export function parseTaskSessionStartRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionStartRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task session taskId cannot be empty.");
    }
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Task session baseRef cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
        baseRef,
    };
}
export function parseTaskSessionStopRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionStopRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid task session stop payload.");
    }
    return {
        taskId,
    };
}
export function parseTaskSessionInputRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionInputRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task session taskId cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
    };
}
export function parseShellSessionStartRequest(value) {
    const parsed = parseWithSchema(runtimeShellSessionStartRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Shell session taskId cannot be empty.");
    }
    if (parsed.workspaceTaskId !== undefined && !parsed.workspaceTaskId.trim()) {
        throw new Error("Invalid shell session workspaceTaskId.");
    }
    const workspaceTaskId = parsed.workspaceTaskId?.trim() || undefined;
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Shell session baseRef cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
        workspaceTaskId,
        baseRef,
    };
}
export function parseHookIngestRequest(value) {
    const parsed = parseWithSchema(runtimeHookIngestRequestSchema, value);
    const taskId = parsed.taskId.trim();
    const workspaceId = parsed.workspaceId.trim();
    if (!taskId) {
        throw new Error("Missing taskId");
    }
    if (!workspaceId) {
        throw new Error("Missing workspaceId");
    }
    const metadata = parsed.metadata
        ? {
            activityText: parsed.metadata.activityText?.trim(),
            toolName: parsed.metadata.toolName?.trim(),
            finalMessage: parsed.metadata.finalMessage?.trim(),
            hookEventName: parsed.metadata.hookEventName?.trim(),
            notificationType: parsed.metadata.notificationType?.trim(),
            source: parsed.metadata.source?.trim(),
        }
        : undefined;
    return {
        ...parsed,
        taskId,
        workspaceId,
        metadata,
    };
}
export function parseTerminalWsClientMessage(value) {
    const parsed = runtimeTerminalWsClientMessageSchema.safeParse(value);
    if (!parsed.success) {
        return null;
    }
    return parsed.data;
}
//# sourceMappingURL=api-validation.js.map