/**
 * Handler for git_file_base messages.
 * Returns file content at a given git ref (default HEAD) for diff comparison.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { getProject } from "../db/repositories/projects.js";
import { getFileAtRef, GitNotInitializedError } from "../services/git.js";
import { logger } from "../utils/logger.js";
export async function handleGitFileBase(message, send) {
    const msg = message;
    const { requestId, sessionId } = msg;
    const project = getProject(sessionId);
    if (!project) {
        send({
            type: "git_file_base_response",
            requestId,
            error: {
                code: ErrorCode.INVALID_REQUEST,
                message: `Project not found: ${sessionId}`,
            },
        });
        return;
    }
    try {
        const { path, ref } = msg.payload;
        const result = await getFileAtRef(project.path, path, ref);
        send({
            type: "git_file_base_response",
            requestId,
            payload: {
                path: result.path,
                ref: result.ref,
                content: result.content,
            },
        });
    }
    catch (err) {
        if (err instanceof GitNotInitializedError) {
            send({
                type: "git_file_base_response",
                requestId,
                error: {
                    code: ErrorCode.GIT_NOT_INITIALIZED,
                    message: "Not a git repository",
                },
            });
            return;
        }
        const errorMessage = err instanceof Error ? err.message : String(err);
        logger.error(`git_file_base error: ${errorMessage}`);
        send({
            type: "git_file_base_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: errorMessage,
            },
        });
    }
}
//# sourceMappingURL=git-file-base.js.map