import { parseString as parseAsync } from 'xml2js';
/**
 * Convert XML to Javascript Object
 */
var XMLParser = /** @class */ (function () {
    function XMLParser() {
    }
    /**
     * Parse XML input synchonously
     * @param xml
     */
    XMLParser.prototype.parseSync = function (xml) {
        var res = null;
        parseAsync(xml, function (err, result) {
            if (err !== null) {
                throw err;
            }
            res = result;
        });
        return res;
    };
    /**
     * Parse XML input async
     * @param xml
     */
    XMLParser.prototype.parse = function (xml) {
        xml = this.cleanifyXml(xml);
        return new Promise(function (resolve, reject) {
            parseAsync(xml, function (err, result) {
                if (err !== null) {
                    reject(err);
                }
                resolve(result);
            });
        });
    };
    /**
     * Cleanify the xml input before parsing
     * @see http://stackoverflow.com/questions/34783452/cannot-parse-xml-error-non-whitespace-before-first-tag-line-0-column-1-cha
     */
    XMLParser.prototype.cleanifyXml = function (xml) {
        if (!xml) {
            return xml;
        }
        return xml.replace('\ufeff', '').trim();
    };
    return XMLParser;
}());
export { XMLParser };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieG1sLXBhcnNlci5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2NvbmZpZy9pb3RpemUtc3R1ZGlvL3BhcnNlci94bWwvIiwic291cmNlcyI6WyJ4bWwtcGFyc2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLE9BQU8sRUFBRSxXQUFXLElBQUksVUFBVSxFQUFFLE1BQU0sUUFBUSxDQUFDO0FBRW5EOztHQUVHO0FBQ0g7SUFBQTtJQTBDQSxDQUFDO0lBekNHOzs7T0FHRztJQUNILDZCQUFTLEdBQVQsVUFBVSxHQUFXO1FBQ2pCLElBQUksR0FBRyxHQUFRLElBQUksQ0FBQztRQUNwQixVQUFVLENBQUMsR0FBRyxFQUFFLFVBQVMsR0FBUSxFQUFFLE1BQVc7WUFDMUMsSUFBSSxHQUFHLEtBQUssSUFBSSxFQUFFO2dCQUNkLE1BQU0sR0FBRyxD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