import { contextBridge, ipcRenderer } from "electron";

const electronAPI = {
    process: {
        versions: process.versions
    },
    ipcRenderer: {
        invoke: (channel: string, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
        on: (channel: string, listener: (...args: any[]) => void) => ipcRenderer.on(channel, listener),
        off: (channel: string, listener: (...args: any[]) => void) => ipcRenderer.off(channel, listener),
        send: (channel: string, ...args: any[]) => ipcRenderer.send(channel, ...args),
        removeAllListeners: (channel: string) => ipcRenderer.removeAllListeners(channel),
    }
};

const api = {
    installOllama: () => ipcRenderer.invoke("install-ollama-and-model"),

    // 监听进度 (Send/On)
    onInstallProgress: (callback: any) => {
        ipcRenderer.on("install-progress", (_event, value) => callback(value));
    },

    // 移除监听器
    removeInstallProgressListeners: () => {
        ipcRenderer.removeAllListeners("install-progress");
    },
};

if (process.contextIsolated) {
    try {
        contextBridge.exposeInMainWorld("electron", electronAPI);
        contextBridge.exposeInMainWorld("api", api);
    } catch (error) {
        console.error(error);
    }
} else {
    // @ts-ignore (define in dts)
    window.electron = electronAPI;
    // @ts-ignore (define in dts)
    window.api = api;
}
