import { IotizeConfigFactory, IotizeConfigModel } from '@iotize/tap/config/iotize-studio';
import { parseString } 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;
        parseString(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) {
            parseString(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;
}());

/**
 * Create IotizeConfigModel from a XML string
 * @param xml
 */
IotizeConfigFactory.fromXML = function (xml) {
    return new IotizeConfigModel(new XMLParser().parseSync(xml));
};

/**
 * Generated bundle index. Do not edit.
 */

export { XMLParser };
//# sourceMappingURL=iotize-tap-config-iotize-studio-parser-xml.js.map
