import { contextBridge, ipcRenderer } from 'electron';

function createListener<T>(channel: string) {
  return (callback: (value: T) => void): (() => void) => {
    const listener = (_event: any, value: T) => callback(value);
    ipcRenderer.on(channel, listener);
    return () => {
      ipcRenderer.removeListener(channel, listener);
    };
  };
}

contextBridge.exposeInMainWorld('igniteAiAPI', {
  getTheme: async (): Promise<string> => {
    return ipcRenderer.invoke('settings:get-theme');
  },
  onThemeChange: createListener<string>('theme-changed'),
});
