/**
 * Handler for git_branch_create messages.
 * Creates a new git branch from an optional start point (default HEAD).
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { getProject } from "../db/repositories/projects.js";
import { createGitBranch, GitNotInitializedError } from "../services/git.js";
import { logger } from "../utils/logger.js";
export async function handleGitBranchCreate(message, send) {
    const msg = message;
    const { requestId, sessionId } = msg;
    // Resolve project from sessionId (which is the projectId)
    const project = getProject(sessionId);
    if (!project) {
        send({
            type: "git_branch_create_response",
            requestId,
            error: {
                code: ErrorCode.INVALID_REQUEST,
                message: `Project not found: ${sessionId}`,
            },
        });
        return;
    }
    try {
        const result = await createGitBranch(project.path, msg.payload.name, msg.payload.startPoint);
        send({
            type: "git_branch_create_response",
            requestId,
            payload: {
                name: result.name,
                created: result.created,
            },
        });
    }
    catch (err) {
        if (err instanceof GitNotInitializedError) {
            send({
                type: "git_branch_create_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_branch_create error: ${errorMessage}`);
        send({
            type: "git_branch_create_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: errorMessage,
            },
        });
    }
}
//# sourceMappingURL=git-branch-create.js.map