export const installPreload = (bridge, ipcRenderer) => {
    bridge.exposeInMainWorld('filesystem', {
        listDirectory: (options = {}) => ipcRenderer.invoke('fs:list-directory', options),
        readFile: (filePath) => ipcRenderer.invoke('fs:read-file', filePath),
        writeFile: (filePath, contents) => ipcRenderer.invoke('fs:write-file', filePath, contents)
    });
    bridge.exposeInMainWorld('network', {
        fetchURL: async (sourceUrl) => {
            return (await ipcRenderer.invoke('net:fetch-url', sourceUrl));
        }
    });
    bridge.exposeInMainWorld('localFileServer', {
        getInfo: async () => {
            return (await ipcRenderer.invoke('local-file-server:info'));
        }
    });
    bridge.exposeInMainWorld('desktopFsPromises', {
        readFile: (filePath, options) => ipcRenderer.invoke('fs:promises:read-file', { filePath, options }),
        writeFile: (filePath, data, options) => ipcRenderer.invoke('fs:promises:write-file', { filePath, data, options }),
        stat: (filePath, options) => ipcRenderer.invoke('fs:promises:stat', { filePath, options }),
        access: (filePath, mode) => ipcRenderer.invoke('fs:promises:access', { filePath, mode }),
        mkdir: (targetPath, options) => ipcRenderer.invoke('fs:promises:mkdir', { targetPath, options }),
        rm: (targetPath, options) => ipcRenderer.invoke('fs:promises:rm', { targetPath, options })
    });
    bridge.exposeInMainWorld('splashBridge', {
        onStatusUpdate: (callback) => {
            const listener = (_event, status) => {
                if (typeof status !== 'string') {
                    return;
                }
                callback(status);
            };
            ipcRenderer.on('splash:update-status', listener);
            return () => {
                ipcRenderer.removeListener('splash:update-status', listener);
            };
        }
    });
    bridge.exposeInMainWorld('child_process', {
        runBashScript: async (code, args = [], options) => {
            const response = (await ipcRenderer.invoke('child-process:run-bash-script', {
                code,
                args,
                options
            }));
            if (response.error) {
                throw new Error(response.error);
            }
            return response;
        }
    });
    const rendererCwd = ipcRenderer.sendSync('process:get-cwd');
    bridge.exposeInMainWorld('desktopProcess', {
        cwd: () => rendererCwd,
        getEnvSnapshot: () => ipcRenderer.sendSync('process:get-env'),
        getEnvVar: (key) => ipcRenderer.sendSync('process:get-env-var', key)
    });
};
