import { ipcMain } from 'electron'
import { listAddonRegistry, listInstalledAddons } from '../ddev'
import { runStreamed } from '../commandRunner'

export function registerAddonsIpc(): void {
  ipcMain.handle('addons:listRegistry', () => listAddonRegistry())
  ipcMain.handle('addons:listInstalled', (_event, name: string) => listInstalledAddons(name))

  ipcMain.handle('addons:install', (event, operationId: string, name: string, addonRepo: string) =>
    runStreamed(operationId, ['addon', 'get', addonRepo, '--project', name], event.sender)
  )

  ipcMain.handle('addons:remove', (event, operationId: string, name: string, addonName: string) =>
    runStreamed(operationId, ['addon', 'remove', addonName, '--project', name], event.sender)
  )
}
