import { __extends, __rest } from "tslib";
import Source from '@antv/l7-source';
import EventEmitter from '@antv/event-emitter';
import { isEqual, isUndefined, uniqueId, omit } from '@antv/util';
import { deepMergeLayerOptions, isSourceChanged } from '../utils';
import { CoreLayerEvent, OriginLayerEventList } from './constants';
import { MappingAttribute } from '../adaptor/attribute';
var CoreLayer = /** @class */ (function (_super) {
    __extends(CoreLayer, _super);
    function CoreLayer(options) {
        var _this = _super.call(this) || this;
        /**
         * 是否是复合图层
         */
        _this.isComposite = false;
        var id = options.id, name = options.name, source = options.source;
        _this.id = id ? id : uniqueId('core-layer');
        _this.name = name ? name : _this.id;
        _this.options = deepMergeLayerOptions(_this.getDefaultOptions(), options);
        _this.lastOptions = _this.options;
        _this.layer = _this.createLayer();
        _this.adaptorLayerAttr();
        _this.setSource(source);
        _this.emit(CoreLayerEvent.CREATED);
        return _this;
    }
    Object.defineProperty(CoreLayer.prototype, "inited", {
        /**
         * 图层是否初始化成功
         */
        get: function () {
            return this.layer.inited;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(CoreLayer.prototype, "source", {
        /**
         * 图层的 source 实例
         */
        get: function () {
            return this.layer.getSource();
        },
        enumerable: false,
        configurable: true
    });
    /**
     * 获取默认配置
     */
    CoreLayer.prototype.getDefaultOptions = function () {
        return CoreLayer.DefaultOptions;
    };
    /**
     * 获取创建图层配置项
     */
    CoreLayer.prototype.getLayerConfig = function () {
        var config = omit(this.options, [
            'source',
            'shape',
            'color',
            'size',
            'scale',
            'texture',
            'style',
            'animate',
            'state',
        ]);
        return config;
    };
    /**
     * 适配属性配置
     */
    CoreLayer.prototype.adaptorAttrOptions = function (options) {
        return options;
    };
    /**
     * 映射图层属性
     */
    CoreLayer.prototype.adaptorLayerAttr = function () {
        var _a = this.adaptorAttrOptions(this.options), shape = _a.shape, color = _a.color, size = _a.size, scale = _a.scale, texture = _a.texture, style = _a.style, animate = _a.animate, state = _a.state;
        // mapping shape
        shape && MappingAttribute.shape(this.layer, shape);
        // mapping size
        size && MappingAttribute.size(this.layer, size);
        // mapping color
        color && MappingAttribute.color(this.layer, color);
        // mapping scale
        scale && MappingAttribute.scale(this.layer, scale);
        // mapping texture
        texture && MappingAttribute.texture(this.layer, texture);
        // mapping style
        style && MappingAttribute.style(this.layer, style);
        // mapping animate
        animate && MappingAttribute.animate(this.layer, animate);
        // mapping state
        state && MappingAttribute.state(this.layer, state);
    };
    /**
     * 设置图层数据
     * 支持 source 配置项与 source 实例更新
     */
    CoreLayer.prototype.setSource = function (source) {
        if (source instanceof Source) {
            this.layer.setSource(source);
        }
        else {
            var data = source.data, option = __rest(source, ["data"]);
            var layerSource = this.layer.getSource();
            if (layerSource) {
                this.layer.setData(data, option);
            }
            else {
                this.layer.source(data, option);
            }
        }
    };
    /**
     * 添加到场景
     */
    CoreLayer.prototype.addTo = function (scene) {
        this.scene = scene;
        scene.addLayer(this.layer);
        this.emit(CoreLayerEvent.ADD);
    };
    /**
     * 从场景移除
     */
    CoreLayer.prototype.remove = function () {
        if (!this.scene)
            return;
        this.scene.removeLayer(this.layer);
        this.emit(CoreLayerEvent.REMOVE);
    };
    /**
     * 更新
     */
    CoreLayer.prototype.update = function (options) {
        var _a, _b;
        this.updateOption(options);
        this.updateConfig(options);
        // 停止渲染，避免属性更新与数据更新造成多次内部调用 scene render
        (_a = this.scene) === null || _a === void 0 ? void 0 : _a.setEnableRender(false);
        if (isUndefined(this.options.visible) || this.options.visible) {
            this.adaptorLayerAttr();
        }
        if (options.source && isSourceChanged(options.source, this.lastOptions.source)) {
            this.changeData(options.source);
        }
        (_b = this.scene) === null || _b === void 0 ? void 0 : _b.setEnableRender(true);
        this.render();
    };
    /**
     * 更新: 更新配置
     */
    CoreLayer.prototype.updateOption = function (options) {
        this.lastOptions = this.options;
        this.options = deepMergeLayerOptions(this.options, options);
    };
    // 更新: 更新图层属性配置
    CoreLayer.prototype.updateConfig = function (options) {
        if (!isUndefined(options.zIndex) && !isEqual(this.lastOptions.zIndex, this.options.zIndex)) {
            this.setIndex(options.zIndex);
        }
        if (!isUndefined(options.blend) && !isEqual(this.lastOptions.blend, this.options.blend)) {
            this.setBlend(options.blend);
        }
        if (!isUndefined(options.minZoom) && !isEqual(this.lastOptions.minZoom, this.options.minZoom)) {
            this.setMinZoom(options.minZoom);
        }
        if (!isUndefined(options.maxZoom) && !isEqual(this.lastOptions.maxZoom, this.options.maxZoom)) {
            this.setMinZoom(options.maxZoom);
        }
        if (!isUndefined(options.visible) && !isEqual(this.lastOptions.visible, this.options.visible)) {
            options.visible ? this.show() : this.hide();
        }
    };
    CoreLayer.prototype.render = function () {
        if (this.scene) {
            this.scene.render();
        }
    };
    /**
     * 更新数据
     * 支持 source 配置项
     */
    CoreLayer.prototype.changeData = function (source) {
        this.setSource(source);
    };
    /**
     * 设置图层 zIndex
     */
    CoreLayer.prototype.setIndex = function (zIndex) {
        this.layer.setIndex(zIndex);
    };
    /**
     * 设置图层 blend
     */
    CoreLayer.prototype.setBlend = function (blend) {
        this.layer.setBlend(blend);
    };
    /**
     * 设置图层 minZoom
     */
    CoreLayer.prototype.setMinZoom = function (minZoom) {
        this.layer.setMinZoom(minZoom);
    };
    /**
     * 设置图层 maxZoom
     */
    CoreLayer.prototype.setMaxZoom = function (maxZoom) {
        this.layer.setMaxZoom(maxZoom);
    };
    /**
     * 显示图层
     */
    CoreLayer.prototype.show = function () {
        if (!this.layer.inited)
            return;
        this.layer.show();
    };
    /**
     * 隐藏图层
     */
    CoreLayer.prototype.hide = function () {
        if (!this.layer.inited)
            return;
        this.layer.hide();
    };
    /**
     * 切换图层显隐状态
     */
    CoreLayer.prototype.toggleVisible = function () {
        this.isVisible() ? this.hide() : this.show();
    };
    /**
     * 图层是否可见
     */
    CoreLayer.prototype.isVisible = function () {
        return this.layer.inited ? this.layer.isVisible() : this.options.visible || false;
    };
    /**
     * 图层框选数据
     */
    CoreLayer.prototype.boxSelect = function (bounds, callback) {
        this.layer.boxSelect(bounds, callback);
    };
    /**
     * 定位到当前图层数据范围
     */
    CoreLayer.prototype.fitBounds = function (fitBoundsOptions) {
        this.layer.fitBounds(fitBoundsOptions);
    };
    /**
     * 获取图例数据
     */
    CoreLayer.prototype.getLegendItems = function (type) {
        return this.layer.getLegendItems(type);
    };
    /**
     * 事件代理: 绑定事件
     */
    CoreLayer.prototype.on = function (name, callback) {
        if (OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.on(name, callback);
        }
        else {
            _super.prototype.on.call(this, name, callback);
        }
        return this;
    };
    /**
     * 事件代理: 绑定一次事件
     */
    CoreLayer.prototype.once = function (name, callback) {
        if (OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.once(name, callback);
        }
        else {
            _super.prototype.once.call(this, name, callback);
        }
        return this;
    };
    /**
     * 事件代理: 解绑事件
     */
    CoreLayer.prototype.off = function (name, callback) {
        if (OriginLayerEventList.indexOf(name) !== -1) {
            this.layer.off(name, callback);
        }
        else {
            _super.prototype.off.call(this, name, callback);
        }
        return this;
    };
    /**
     * 摧毁
     */
    CoreLayer.prototype.destroy = function () {
        this.layer.destroy();
    };
    /**
     * 默认的 options 配置项
     */
    CoreLayer.DefaultOptions = {};
    return CoreLayer;
}(EventEmitter));
export { CoreLayer };
//# sourceMappingURL=core-layer.js.map