import { ipcMain } from 'electron';
import { accessPath, listDirectory, mkdirPath, readFile, readFileWithEncoding, removePath, statPath, writeFile, writeFileWithEncoding } from './fileSystem';
import { fetchURL } from './fetch';
import { runBashScript } from './childProcess';
export const registerIpcHandlers = (getLocalFileServer) => {
    ipcMain.handle('fs:list-directory', async (_event, options) => {
        return listDirectory(options);
    });
    ipcMain.handle('fs:read-file', async (_event, filePath) => {
        return readFile(filePath);
    });
    ipcMain.handle('fs:write-file', async (_event, filePath, contents) => {
        await writeFile(filePath, contents);
    });
    ipcMain.handle('fs:promises:read-file', async (_event, payload) => {
        const { filePath, options } = payload;
        return readFileWithEncoding(filePath, options);
    });
    ipcMain.handle('fs:promises:write-file', async (_event, payload) => {
        const { filePath, data, options } = payload;
        await writeFileWithEncoding(filePath, data, options);
    });
    ipcMain.handle('fs:promises:stat', async (_event, payload) => {
        if (payload.options?.bigint) {
            throw new Error('fs.promises.stat with bigint option is not supported in this environment');
        }
        return statPath(payload.filePath);
    });
    ipcMain.handle('fs:promises:access', async (_event, payload) => {
        await accessPath(payload.filePath, payload.mode);
    });
    ipcMain.handle('fs:promises:mkdir', async (_event, payload) => {
        await mkdirPath(payload.targetPath, payload.options);
    });
    ipcMain.handle('fs:promises:rm', async (_event, payload) => {
        await removePath(payload.targetPath, payload.options);
    });
    ipcMain.handle('net:fetch-url', async (_event, sourceUrl) => {
        try {
            const result = await fetchURL(sourceUrl);
            const { body, ...metadata } = result;
            const payload = {
                ...metadata,
                body: body.toString('utf-8'),
                bodyEncoding: 'utf-8'
            };
            return payload;
        }
        catch (error) {
            return { error: error.message };
        }
    });
    ipcMain.handle('local-file-server:info', async () => {
        return getLocalFileServer();
    });
    ipcMain.handle('child-process:run-bash-script', async (_event, payload) => {
        const normalizedPayload = typeof payload === 'string'
            ? { code: payload }
            : payload;
        if (!normalizedPayload || typeof normalizedPayload.code !== 'string' || normalizedPayload.code.trim().length === 0) {
            return { error: 'code must be a non-empty string' };
        }
        const args = Array.isArray(normalizedPayload.args) ? normalizedPayload.args : [];
        const options = normalizedPayload.options ?? {};
        try {
            const result = await runBashScript(normalizedPayload.code, args, options);
            return result;
        }
        catch (error) {
            return { error: error.message };
        }
    });
    ipcMain.on('process:get-cwd', event => {
        event.returnValue = process.cwd();
    });
    ipcMain.on('process:get-env', event => {
        event.returnValue = { ...process.env };
    });
    ipcMain.on('process:get-env-var', (event, key) => {
        event.returnValue = process.env[key] ?? undefined;
    });
};
