"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WfsCapabilitiesParser = void 0;
var tslib_1 = require("tslib");
var string_1 = require("../../utils/string");
function tryGetElementContent(el) {
    var _a;
    if (el) {
        return (_a = el.textContent) !== null && _a !== void 0 ? _a : el.text;
    }
}
function getElements(root, tagName, ns) {
    var els;
    if (ns) {
        els = root.getElementsByTagName("".concat(ns, ":").concat(tagName));
        if (els.length == 0) {
            //Try un-qualified
            els = root.getElementsByTagName(tagName);
        }
    }
    else {
        els = root.getElementsByTagName(tagName);
    }
    var elements = [];
    if (els) {
        for (var i = 0; i < els.length; i++) {
            var el = els.item(i);
            if (el) {
                elements.push(el);
            }
        }
    }
    return elements;
}
function trySetProperty(obj, setter, value) {
    if (value) {
        setter(obj, value);
    }
}
/**
 * A basic parser for WFS capabilities XML
 *
 * @export
 * @class WfsCapabilitiesParser
 * @since 0.13
 */
var WfsCapabilitiesParser = /** @class */ (function () {
    function WfsCapabilitiesParser() {
    }
    WfsCapabilitiesParser.prototype.parse = function (xml) {
        var parser = new DOMParser();
        this.xmlDoc = parser.parseFromString(xml, "text/xml");
        var info = this.getServiceInfo();
        var layers = this.getLayers();
        return {
            info: info,
            layers: layers
        };
    };
    WfsCapabilitiesParser.prototype.getServiceInfo = function () {
        var _a, _b;
        var els = getElements(this.xmlDoc, "ServiceIdentification", "ows");
        var info = {
            title: "",
            abstract: "",
            version: ""
        };
        if (els.length == 1) {
            var t = tryGetElementContent((_a = getElements(els[0], "Title", "ows")) === null || _a === void 0 ? void 0 : _a[0]);
            var a = tryGetElementContent((_b = getElements(els[0], "Abstract", "ows")) === null || _b === void 0 ? void 0 : _b[0]);
            var ops = getElements(this.xmlDoc, "Operation", "ows");
            for (var _i = 0, ops_1 = ops; _i < ops_1.length; _i++) {
                var op = ops_1[_i];
                if (op.getAttribute("name") == "GetFeature") {
                    var parms = getElements(op, "Parameter", "ows");
                    for (var _c = 0, parms_1 = parms; _c < parms_1.length; _c++) {
                        var parm = parms_1[_c];
                        if (parm.getAttribute("name") == "outputFormat") {
                            var allowedValues = getElements(parm, "Value", "ows")
                                .map(function (av) { return tryGetElementContent(av); })
                                .filter(function (s) { return !(0, string_1.strIsNullOrEmpty)(s); });
                            if (allowedValues.length > 0) {
                                info.allowedOutputFormats = allowedValues;
                            }
                        }
                    }
                }
            }
            var v = getElements(els[0], "ServiceTypeVersion", "ows")
                .map(function (el) { return tryGetElementContent(el); })
                .filter(function (s) { return !(0, string_1.strIsNullOrEmpty)(s); });
            v.sort();
            trySetProperty(info, function (i, v) { return i.title = v; }, t);
            trySetProperty(info, function (i, v) { return i.abstract = v; }, a);
            //Pick the last (highest) version
            if (v.length > 0) {
                info.version = v[v.length - 1];
            }
        }
        return info;
    };
    WfsCapabilitiesParser.prototype.getLayers = function () {
        var els = getElements(this.xmlDoc, "FeatureType", "wfs");
        var layers = els.map(function (el) {
            var _a, _b, _c, _d, _e, _f, _g, _h;
            var info = {
                name: "",
                title: "",
                abstract: "",
                defaultCrs: ""
            };
            var n = tryGetElementContent((_a = getElements(el, "Name", "wfs")) === null || _a === void 0 ? void 0 : _a[0]);
            var t = tryGetElementContent((_b = getElements(el, "Title", "wfs")) === null || _b === void 0 ? void 0 : _b[0]);
            var a = tryGetElementContent((_c = getElements(el, "Abstract", "wfs")) === null || _c === void 0 ? void 0 : _c[0]);
            var c = tryGetElementContent((_e = (_d = getElements(el, "DefaultCRS", "wfs")) === null || _d === void 0 ? void 0 : _d[0]) !== null && _e !== void 0 ? _e : (_f = getElements(el, "DefaultSRS", "wfs")) === null || _f === void 0 ? void 0 : _f[0] // WFS older versions
            );
            var oc = getElements(el, "OtherCRS", "wfs")
                .map(function (fel) { return tryGetElementContent(fel); })
                .filter(function (s) { return !(0, string_1.strIsNullOrEmpty)(s); });
            var os = getElements(el, "OtherSRS", "wfs")
                .map(function (fel) { return tryGetElementContent(fel); })
                .filter(function (s) { return !(0, string_1.strIsNullOrEmpty)(s); });
            info.otherCrs = tslib_1.__spreadArray(tslib_1.__spreadArray([], oc, true), os, true);
            var fmts = getElements(el, "Format", "wfs")
                .map(function (fel) { return tryGetElementContent(fel); })
                .filter(function (s) { return !(0, string_1.strIsNullOrEmpty)(s); });
            var ll = tryGetElementContent((_g = getElements(el, "LowerCorner", "ows")) === null || _g === void 0 ? void 0 : _g[0]);
            var ur = tryGetElementContent((_h = getElements(el, "UpperCorner", "ows")) === null || _h === void 0 ? void 0 : _h[0]);
            trySetProperty(info, function (i, v) { return i.name = v; }, n);
            trySetProperty(info, function (i, v) { return i.title = v; }, t);
            trySetProperty(info, function (i, v) { return i.abstract = v; }, a);
            trySetProperty(info, function (i, v) { return i.defaultCrs = v; }, c);
            if (fmts.length > 0) {
                info.formats = fmts;
            }
            var llParts = ll === null || ll === void 0 ? void 0 : ll.split(' ').map(function (s) { return parseFloat(s); });
            var urParts = ur === null || ur === void 0 ? void 0 : ur.split(' ').map(function (s) { return parseFloat(s); });
            if ((llParts === null || llParts === void 0 ? void 0 : llParts.length) == 2 && (urParts === null || urParts === void 0 ? void 0 : urParts.length) == 2) {
                info.wgs84Bounds = [
                    llParts[0],
                    llParts[1],
                    urParts[0],
                    urParts[1]
                ];
            }
            return info;
        });
        return layers;
    };
    return WfsCapabilitiesParser;
}());
exports.WfsCapabilitiesParser = WfsCapabilitiesParser;
//# sourceMappingURL=wfs-capabilities-parser.js.map