"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeLayer = void 0;
var tslib_1 = require("tslib");
var util_1 = require("@antv/util");
var event_emitter_1 = tslib_1.__importDefault(require("@antv/event-emitter"));
var l7_source_1 = tslib_1.__importDefault(require("@antv/l7-source"));
var utils_1 = require("../utils");
var types_1 = require("../types");
var constants_1 = require("./constants");
var layer_group_1 = require("./layer-group");
var CompositeLayer = /** @class */ (function (_super) {
    tslib_1.__extends(CompositeLayer, _super);
    function CompositeLayer(options) {
        var _this = _super.call(this) || this;
        /**
         * 是否是复合图层
         */
        _this.isComposite = true;
        var id = options.id, name = options.name;
        _this.id = id ? id : (0, util_1.uniqueId)('composite-layer');
        _this.name = name ? name : _this.id;
        _this.options = (0, utils_1.deepMergeLayerOptions)(_this.getDefaultOptions(), options);
        _this.lastOptions = _this.options;
        _this.source = _this.createSource();
        var layers = _this.createSubLayers();
        _this.subLayers = new layer_group_1.LayerGroup(layers);
        _this.emit(constants_1.CompositeLayerEvent.CREATED);
        return _this;
    }
    /**
     * 获取默认配置
     */
    CompositeLayer.prototype.getDefaultOptions = function () {
        return CompositeLayer.DefaultOptions;
    };
    /**
     * 创建图层间共享 source 方法
     */
    CompositeLayer.prototype.createSource = function () {
        var sourceOptions = this.options.source;
        var data = sourceOptions.data, sourceCFG = tslib_1.__rest(sourceOptions, ["data"]);
        var source = this.isSourceInstance(sourceOptions) ? sourceOptions : new l7_source_1.default(data, sourceCFG);
        return source;
    };
    /**
     * 判断 source 是否是实例工具方法
     */
    CompositeLayer.prototype.isSourceInstance = function (source) {
        if (source instanceof l7_source_1.default) {
            return true;
        }
        return false;
    };
    /**
     * 设置子图层数据
     *支持 source 配置项与 source 实例更新
     */
    CompositeLayer.prototype.setSubLayersSource = function (source) {
        this.layer.changeData(source);
    };
    /**
     * 初始化子图层相关事件绑定
     */
    CompositeLayer.prototype.initSubLayersEvent = function () {
        //
    };
    /**
     * 添加到场景
     */
    CompositeLayer.prototype.addTo = function (scene) {
        this.scene = scene;
        this.subLayers.addTo(scene);
        this.emit(constants_1.CompositeLayerEvent.ADD);
    };
    /**
     * 从场景移除
     */
    CompositeLayer.prototype.remove = function () {
        if (!this.scene)
            return;
        this.subLayers.remove();
        this.emit(constants_1.CompositeLayerEvent.REMOVE);
    };
    /**
     * 更新
     */
    CompositeLayer.prototype.update = function (options) {
        this.updateOption(options);
        // 数据更新
        if (options.source && (0, utils_1.isSourceChanged)(options.source, this.lastOptions.source)) {
            this.changeData(options.source);
        }
        // 图层更新
        this.updateSubLayers(options);
    };
    /**
     * 更新: 更新配置
     */
    CompositeLayer.prototype.updateOption = function (options) {
        this.lastOptions = this.options;
        this.options = (0, utils_1.deepMergeLayerOptions)(this.options, options);
    };
    CompositeLayer.prototype.render = function () {
        if (this.scene) {
            this.scene.render();
        }
    };
    /**
     * 更新数据
     */
    CompositeLayer.prototype.changeData = function (source) {
        this.setSubLayersSource(source);
    };
    /**
     * 设置图层 zIndex
     */
    CompositeLayer.prototype.setIndex = function (zIndex) {
        this.subLayers.setZIndex(zIndex);
    };
    /**
     * 设置图层 blend
     */
    CompositeLayer.prototype.setBlend = function (blend) {
        this.layer.setBlend(blend);
    };
    /**
     * 设置图层 minZoom
     */
    CompositeLayer.prototype.setMinZoom = function (minZoom) {
        this.subLayers.getLayers().forEach(function (layer) {
            layer.setMinZoom(minZoom);
        });
    };
    /**
     * 设置图层 maxZoom
     */
    CompositeLayer.prototype.setMaxZoom = function (maxZoom) {
        this.subLayers.getLayers().forEach(function (layer) {
            layer.setMaxZoom(maxZoom);
        });
    };
    /**
     * 显示图层
     */
    CompositeLayer.prototype.show = function () {
        if (!this.layer.inited)
            return;
        this.subLayers.getLayers().forEach(function (layer) {
            layer.show();
        });
    };
    /**
     * 隐藏图层
     */
    CompositeLayer.prototype.hide = function () {
        if (!this.layer.inited)
            return;
        this.subLayers.getLayers().forEach(function (layer) {
            layer.hide();
        });
    };
    /**
     * 切换图层显隐状态
     */
    CompositeLayer.prototype.toggleVisible = function () {
        this.isVisible() ? this.hide() : this.show();
    };
    /**
     * 图层是否可见
     */
    CompositeLayer.prototype.isVisible = function () {
        return this.layer.inited ? this.layer.isVisible() : this.options.visible;
    };
    /**
     * 定位到当前图层数据范围
     */
    CompositeLayer.prototype.fitBounds = function (fitBoundsOptions) {
        this.layer.fitBounds(fitBoundsOptions);
    };
    /**
     * 获取图例数据
     */
    CompositeLayer.prototype.getLegendItems = function (type) {
        return this.layer.getLegendItems(type);
    };
    /**
     * 获取颜色图例数据
     */
    CompositeLayer.prototype.getColorLegendItems = function () {
        var colorLegendItems = this.layer.getLegendItems('color');
        if (Array.isArray(colorLegendItems) && colorLegendItems.length !== 0) {
            var items = colorLegendItems;
            return items;
        }
        return [];
    };
    /**
     * 摧毁
     */
    CompositeLayer.prototype.destroy = function () {
        this.subLayers.destroy();
    };
    /**
     * 事件代理: 绑定事件
     */
    CompositeLayer.prototype.on = function (name, callback) {
        if (constants_1.OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.on(name, callback);
        }
        else {
            _super.prototype.on.call(this, name, callback);
        }
        return this;
    };
    /**
     * 事件代理: 绑定一次事件
     */
    CompositeLayer.prototype.once = function (name, callback) {
        if (constants_1.OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.once(name, callback);
        }
        else {
            _super.prototype.once.call(this, name, callback);
        }
        return this;
    };
    /**
     * 事件代理: 解绑事件
     */
    CompositeLayer.prototype.off = function (name, callback) {
        if (constants_1.OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.off(name, callback);
        }
        else {
            _super.prototype.off.call(this, name, callback);
        }
        return this;
    };
    /**
     * 复合图层类型
     */
    CompositeLayer.LayerType = types_1.CompositeLayerType;
    /**
     * 默认的 options 配置项
     */
    CompositeLayer.DefaultOptions = {};
    return CompositeLayer;
}(event_emitter_1.default));
exports.CompositeLayer = CompositeLayer;
//# sourceMappingURL=composite-layer.js.map