"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreLayer = void 0;
var tslib_1 = require("tslib");
var l7_source_1 = tslib_1.__importDefault(require("@antv/l7-source"));
var event_emitter_1 = tslib_1.__importDefault(require("@antv/event-emitter"));
var util_1 = require("@antv/util");
var utils_1 = require("../utils");
var constants_1 = require("./constants");
var attribute_1 = require("../adaptor/attribute");
var CoreLayer = /** @class */ (function (_super) {
    tslib_1.__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 : (0, util_1.uniqueId)('core-layer');
        _this.name = name ? name : _this.id;
        _this.options = (0, utils_1.deepMergeLayerOptions)(_this.getDefaultOptions(), options);
        _this.lastOptions = _this.options;
        _this.layer = _this.createLayer();
        _this.adaptorLayerAttr();
        _this.setSource(source);
        _this.emit(constants_1.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 = (0, util_1.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 && attribute_1.MappingAttribute.shape(this.layer, shape);
        // mapping size
        size && attribute_1.MappingAttribute.size(this.layer, size);
        // mapping color
        color && attribute_1.MappingAttribute.color(this.layer, color);
        // mapping scale
        scale && attribute_1.MappingAttribute.scale(this.layer, scale);
        // mapping texture
        texture && attribute_1.MappingAttribute.texture(this.layer, texture);
        // mapping style
        style && attribute_1.MappingAttribute.style(this.layer, style);
        // mapping animate
        animate && attribute_1.MappingAttribute.animate(this.layer, animate);
        // mapping state
        state && attribute_1.MappingAttribute.state(this.layer, state);
    };
    /**
     * 设置图层数据
     * 支持 source 配置项与 source 实例更新
     */
    CoreLayer.prototype.setSource = function (source) {
        if (source instanceof l7_source_1.default) {
            this.layer.setSource(source);
        }
        else {
            var data = source.data, option = tslib_1.__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(constants_1.CoreLayerEvent.ADD);
    };
    /**
     * 从场景移除
     */
    CoreLayer.prototype.remove = function () {
        if (!this.scene)
            return;
        this.scene.removeLayer(this.layer);
        this.emit(constants_1.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 ((0, util_1.isUndefined)(this.options.visible) || this.options.visible) {
            this.adaptorLayerAttr();
        }
        if (options.source && (0, utils_1.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 = (0, utils_1.deepMergeLayerOptions)(this.options, options);
    };
    // 更新: 更新图层属性配置
    CoreLayer.prototype.updateConfig = function (options) {
        if (!(0, util_1.isUndefined)(options.zIndex) && !(0, util_1.isEqual)(this.lastOptions.zIndex, this.options.zIndex)) {
            this.setIndex(options.zIndex);
        }
        if (!(0, util_1.isUndefined)(options.blend) && !(0, util_1.isEqual)(this.lastOptions.blend, this.options.blend)) {
            this.setBlend(options.blend);
        }
        if (!(0, util_1.isUndefined)(options.minZoom) && !(0, util_1.isEqual)(this.lastOptions.minZoom, this.options.minZoom)) {
            this.setMinZoom(options.minZoom);
        }
        if (!(0, util_1.isUndefined)(options.maxZoom) && !(0, util_1.isEqual)(this.lastOptions.maxZoom, this.options.maxZoom)) {
            this.setMinZoom(options.maxZoom);
        }
        if (!(0, util_1.isUndefined)(options.visible) && !(0, util_1.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 (constants_1.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 (constants_1.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 (constants_1.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;
}(event_emitter_1.default));
exports.CoreLayer = CoreLayer;
//# sourceMappingURL=core-layer.js.map