import { __extends, __values } from "tslib";
import { uniqueId } from '@antv/util';
import EventEmitter from '@antv/event-emitter';
var LayerGroup = /** @class */ (function (_super) {
    __extends(LayerGroup, _super);
    function LayerGroup(layers, option) {
        if (layers === void 0) { layers = []; }
        if (option === void 0) { option = {}; }
        var _this = _super.call(this) || this;
        /**
         * 子图层
         */
        _this.layerMap = new Map();
        _this.name = option.name ? option.name : uniqueId('layerGroup');
        for (var index = 0; index < layers.length; index++) {
            var layer = layers[index];
            _this.addLayer(layer);
        }
        return _this;
    }
    /**
     * 图层组添加到地图上
     */
    LayerGroup.prototype.addTo = function (scene) {
        var e_1, _a;
        var _this = this;
        this.scene = scene;
        var layerIndex = 0;
        var layerLength = this.layerMap.size;
        try {
            for (var _b = __values(this.layerMap.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var layer = _c.value;
                layer.once('inited', function (e) {
                    layerIndex++;
                    _this.emit('inited-layer', e);
                    if (layerIndex === layerLength) {
                        _this.emit('inited-layers');
                    }
                });
                layer.addTo(scene);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    /**
     * 图层组从地图上移除
     */
    LayerGroup.prototype.remove = function () {
        if (this.scene) {
            this.removeAllLayer();
        }
    };
    /**
     * 图层组是否有该图层
     */
    LayerGroup.prototype.hasLayer = function (layer) {
        var layerId = typeof layer === 'string' ? layer : this.getLayerId(layer);
        return this.layerMap.has(layerId);
    };
    /**
     * 添加图层
     */
    LayerGroup.prototype.addLayer = function (layer) {
        var _this = this;
        var layerId = this.getLayerId(layer);
        this.layerMap.set(layerId, layer);
        if (this.scene) {
            layer.once('inited', function (e) { return _this.emit('inited-layer', e); });
            layer.addTo(this.scene);
        }
    };
    /**
     * 添加多个图层
     */
    LayerGroup.prototype.addLayers = function (layers) {
        var _this = this;
        layers.forEach(function (layer) {
            _this.addLayer(layer);
        });
    };
    /**
     * 根据图层 id 或图层实例移除 layer 图层
     */
    LayerGroup.prototype.removeLayer = function (layer) {
        var layerId = typeof layer === 'string' ? layer : this.getLayerId(layer);
        var findLayer = this.layerMap.get(layerId);
        if (!findLayer)
            return false;
        this.layerMap.delete(layerId);
        if (this.scene) {
            findLayer.remove();
        }
        return true;
    };
    /**
     * 获取图层组所有的图层
     */
    LayerGroup.prototype.getLayers = function () {
        return Array.from(this.layerMap.values());
    };
    /**
     * 根据图层 ID 获取图层
     */
    LayerGroup.prototype.getLayer = function (id) {
        return this.layerMap.get(id);
    };
    /**
     * 根据图层 name 获取图层
     */
    LayerGroup.prototype.getLayerByName = function (name) {
        return this.getLayers().find(function (itemLayer) { return itemLayer.name === name; });
    };
    /**
     * 移除所有的图层对象
     */
    LayerGroup.prototype.removeAllLayer = function () {
        var e_2, _a;
        try {
            for (var _b = __values(this.layerMap.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var layer = _c.value;
                if (this.scene) {
                    layer.remove();
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
        this.layerMap.clear();
    };
    /**
     * 是否图层组为空
     */
    LayerGroup.prototype.isEmpty = function () {
        return this.layerMap.size === 0;
    };
    /**
     * 设置图层组所有图层 zIndex
     */
    LayerGroup.prototype.setZIndex = function (zIndex) {
        var e_3, _a;
        try {
            for (var _b = __values(this.layerMap.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var layer = _c.value;
                layer.setIndex(zIndex);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_3) throw e_3.error; }
        }
    };
    /**
     * 根据图层获取图层 ID
     */
    LayerGroup.prototype.getLayerId = function (layer) {
        if ('id' in layer) {
            return layer.id;
        }
        if ('name' in layer) {
            return layer.name;
        }
        return layer.layer.id;
    };
    /**
     * 摧毁图层组
     */
    LayerGroup.prototype.destroy = function () {
        this.remove();
        this.off('*');
    };
    return LayerGroup;
}(EventEmitter));
export { LayerGroup };
//# sourceMappingURL=layer-group.js.map