import { ipcRenderer } from 'electron';

/**
 * Creates an output listener for IPC channels that stream output
 * @param channel The IPC channel to listen on
 * @returns A function that accepts a callback and returns a cleanup function
 */
export function createOutputListener(channel: string) {
  return (callback: (output: string) => void): (() => void) => {
    const listener = (_event: any, output: string) => callback(output);
    ipcRenderer.on(channel, listener);
    return () => {
      ipcRenderer.removeListener(channel, listener);
    };
  };
}

/**
 * Creates an action listener for IPC channels that pass action entries
 * @param channel The IPC channel to listen on
 * @returns A function that accepts a callback and returns a cleanup function
 */
export function createActionListener<T = any>(channel: string) {
  return (callback: (action: T) => void): (() => void) => {
    const listener = (_event: any, action: T) => callback(action);
    ipcRenderer.on(channel, listener);
    return () => {
      ipcRenderer.removeListener(channel, listener);
    };
  };
}
