/**
 * File Tools — Sprint 3.5
 *
 * file.read, file.write, file.patch, file.list, file.stat
 */
import { getFs } from "./core/get-fs.js";
import { basename } from "node:path";
const IMAGE_FILE_PATTERN = /\.(?:png|jpe?g|gif|webp)$/i;
export function createFileReadTool(registry) {
    return {
        name: "file.read",
        description: "Read the contents of a file within the project",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Relative or absolute path to the file" },
            },
            required: ["path"],
        },
        async execute(input, context) {
            try {
                const fs = await getFs(registry, context, input.path);
                const content = await fs.read(input.path);
                return {
                    ok: true,
                    message: `Read ${input.path}`,
                    data: { path: input.path, content, size: content.length },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "Read failed",
                };
            }
        },
    };
}
export function createFileWriteTool(registry) {
    return {
        name: "file.write",
        description: "Write content to a file within the project (creates parent directories)",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Relative or absolute path to the file" },
                content: { type: "string", description: "File content to write" },
            },
            required: ["path", "content"],
        },
        async execute(input, context) {
            try {
                const fs = await getFs(registry, context, input.path);
                await fs.write(input.path, input.content);
                return {
                    ok: true,
                    message: `Wrote ${input.path} (${input.content.length} bytes)`,
                    data: { path: input.path, size: input.content.length },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "Write failed",
                };
            }
        },
    };
}
export function createFilePatchTool(registry) {
    return {
        name: "file.patch",
        description: "Search-and-replace within a file (first occurrence)",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Path to the file" },
                search: { type: "string", description: "Exact text to find" },
                replace: { type: "string", description: "Replacement text" },
            },
            required: ["path", "search", "replace"],
        },
        async execute(input, context) {
            try {
                const fs = await getFs(registry, context, input.path);
                const result = await fs.patch(input.path, input.search, input.replace);
                if (!result.matched) {
                    return { ok: false, message: `Search string not found in ${input.path}` };
                }
                return {
                    ok: true,
                    message: `Patched ${input.path}`,
                    data: { path: input.path, matched: true },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "Patch failed",
                };
            }
        },
    };
}
export function createFileListTool(registry) {
    return {
        name: "file.list",
        description: "List files and directories at a path within the project",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Directory path to list" },
            },
            required: ["path"],
        },
        async execute(input, context) {
            try {
                const fs = await getFs(registry, context, input.path);
                const entries = await fs.list(input.path);
                return {
                    ok: true,
                    message: `Listed ${entries.length} entries in ${input.path}`,
                    data: { path: input.path, entries },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "List failed",
                };
            }
        },
    };
}
export function createFileStatTool(registry) {
    return {
        name: "file.stat",
        description: "Get file metadata (size, type, modified date)",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Path to the file" },
            },
            required: ["path"],
        },
        async execute(input, context) {
            try {
                const fs = await getFs(registry, context, input.path);
                const info = await fs.statFile(input.path);
                return {
                    ok: true,
                    message: `Stat ${input.path}`,
                    data: { path: input.path, ...info },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "Stat failed",
                };
            }
        },
    };
}
export function createImageViewTool(registry) {
    return {
        name: "image.view",
        description: "Display an image file from the project in chat",
        tier: "standard",
        category: "filesystem",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                path: { type: "string", description: "Path to the image file" },
            },
            required: ["path"],
        },
        async execute(input, context) {
            try {
                const trimmedPath = input.path.trim();
                if (!IMAGE_FILE_PATTERN.test(trimmedPath)) {
                    return { ok: false, message: "Only PNG, JPG, GIF, or WEBP images are supported" };
                }
                const fs = await getFs(registry, context, input.path);
                const info = await fs.statFile(input.path);
                if (info.isDirectory) {
                    return { ok: false, message: `${input.path} is not a file` };
                }
                const bytes = await fs.readBinary(input.path);
                const extension = trimmedPath.match(/\.(png|jpe?g|gif|webp)$/i)?.[1]?.toLowerCase() ?? "png";
                const mimeType = extension === "png" ? "image/png" : extension === "webp" ? "image/webp" : extension === "gif" ? "image/gif" : "image/jpeg";
                const base64 = Buffer.from(bytes).toString("base64");
                const dataUri = `data:${mimeType};base64,${base64}`;
                return {
                    ok: true,
                    message: `Displaying image ${basename(trimmedPath)}`,
                    data: { path: input.path, base64, dataUri, mimeType, size: bytes.length, modified: info.modified, isDirectory: info.isDirectory },
                };
            }
            catch (err) {
                return {
                    ok: false,
                    message: err instanceof Error ? err.message : "View image failed",
                };
            }
        },
    };
}
//# sourceMappingURL=file-tools.js.map