"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WfsCapabilitiesPanel = exports.parseEpsgCodeFromCRS = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var i18n_1 = require("../../api/i18n");
var core_1 = require("@blueprintjs/core");
/**
 * Attempts to extract an EPSG code from the given CRS identifier
 *
 * @export
 * @param {string | undefined} crs
 * @returns {(number | undefined)}
 * @since 0.13
 */
function parseEpsgCodeFromCRS(crs) {
    if (crs == "urn:ogc:def:crs:OGC:1.3:CRS84") {
        return 4326;
    }
    var res = crs === null || crs === void 0 ? void 0 : crs.match(/urn:ogc:def:crs:EPSG::(\d+)/);
    if ((res === null || res === void 0 ? void 0 : res.length) == 2) {
        return parseInt(res[1], 10);
    }
    res = crs === null || crs === void 0 ? void 0 : crs.match(/EPSG:(\d+)/);
    if ((res === null || res === void 0 ? void 0 : res.length) == 2) {
        return parseInt(res[1], 10);
    }
    return undefined;
}
exports.parseEpsgCodeFromCRS = parseEpsgCodeFromCRS;
function isGeoJsonMimeType(mimeType) {
    var _a;
    var lmt = (_a = mimeType === null || mimeType === void 0 ? void 0 : mimeType.toLowerCase()) !== null && _a !== void 0 ? _a : "";
    if (lmt.indexOf("application/vnd.geo+json") >= 0 ||
        lmt.indexOf("application/json") >= 0 ||
        lmt.indexOf("geojson") >= 0) {
        return true;
    }
    return false;
}
function getGeoJsonFormat(serviceInfo, layer) {
    var _a, _b, _c, _d;
    var lformats = (_b = ((_a = layer.formats) !== null && _a !== void 0 ? _a : []).filter(function (f) { return isGeoJsonMimeType(f); })) === null || _b === void 0 ? void 0 : _b[0];
    // NOTE: Some WFS services advertise GeoJSON support as an allowed outputFormat parameter at the service-level, 
    // but not as an advertised outputFormat for the layer in question. Without fully reading the spec, I don't know
    // if using GetFeature for a layer with a service-level outputFormat for such layers is formally allowed. Based
    // on my sample WFS services, this appears to be allowed, so if geojson is supported at the service-level, but
    // not at the layer level, we'll still consider geojson as being "supported"
    var sformats = (_d = ((_c = serviceInfo.allowedOutputFormats) !== null && _c !== void 0 ? _c : []).filter(function (f) { return isGeoJsonMimeType(f); })) === null || _d === void 0 ? void 0 : _d[0];
    return lformats !== null && lformats !== void 0 ? lformats : sformats;
}
function getLayerCrs(layer) {
    var pdc = [parseEpsgCodeFromCRS(layer.defaultCrs), layer.defaultCrs];
    // We strongly prefer 4326/3857 projections over others
    if (!(pdc[0] == 4326 || pdc[0] == 3857)) {
        if (layer.otherCrs) {
            var matches = layer.otherCrs.filter(function (c) {
                var poc = parseEpsgCodeFromCRS(c);
                return (poc == 4326 || poc == 3857);
            });
            if (matches.length > 0) {
                pdc = [parseEpsgCodeFromCRS(matches[0]), matches[0]];
            }
        }
    }
    return pdc;
}
/**
 * @hidden
 */
var WfsCapabilitiesPanel = function (props) {
    var locale = props.locale, capabilities = props.capabilities, onAddLayer = props.onAddLayer;
    var layers = capabilities.layers, info = capabilities.info;
    return React.createElement(React.Fragment, null,
        React.createElement(core_1.Card, null,
            React.createElement("h5", { className: "bp3-heading" },
                React.createElement("a", { href: "#" }, (0, i18n_1.tr)("WFS_SERVICE_INFO", locale))),
            React.createElement("p", null, (0, i18n_1.tr)("WFS_VERSION", locale, { version: info.version })),
            React.createElement("p", null, (0, i18n_1.tr)("OWS_SERVICE_TITLE", locale, { title: info.title })),
            React.createElement("p", null, (0, i18n_1.tr)("OWS_SERVICE_ABSTRACT", locale, { abstract: info.abstract }))),
        React.createElement(core_1.Card, { style: { marginBottom: 10 } },
            React.createElement("h5", { className: "bp3-heading" },
                React.createElement("a", { href: "#" }, (0, i18n_1.tr)("WFS_AVAILABLE_LAYERS", locale))),
            layers.map(function (layer) {
                var geoJsonFmt = getGeoJsonFormat(info, layer);
                var _a = getLayerCrs(layer), epsgCode = _a[0], origCrs = _a[1];
                //Only include layers whose CRS was able to be parsed into an epsg code
                if (epsgCode && geoJsonFmt) {
                    var otherActions = React.createElement(React.Fragment, null);
                    return React.createElement(core_1.Card, { key: layer.name, style: { padding: 15, paddingTop: 5 } },
                        React.createElement("h4", null,
                            React.createElement(core_1.Icon, { icon: "layer" }),
                            " ",
                            layer.name),
                        React.createElement("p", null, (0, i18n_1.tr)("OWS_LAYER_TITLE", locale, { title: layer.title })),
                        React.createElement("p", null, (0, i18n_1.tr)("OWS_LAYER_CRS", locale, { crs: "EPSG:".concat(epsgCode) })),
                        React.createElement(core_1.ButtonGroup, { fill: true },
                            React.createElement(core_1.Button, { onClick: function () { return onAddLayer(layer.name, info.version, geoJsonFmt, origCrs, epsgCode, layer.wgs84Bounds); }, intent: core_1.Intent.PRIMARY, icon: "new-layer" }, (0, i18n_1.tr)("ADD_LAYER", locale)),
                            otherActions));
                }
            }).filter(function (c) { return c; })));
};
exports.WfsCapabilitiesPanel = WfsCapabilitiesPanel;
//# sourceMappingURL=wfs-capabilities-panel.js.map