const Rpc = {
    create : function( params = {} )
    {
        this.url = params.get( 'url' );
    },

    call : function( controller, action, params, onSuccess = null, onError = null )
    {
        let cbSuccess = onSuccess,
            cbError = onError,
            xhr = this.app.createXhr({
                headers :  [
                    {
                        key : "Content-Type",
                        value : "application/json"
                    }
                ],
                responseType : 'json'
            }),
            requestData = {
                auth : {
                    version : 1000,
                    sessionToken : this.app.user.sessionId,
                },
                c : controller,
                a: action,
                params : params || null
        };

        return xhr.post(
            this.url,
            JSON.stringify( requestData ),
            function( err, response )
            {
                if ( err && cbError )
                {
                    cbError( err );
                }
                else
                {
                    cbSuccess && cbSuccess( response, err );
                }
            }
        );
    }
};

export { Rpc };