/**
 * Handler for fs_read messages.
 * Reads file content within a project with size and format validation.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { getProject } from "../db/repositories/projects.js";
import { readFile, PathEscapeError, FileTooLargeError, UnsupportedFormatError, FileNotFoundError, } from "../services/filesystem.js";
import { logger } from "../utils/logger.js";
export async function handleFsRead(message, send) {
    const msg = message;
    const { requestId, sessionId, payload } = msg;
    const { path: relativePath } = payload;
    // Resolve project from sessionId (which is the projectId)
    const project = getProject(sessionId);
    if (!project) {
        send({
            type: "fs_read_response",
            requestId,
            error: {
                code: ErrorCode.INVALID_REQUEST,
                message: `Project not found: ${sessionId}`,
            },
        });
        return;
    }
    try {
        const result = await readFile(project.path, relativePath);
        send({
            type: "fs_read_response",
            requestId,
            payload: {
                path: relativePath,
                content: result.content,
                encoding: result.encoding,
                size: result.size,
                mtime: result.mtime,
            },
        });
    }
    catch (err) {
        if (err instanceof PathEscapeError) {
            send({
                type: "fs_read_response",
                requestId,
                error: {
                    code: ErrorCode.OUTSIDE_PROJECT_ROOT,
                    message: "Path escapes project directory",
                },
            });
            return;
        }
        if (err instanceof FileTooLargeError) {
            send({
                type: "fs_read_response",
                requestId,
                error: {
                    code: ErrorCode.FILE_TOO_LARGE,
                    message: "File exceeds 10MB limit",
                    details: { size: err.size, limit: err.limit },
                },
            });
            return;
        }
        if (err instanceof UnsupportedFormatError) {
            send({
                type: "fs_read_response",
                requestId,
                error: {
                    code: ErrorCode.UNSUPPORTED_FORMAT,
                    message: "Binary file format not supported",
                },
            });
            return;
        }
        if (err instanceof FileNotFoundError) {
            send({
                type: "fs_read_response",
                requestId,
                error: {
                    code: ErrorCode.FILE_NOT_FOUND,
                    message: `File not found: ${relativePath}`,
                },
            });
            return;
        }
        const errorMessage = err instanceof Error ? err.message : String(err);
        logger.error(`fs_read error: ${errorMessage}`);
        send({
            type: "fs_read_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: errorMessage,
            },
        });
    }
}
//# sourceMappingURL=fs-read.js.map