"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MgLayerSetGroup = void 0;
var tslib_1 = require("tslib");
var common_1 = require("./common");
var Image_1 = require("ol/source/Image");
var ImageMapGuide_1 = tslib_1.__importDefault(require("ol/source/ImageMapGuide"));
var Image_2 = tslib_1.__importDefault(require("ol/layer/Image"));
var never_1 = require("../utils/never");
var layer_set_group_base_1 = require("./layer-set-group-base");
var layer_set_1 = require("./layer-set");
var mapguide_debug_context_1 = require("../components/mapguide-debug-context");
var type_guards_1 = require("../utils/type-guards");
var string_1 = require("../utils/string");
var error_1 = require("./error");
var generic_layer_set_1 = require("./generic-layer-set");
var Image_3 = tslib_1.__importDefault(require("ol/layer/Image"));
var Tile_1 = tslib_1.__importDefault(require("ol/layer/Tile"));
var TileWMS_1 = tslib_1.__importDefault(require("ol/source/TileWMS"));
var ImageWMS_1 = tslib_1.__importDefault(require("ol/source/ImageWMS"));
var MgLayerSetGroup = /** @class */ (function (_super) {
    tslib_1.__extends(MgLayerSetGroup, _super);
    function MgLayerSetGroup(props, callback) {
        var _this = _super.call(this, callback) || this;
        _this.updateSelectionColor = function (color) { return _this.mainSet.updateSelectionColor(color); };
        _this.update = function (showGroups, showLayers, hideGroups, hideLayers) { return _this.mainSet.update(showGroups, showLayers, hideGroups, hideLayers); };
        var map = props.map, agentUri = props.agentUri;
        var isRtMap = (0, type_guards_1.isRuntimeMap)(map);
        var hasAgentUri = (0, string_1.strIsNullOrEmpty)(agentUri);
        if (isRtMap) {
            if (hasAgentUri) {
                throw new error_1.MgError("Expected agentUri to be set");
            }
        }
        var factory = new layer_set_1.MgInnerLayerSetFactory(callback, map, agentUri, props.imageFormat, props.selectionImageFormat, props.selectionColor);
        //NOTE: MapGuide does not like concurrent map rendering operations of the same mapname/session pair, which
        //this will do when the MG overlay is shared between the main viewer and the overview map. This is probably
        //because the concurrent requests both have SET[X/Y/SCALE/DPI/etc] parameters attached, so there is concurrent
        //requests to modify and persist the runtime map state (in addition to the rendering) and there is most likely
        //server-side lock contention to safely update the map state. Long story short: re-using the main overlay for the
        //OverviewMap control IS A BAD THING. Same thing happens with selection overlays
        //
        //As of OL6, this unwanted behavior from shared layers extends to all layer types, so what this means is that
        //we have to create 2 sets of layers, one for the main map and one for the overview map. We CANNOT and DO NOT share
        //any of these layer instances between the main map and the overview map!
        _this.mainSet = factory.create(props.locale, props.externalBaseLayers, props.stateless ? layer_set_1.MgLayerSetMode.Stateless : layer_set_1.MgLayerSetMode.Stateful, props.appSettings);
        _this.overviewSet = factory.create(props.locale, props.externalBaseLayers, layer_set_1.MgLayerSetMode.OverviewMap, props.appSettings);
        var progressNotifySources = _this.mainSet.getSourcesForProgressTracking();
        /*
        console.log("Draw Order:");
        for (let i = 0; i < layers.length; i++) {
            console.log(" " + layers[i].get(LayerProperty.LAYER_NAME));
        }
        */
        for (var _i = 0, progressNotifySources_1 = progressNotifySources; _i < progressNotifySources_1.length; _i++) {
            var src = progressNotifySources_1[_i];
            var suppress = src.get(common_1.SourceProperty.SUPPRESS_LOAD_EVENTS);
            if (!(suppress == true))
                _this.registerSourceEvents(src);
        }
        return _this;
    }
    /**
     * @override
     */
    MgLayerSetGroup.prototype.tryGetSubjectLayer = function () {
        if (this.mainSet instanceof generic_layer_set_1.GenericLayerSetOL) {
            return this.mainSet.subjectLayer;
        }
        else if (this.mainSet instanceof layer_set_1.MgLayerSetOL) {
            return this.mainSet.overlay;
        }
        return undefined;
    };
    /**
     * @override
     * @returns
     * @memberof MgLayerSetGroup
     */
    MgLayerSetGroup.prototype.tryGetWmsSource = function () {
        var subjectLayer = this.tryGetSubjectLayer();
        if (subjectLayer instanceof Image_3.default || subjectLayer instanceof Tile_1.default) {
            var source = subjectLayer.getSource();
            if (source instanceof ImageWMS_1.default || source instanceof TileWMS_1.default) {
                return [subjectLayer, source];
            }
        }
        return undefined;
    };
    MgLayerSetGroup.prototype.setMapGuideMocking = function (mock) {
        var allLayers = this.mainSet.getLayers();
        for (var _i = 0, allLayers_1 = allLayers; _i < allLayers_1.length; _i++) {
            var layer = allLayers_1[_i];
            if (layer instanceof Image_2.default) {
                var source = layer.getSource();
                if (source instanceof ImageMapGuide_1.default) {
                    if (typeof (mock) != 'undefined') {
                        switch (mock) {
                            case mapguide_debug_context_1.MapGuideMockMode.RenderPlaceholder:
                                source.setImageLoadFunction(layer_set_1.mockMapGuideImageLoadFunction);
                                break;
                            case mapguide_debug_context_1.MapGuideMockMode.DoNotRender:
                                source.setImageLoadFunction(layer_set_1.blankImageLoadFunction);
                                break;
                            default:
                                (0, never_1.assertNever)(mock);
                                break;
                        }
                    }
                    else {
                        source.setImageLoadFunction(Image_1.defaultImageLoadFunction);
                    }
                }
            }
        }
    };
    return MgLayerSetGroup;
}(layer_set_group_base_1.LayerSetGroupBase));
exports.MgLayerSetGroup = MgLayerSetGroup;
//# sourceMappingURL=mg-layer-set-group.js.map