
import { XmlRpcHelper } from "./xmlrpc-helper";
import { JsToXml } from "./js-to-xml";    
import { Xml2Js } from "./xml-to-js";

export class XmlRpc {

    configuration: {
        host: string,
        
    }

    constructor(
        private helper: XmlRpcHelper,
        private jsToXml: JsToXml,
        private xmlToJs: Xml2Js
    ) {
    }

    /**
     *
     */
    serialize(xml: any): any {
        let text = xml.xml;

        if (text) {
            return text;
        }

        if (typeof XMLSerializer != undefined) {
            return new XMLSerializer().serializeToString(xml);
        }

        throw new Error('Your browser does not support serializing XML documents');
    }

    /**
     *
     */
    createCall(method: any, params: any): any {
        var doc = this.helper.createDocument('methodCall');
        doc.firstChild.appendChild(this.helper.createNode(doc, 'methodName', method));

        if (arguments.length > 2) {
            params = this.helper.cloneArray(arguments);
            params.shift();
        }

        if (params && params.length > 0) {
            let paramsNode = this.helper.createNode(doc, params);

            for (let i = 0; i < params.length; i++) {
                paramsNode.appendChild(this.helper.createNode(doc, 'param', this.jsToXml.jsToXml(doc, params[i])));
            }

            doc.firstChild.appendChild(paramsNode);
        }

        return (this.serialize(doc)).replace(/[\s\xa0]+$/, '');
    }

    /**
     *
     */
    callMethod(method: any, params: any): any {
        let xmlStr = this.createCall(method, params);
        let targetUrl = 
    }
}