/**
 * Handler for terminal_create messages.
 *
 * Spawns a new PTY process in the project directory and returns the terminalId.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { getProject } from "../db/repositories/projects.js";
import { createTerminal } from "../services/terminal-manager.js";
import { logger } from "../utils/logger.js";
export async function handleTerminalCreate(message, send) {
    const msg = message;
    const { requestId, sessionId, payload } = msg;
    const { cols, rows } = payload;
    // Resolve project from sessionId
    const project = getProject(sessionId);
    if (!project) {
        send({
            type: "terminal_create_response",
            requestId,
            error: {
                code: ErrorCode.INVALID_REQUEST,
                message: `Project not found: ${sessionId}`,
            },
        });
        return;
    }
    try {
        const terminalId = createTerminal(project.project_id, project.path, cols, rows);
        const response = {
            type: "terminal_create_response",
            requestId,
            payload: { terminalId },
        };
        send(response);
        logger.info(`Terminal created: ${terminalId} for project ${project.name}`);
    }
    catch (err) {
        const errMsg = err instanceof Error ? err.message : String(err);
        logger.error(`Failed to create terminal: ${errMsg}`);
        send({
            type: "terminal_create_response",
            requestId,
            error: {
                code: ErrorCode.TERMINAL_SPAWN_FAILED,
                message: `Failed to spawn terminal: ${errMsg}`,
            },
        });
    }
}
//# sourceMappingURL=terminal-create.js.map