import { ipcMain } from 'electron';
import fs from 'fs';
import AudioChannels from './channels';
import { LoadAudioBufferArgs, LoadAudioBufferResult } from './types';

export default function registerAudioIpcHandlers() {
  ipcMain.handle(
    AudioChannels.LOAD_AUDIO_BUFFER,
    async (_, args: LoadAudioBufferArgs): Promise<LoadAudioBufferResult> => {
      try {
        const buffer = await fs.promises.readFile(args.filePath);
        return { buffer };
      } catch (err: any) {
        return { error: err.message };
      }
    },
  );
}
