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

const { RETRIEVE } = EVENT_NAMES.ESSENTIAL;
const METHODS = {
  RETRIEVE: 'GetEssential',
};

export default function userClient(mainProto, SERVER_URL, options) {
  const client = new mainProto.EssentialController(SERVER_URL, grpc.credentials.createInsecure(), options);

  const handleRequest = async (event, methodName, attributes) => {
    const request = EssentialDataManager.convertRequestData(attributes);
    const response = await grpcClientCall(client, methodName, request, event);
    return EssentialDataManager.convertResponseData(response);
  };

  ipcMain.handle(RETRIEVE, (event, attributes) => handleRequest(event, METHODS.RETRIEVE, attributes));
}
