"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeSelection = exports.CompositeSelectionLayer = void 0;
//FIXME: We are inlining extend of ol/Extent here so that this module can be included in any jest test case
//without jest complaining about not being able to import ol's ES6 modules. I don't know how to get jest to
//recognize ol imports. None of the solutions I tried worked!
function extend(extent1, extent2) {
    if (extent2[0] < extent1[0]) {
        extent1[0] = extent2[0];
    }
    if (extent2[2] > extent1[2]) {
        extent1[2] = extent2[2];
    }
    if (extent2[1] < extent1[1]) {
        extent1[1] = extent2[1];
    }
    if (extent2[3] > extent1[3]) {
        extent1[3] = extent2[3];
    }
    return extent1;
}
/**
 * A layer of a {@link CompositeSelection}
 *
 * @since 0.14
 */
var CompositeSelectionLayer = /** @class */ (function () {
    function CompositeSelectionLayer(layer) {
        this.layer = layer;
        this.features = [];
        if (this.isSelectedLayer(this.layer)) {
            for (var _i = 0, _a = this.layer.Feature; _i < _a.length; _i++) {
                var f = _a[_i];
                this.features.push(f);
            }
        }
        else {
            for (var _b = 0, _c = this.layer.features; _b < _c.length; _b++) {
                var f = _c[_b];
                var fp = [];
                for (var k in f.properties) {
                    fp.push({
                        Name: k,
                        Value: f.properties[k]
                    });
                }
                var fb = f.bounds ? f.bounds.join(" ") : undefined;
                this.features.push({
                    Bounds: fb,
                    Property: fp
                });
            }
        }
    }
    CompositeSelectionLayer.prototype.isSelectedLayer = function (layer) {
        return layer.Feature
            && layer["@id"]
            && layer["@name"];
    };
    /**
     * Gets the combined bounds of all selected features
     *
     * @returns The combined bounds of all selected features in the projection of the current map view
     */
    CompositeSelectionLayer.prototype.getBounds = function () {
        var bounds;
        if (this.isSelectedLayer(this.layer)) {
            this.layer.Feature.forEach(function (feat) {
                var b = feat.Bounds
                    ? feat.Bounds.split(" ").map(function (s) { return parseFloat(s); })
                    : undefined;
                if (b) {
                    if (!bounds) {
                        bounds = b;
                    }
                    else {
                        bounds = extend(bounds, b);
                    }
                }
            });
        }
        else {
            for (var _i = 0, _a = this.layer.features; _i < _a.length; _i++) {
                var f = _a[_i];
                if (f.bounds) {
                    if (bounds == null) {
                        bounds = f.bounds;
                    }
                    else {
                        bounds = extend(bounds, f.bounds);
                    }
                }
            }
        }
        return bounds;
    };
    CompositeSelectionLayer.prototype.getLayerId = function () {
        if (this.isSelectedLayer(this.layer)) {
            return this.layer["@id"];
        }
        return undefined;
    };
    CompositeSelectionLayer.prototype.getName = function () {
        if (this.isSelectedLayer(this.layer)) {
            return this.layer["@name"];
        }
        else {
            return this.layer.name;
        }
    };
    CompositeSelectionLayer.prototype.getFeatureAt = function (featureIndex) {
        return this.features[featureIndex];
    };
    CompositeSelectionLayer.prototype.getFeatureCount = function () { return this.features.length; };
    CompositeSelectionLayer.prototype.getLayerMetadata = function () {
        if (this.isSelectedLayer(this.layer)) {
            return this.layer.LayerMetadata;
        }
        return undefined;
    };
    return CompositeSelectionLayer;
}());
exports.CompositeSelectionLayer = CompositeSelectionLayer;
/**
 * A composition of a MapGuide selection set and a client-side vector feature selection
 *
 * @since 0.14
 */
var CompositeSelection = /** @class */ (function () {
    function CompositeSelection(mgSelection, clientSelection) {
        this.layers = [];
        if (mgSelection) {
            for (var _i = 0, _a = mgSelection.SelectedLayer; _i < _a.length; _i++) {
                var layer = _a[_i];
                this.layers.push(new CompositeSelectionLayer(layer));
            }
        }
        if (clientSelection) {
            for (var _b = 0, _c = clientSelection.layers; _b < _c.length; _b++) {
                var layer = _c[_b];
                this.layers.push(new CompositeSelectionLayer(layer));
            }
        }
    }
    CompositeSelection.prototype.getBounds = function () {
        if (this.layers.length == 0) {
            return undefined;
        }
        var bounds;
        for (var _i = 0, _a = this.layers; _i < _a.length; _i++) {
            var lyr = _a[_i];
            var layerBounds = lyr.getBounds();
            if (layerBounds) {
                if (bounds) {
                    bounds = extend(bounds, layerBounds);
                }
                else {
                    bounds = layerBounds;
                }
            }
        }
        return bounds;
    };
    CompositeSelection.prototype.getLayers = function () { return this.layers; };
    CompositeSelection.prototype.getLayerCount = function () {
        return this.layers.length;
    };
    CompositeSelection.prototype.getLayerAt = function (layerIndex) {
        return this.layers[layerIndex];
    };
    CompositeSelection.prototype.getFeatureAt = function (layerIndex, featureIndex) {
        var layer = this.getLayerAt(layerIndex);
        return layer === null || layer === void 0 ? void 0 : layer.getFeatureAt(featureIndex);
    };
    return CompositeSelection;
}());
exports.CompositeSelection = CompositeSelection;
//# sourceMappingURL=composite-selection.js.map