import { grpcClientCall } from '../../utilities/grpcHelper';
import { EVENT_NAMES } from '../../constants/define';
import { CountyDataManager } from '../../mapping';
const grpc = require('@grpc/grpc-js');
const { ipcMain } = require('electron');

const { LIST, DELETE, CREATE, UPDATE } = EVENT_NAMES.COUNTIES;
const METHODS = {
  LIST: 'List',
  DELETE: 'Delete',
  CREATE: 'Create',
  UPDATE: 'Update',
};

export default function CountyClient(mainProto, SERVER_URL, options){
    const client = new mainProto.CountyController( SERVER_URL, grpc.credentials.createInsecure() , options)
    const handleRequest = async (event, methodName, attributes) => {
        const request = CountyDataManager.convertRequestData(attributes);
        const response = await grpcClientCall(client, methodName, request, event);
        return CountyDataManager.convertResponseData(response);
      };
    

    ipcMain.handle(LIST, (event, attributes) => handleRequest(event, METHODS.LIST, attributes));
    ipcMain.handle(DELETE, (event, id) => handleRequest(event, METHODS.DELETE, { id }));
    ipcMain.handle(CREATE, (event, attributes) => handleRequest(event, METHODS.CREATE, attributes));
    ipcMain.handle(UPDATE, (event, attributes) => handleRequest(event, METHODS.UPDATE, attributes));

}


