"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddWmsLayer = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var common_1 = require("../../api/common");
var i18n_1 = require("../../api/i18n");
var error_1 = require("../error");
var client_1 = require("../../api/client");
var wms_capabilities_panel_1 = require("./wms-capabilities-panel");
var WMSCapabilities_1 = tslib_1.__importDefault(require("ol/format/WMSCapabilities"));
var Tile_1 = tslib_1.__importDefault(require("ol/layer/Tile"));
var Image_1 = tslib_1.__importDefault(require("ol/layer/Image"));
var ImageWMS_1 = tslib_1.__importDefault(require("ol/source/ImageWMS"));
var TileWMS_1 = tslib_1.__importDefault(require("ol/source/TileWMS"));
var core_1 = require("@blueprintjs/core");
var string_1 = require("../../utils/string");
var layer_manager_1 = require("../../api/layer-manager");
var runtime_1 = require("../../api/runtime");
/**
 * @hidden
 */
var AddWmsLayer = function (props) {
    var locale = props.locale;
    var _a = React.useState(""), wmsUrl = _a[0], setWmsUrl = _a[1];
    var _b = React.useState(false), loadingCapabilities = _b[0], setLoadingCapabilities = _b[1];
    var _c = React.useState(undefined), caps = _c[0], setCaps = _c[1];
    var _d = React.useState(undefined), error = _d[0], setError = _d[1];
    var onAddLayer = function (name, selectable, isTiled, style) {
        var _a, _b;
        var viewer = (0, runtime_1.getViewer)();
        if (caps && viewer) {
            var params = {
                LAYERS: name
            };
            if (style) {
                params.STYLE = style.Name;
            }
            if (isTiled) {
                params.TILED = true;
            }
            var layer = void 0;
            var source = void 0;
            if (isTiled) {
                source = new TileWMS_1.default({
                    url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                    params: params
                });
                layer = new Tile_1.default({
                    source: source
                });
            }
            else {
                source = new ImageWMS_1.default({
                    url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                    params: params
                });
                layer = new Image_1.default({
                    source: source
                });
            }
            layer.set(common_1.LayerProperty.LAYER_TYPE, "WMS");
            layer.set(common_1.LayerProperty.IS_SELECTABLE, selectable);
            layer.set(common_1.LayerProperty.IS_EXTERNAL, true);
            layer.set(common_1.LayerProperty.IS_GROUP, false);
            if (style) {
                var legendUrl = (_b = (_a = style.LegendURL) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.OnlineResource;
                if (!(0, string_1.strIsNullOrEmpty)(legendUrl)) {
                    layer.set(common_1.LayerProperty.HAS_WMS_LEGEND, true);
                }
            }
            // Suppress automatic load event handling for this as we need our own
            source.set(common_1.SourceProperty.SUPPRESS_LOAD_EVENTS, true);
            var started = function () {
                viewer.addImageLoading();
                props.onAddLayerBusyWorker(name);
            };
            var finished = function () {
                viewer.addImageLoaded();
                props.onRemoveLayerBusyWorker(name);
            };
            if (source instanceof TileWMS_1.default) {
                source.on("tileloadstart", started);
                source.on("tileloadend", finished);
                source.on("tileloaderror", finished);
            }
            else if (source instanceof ImageWMS_1.default) {
                source.on("imageloadstart", started);
                source.on("imageloadend", finished);
                source.on("imageloaderror", finished);
            }
            viewer.getLayerManager().addLayer(name, layer);
            viewer.toastSuccess("success", (0, i18n_1.tr)("ADDED_LAYER", locale, { name: name }));
            props.onLayerAdded((0, layer_manager_1.getLayerInfo)(layer, true));
        }
    };
    var onLoadCaps = function () {
        setCaps(undefined);
        setLoadingCapabilities(true);
        var client = new client_1.Client("", "mapagent");
        client.getText(wmsUrl).then(function (s) {
            var parser = new WMSCapabilities_1.default();
            var caps = parser.read(s);
            if (caps.version != "1.3.0") {
                setLoadingCapabilities(false);
                setCaps(undefined);
                setError((0, i18n_1.tr)("WMS_UNSUPPORTED_VERSION", locale, { version: caps.version }));
            }
            else {
                setLoadingCapabilities(false);
                setCaps(caps);
                setError(undefined);
            }
        }).catch(function (err) {
            setLoadingCapabilities(false);
            setCaps(undefined);
            setError(err);
        });
    };
    var onWmsUrlChange = function (e) {
        setWmsUrl(e.target.value);
    };
    return React.createElement("div", null,
        React.createElement(core_1.ControlGroup, { fill: true },
            React.createElement(core_1.InputGroup, { leftIcon: "geosearch", placeholder: (0, i18n_1.tr)("ADD_WMS_LAYER_URL", locale), value: wmsUrl, onChange: onWmsUrlChange, readOnly: loadingCapabilities, rightElement: React.createElement(core_1.Button, { intent: core_1.Intent.PRIMARY, icon: "arrow-right", onClick: onLoadCaps, disabled: loadingCapabilities }) })),
        React.createElement("br", null),
        React.createElement("div", null, (function () {
            if (loadingCapabilities) {
                return React.createElement(core_1.NonIdealState, { icon: React.createElement(core_1.Spinner, { intent: core_1.Intent.NONE, size: core_1.Spinner.SIZE_LARGE }), title: (0, i18n_1.tr)("ADD_WMS_LAYER_LOADING", locale), description: (0, i18n_1.tr)("ADD_WMS_LAYER_LOADING_DESC", locale) });
            }
            else {
                if (caps) {
                    return React.createElement(wms_capabilities_panel_1.WmsCapabilitiesPanel, { onAddLayer: onAddLayer, capabilities: caps, locale: locale });
                }
                else if (error) {
                    return React.createElement(error_1.Error, { error: error });
                }
                else {
                    return React.createElement(core_1.NonIdealState, { icon: "issue", title: (0, i18n_1.tr)("ADD_WMS_LAYER_NO_LAYERS", locale), description: (0, i18n_1.tr)("WMS_NO_LAYER_DESCRIPTION", locale) });
                }
            }
        })()));
};
exports.AddWmsLayer = AddWmsLayer;
//# sourceMappingURL=add-wms-layer.js.map