import { Client } from "@hibas123/openauth-internalapi";
import request, { RequestError } from "./request";

const provider = new Client.ServiceProvider((data) => {
   request("/api/jrpc", {}, "POST", data, true, true).then(result => {
      provider.onPacket(result);
   }).catch(err => {
      if (err instanceof RequestError) {
         let data = err.response;
         if (data.error && Array.isArray(data.error)) {
            data.error = data.error[0];
         }
         provider.onPacket(data);
      }
   });
});

const InternalAPI = {
   Account: new Client.AccountService(provider),
   Security: new Client.SecurityService(provider),
}

export default InternalAPI;
