"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BubbleLayer = void 0;
var tslib_1 = require("tslib");
var util_1 = require("@antv/util");
var composite_layer_1 = require("../../core/composite-layer");
var point_layer_1 = require("../../core-layers/point-layer");
var text_layer_1 = require("../../core-layers/text-layer");
var adaptor_1 = require("./adaptor");
var constants_1 = require("../common/constants");
var constants_2 = require("./constants");
var label_layer_1 = require("../common/label-layer");
var BubbleLayer = /** @class */ (function (_super) {
    tslib_1.__extends(BubbleLayer, _super);
    function BubbleLayer(options) {
        var _this = _super.call(this, options) || this;
        /**
         * 复合图层类型
         */
        _this.type = composite_layer_1.CompositeLayer.LayerType.BubbleLayer;
        /**
         * 选中数据
         */
        _this.selectData = [];
        /**
         * 图层交互状态配置
         */
        _this.layerState = constants_2.DEFAULT_STATE;
        /**
         * 图层是否具有交互属性
         */
        _this.interaction = true;
        /**
         * 图层高亮回调
         */
        _this.onHighlighHandle = function (event) {
            var feature = event.feature, featureId = event.featureId;
            _this.setHighlightLayerSource(feature, featureId);
        };
        /**
         * 图层取消高亮回调
         */
        _this.onUnhighlighHandle = function () {
            _this.setHighlightLayerSource();
        };
        /**
         * 图层选中回调
         */
        _this.onSelectHandle = function (event) {
            var feature = event.feature, featureId = event.featureId;
            _this.handleSelectData(featureId, feature);
        };
        _this.initSubLayersEvent();
        return _this;
    }
    Object.defineProperty(BubbleLayer.prototype, "layer", {
        /**
         * 主图层
         */
        get: function () {
            return this.fillLayer;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BubbleLayer.prototype, "fillLayer", {
        /**
         * 填充图层
         */
        get: function () {
            return this.subLayers.getLayer('fillLayer');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BubbleLayer.prototype, "highlightStrokeLayer", {
        /**
         * 高亮描边图层
         */
        get: function () {
            return this.subLayers.getLayer('highlightStrokeLayer');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BubbleLayer.prototype, "selectFillLayer", {
        /**
         * 选中填充面图层
         */
        get: function () {
            return this.subLayers.getLayer('selectFillLayer');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BubbleLayer.prototype, "selectStrokeLayer", {
        /**
         * 选中描边图层
         */
        get: function () {
            return this.subLayers.getLayer('selectStrokeLayer');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BubbleLayer.prototype, "labelLayer", {
        /**
         * 标注文本图层
         */
        get: function () {
            return this.subLayers.getLayer('labelLayer');
        },
        enumerable: false,
        configurable: true
    });
    /**
     * 获取默认配置
     */
    BubbleLayer.prototype.getDefaultOptions = function () {
        return BubbleLayer.DefaultOptions;
    };
    /**
     * 创建子图层
     */
    BubbleLayer.prototype.createSubLayers = function () {
        var source = this.source;
        this.layerState = (0, adaptor_1.getDefaultState)(this.options.state);
        // 映射填充图层
        var fillLayer = new point_layer_1.PointLayer(tslib_1.__assign(tslib_1.__assign({}, this.getFillLayerOptions()), { id: 'fillLayer', shape: 'circle', source: source }));
        // 高亮描边图层
        var highlightStrokeLayer = new point_layer_1.PointLayer(tslib_1.__assign(tslib_1.__assign({}, this.gethigHlightStrokeLayerOptions()), { id: 'highlightStrokeLayer', shape: 'circle' }));
        // 选中填充图层
        var selectFillLayer = new point_layer_1.PointLayer(tslib_1.__assign(tslib_1.__assign({}, this.getSelectFillLayerOptions()), { id: 'selectFillLayer', shape: 'circle' }));
        // 选中描边图层
        var selectStrokeLayer = new point_layer_1.PointLayer(tslib_1.__assign(tslib_1.__assign({}, this.getSelectStrokeLayerOptions()), { id: 'selectStrokeLayer', shape: 'circle' }));
        // 标注图层
        var labelLayer = new text_layer_1.TextLayer(tslib_1.__assign(tslib_1.__assign({}, (0, label_layer_1.getLabelLayerOptions)(this.options)), { id: 'labelLayer', source: source }));
        var subLayers = [fillLayer, highlightStrokeLayer, selectFillLayer, selectStrokeLayer, labelLayer];
        return subLayers;
    };
    /**
     * 获取填充图层配置项
     */
    BubbleLayer.prototype.getFillLayerOptions = function () {
        var _a = (0, util_1.omit)(this.options, ['source']), visible = _a.visible, minZoom = _a.minZoom, maxZoom = _a.maxZoom, _b = _a.zIndex, zIndex = _b === void 0 ? 0 : _b, fillColor = _a.fillColor, radius = _a.radius, opacity = _a.opacity, strokeColor = _a.strokeColor, lineOpacity = _a.lineOpacity, lineWidth = _a.lineWidth, baseConfig = tslib_1.__rest(_a, ["visible", "minZoom", "maxZoom", "zIndex", "fillColor", "radius", "opacity", "strokeColor", "lineOpacity", "lineWidth"]);
        // omit source 目前是图层共享 source，避免更新时透传 source 数据
        var defaultState = this.layerState;
        var fillState = {
            active: defaultState.active.fillColor === false ? false : { color: defaultState.active.fillColor },
            select: false,
        };
        var fillStyle = {
            opacity: opacity,
            stroke: strokeColor,
            strokeOpacity: (0, util_1.isUndefined)(lineOpacity) ? opacity : lineOpacity,
            strokeWidth: lineWidth,
        };
        var options = tslib_1.__assign(tslib_1.__assign({}, baseConfig), { visible: visible, minZoom: minZoom, maxZoom: maxZoom, zIndex: zIndex, color: fillColor, size: radius, state: fillState, style: fillStyle });
        return options;
    };
    BubbleLayer.prototype.gethigHlightStrokeLayerOptions = function () {
        var _a;
        var _b = this.options, visible = _b.visible, minZoom = _b.minZoom, maxZoom = _b.maxZoom, _c = _b.zIndex, zIndex = _c === void 0 ? 0 : _c, radius = _b.radius;
        var defaultState = this.layerState;
        var strokeStyle = {
            opacity: 0,
            stroke: defaultState.active.strokeColor || undefined,
            strokeOpacity: defaultState.active.lineOpacity,
            strokeWidth: (_a = defaultState.active) === null || _a === void 0 ? void 0 : _a.lineWidth,
        };
        var options = {
            visible: visible && Boolean(defaultState.active.strokeColor),
            zIndex: zIndex + 0.1,
            minZoom: minZoom,
            maxZoom: maxZoom,
            source: constants_1.EMPTY_JSON_SOURCE,
            size: radius,
            style: strokeStyle,
        };
        return options;
    };
    BubbleLayer.prototype.getSelectFillLayerOptions = function () {
        var _a = this.options, visible = _a.visible, minZoom = _a.minZoom, maxZoom = _a.maxZoom, _b = _a.zIndex, zIndex = _b === void 0 ? 0 : _b, radius = _a.radius, opacity = _a.opacity;
        var defaultState = this.layerState;
        var color = defaultState.select.fillColor || undefined;
        var fillStyle = { opacity: opacity };
        var option = {
            visible: visible && Boolean(color),
            zIndex: zIndex + 0.1,
            minZoom: minZoom,
            maxZoom: maxZoom,
            source: constants_1.EMPTY_JSON_SOURCE,
            color: color,
            size: radius,
            style: fillStyle,
            state: { select: false, active: false },
        };
        return option;
    };
    BubbleLayer.prototype.getSelectStrokeLayerOptions = function () {
        var _a = this.options, visible = _a.visible, minZoom = _a.minZoom, maxZoom = _a.maxZoom, _b = _a.zIndex, zIndex = _b === void 0 ? 0 : _b, radius = _a.radius;
        var defaultState = this.layerState;
        var strokeStyle = {
            opacity: 0,
            stroke: defaultState.select.strokeColor || undefined,
            strokeOpacity: defaultState.select.lineOpacity,
            strokeWidth: defaultState.select.lineWidth,
        };
        var option = {
            visible: visible && Boolean(strokeStyle.stroke),
            zIndex: zIndex + 0.1,
            minZoom: minZoom,
            maxZoom: maxZoom,
            source: constants_1.EMPTY_JSON_SOURCE,
            size: radius,
            style: strokeStyle,
        };
        return option;
    };
    /**
     * 设置子图层数据
     */
    BubbleLayer.prototype.setSubLayersSource = function (source) {
        if (this.isSourceInstance(source)) {
            this.source = source;
            this.fillLayer.setSource(source);
            this.labelLayer.setSource(source);
        }
        else {
            var data = source.data, option = tslib_1.__rest(source, ["data"]);
            this.source.setData(data, option);
        }
        this.highlightStrokeLayer.changeData(constants_1.EMPTY_JSON_SOURCE);
        this.selectFillLayer.changeData(constants_1.EMPTY_JSON_SOURCE);
        this.selectStrokeLayer.changeData(constants_1.EMPTY_JSON_SOURCE);
    };
    /**
     * 设置高亮描边子图层数据
     */
    BubbleLayer.prototype.setHighlightLayerSource = function (feature, featureId) {
        if (featureId === void 0) { featureId = -999; }
        if (this.highlightData === featureId) {
            return;
        }
        var features = feature ? [feature] : [];
        var parser = this.source.parser;
        var data = parser.type === 'geojson' ? { type: 'FeatureCollection', features: features } : features;
        // if (!Array.isArray(this.options.radius) && typeof this.options.radius === 'object' && this.options.radius.field) {
        //   const scale = this.fillLayer.layer.getScale(this.options.radius.field);
        //   console.log('this.options.radius.field): ', this.options.radius.field);
        //   console.log('scale: ', scale);
        // }
        this.highlightStrokeLayer.changeData({ data: data, parser: parser });
        this.highlightData = featureId;
    };
    /**
     * 设置选中描边与填充子图层数据
     */
    BubbleLayer.prototype.setSelectLayerSource = function (selectData) {
        if (selectData === void 0) { selectData = []; }
        if (this.selectData.length === selectData.length &&
            (0, util_1.isEqual)(this.selectData.map(function (_a) {
                var featureId = _a.featureId;
                return featureId;
            }), selectData.map(function (_a) {
                var featureId = _a.featureId;
                return featureId;
            }))) {
            return;
        }
        var features = selectData.map(function (_a) {
            var feature = _a.feature;
            return feature;
        });
        var parser = this.source.parser;
        var data = parser.type === 'geojson' ? { type: 'FeatureCollection', features: features } : features;
        this.selectFillLayer.changeData({ data: data, parser: parser });
        this.selectStrokeLayer.changeData({ data: data, parser: parser });
        this.selectData = selectData;
    };
    /**
     * 初始化子图层事件
     */
    BubbleLayer.prototype.initSubLayersEvent = function () {
        // 初始化主图层交互事件
        this.fillLayer.off('mousemove', this.onHighlighHandle);
        this.fillLayer.off('mouseout', this.onHighlighHandle);
        this.fillLayer.off('click', this.onSelectHandle);
        this.selectData = [];
        this.highlightData = null;
        if (!this.options.state)
            return;
        // active
        if (this.options.state.active) {
            this.fillLayer.on('mousemove', this.onHighlighHandle);
            this.fillLayer.on('mouseout', this.onUnhighlighHandle);
        }
        // select
        if (this.options.state.select) {
            this.fillLayer.on('click', this.onSelectHandle);
        }
    };
    BubbleLayer.prototype.handleSelectData = function (featureId, feature) {
        var enabledMultiSelect = this.options.enabledMultiSelect;
        var selectData = (0, util_1.clone)(this.selectData);
        var index = selectData.findIndex(function (item) { return item.featureId === featureId; });
        if (index === -1) {
            if (enabledMultiSelect) {
                selectData.push({ feature: feature, featureId: featureId });
            }
            else {
                selectData = [{ feature: feature, featureId: featureId }];
            }
            this.emit('select', feature, (0, util_1.clone)(selectData));
        }
        else {
            var unselectFeature = selectData[index];
            if (enabledMultiSelect) {
                selectData.splice(index, 1);
            }
            else {
                selectData = [];
            }
            this.emit('unselect', unselectFeature, (0, util_1.clone)(selectData));
        }
        this.setSelectLayerSource(selectData);
    };
    /**
     * 更新
     */
    BubbleLayer.prototype.update = function (options) {
        _super.prototype.update.call(this, options);
        this.initSubLayersEvent();
    };
    /**
     * 更新: 更新配置
     */
    BubbleLayer.prototype.updateOption = function (options) {
        _super.prototype.updateOption.call(this, options);
        this.layerState = (0, adaptor_1.getDefaultState)(this.options.state);
    };
    /**
     * 更新子图层
     */
    BubbleLayer.prototype.updateSubLayers = function (options) {
        // 映射填充面图层
        this.fillLayer.update(this.getFillLayerOptions());
        // 高亮图层
        this.highlightStrokeLayer.update(this.gethigHlightStrokeLayerOptions());
        // 选中填充图层
        this.selectFillLayer.update(this.getSelectFillLayerOptions());
        // 选中描边图层
        this.selectStrokeLayer.update(this.getSelectStrokeLayerOptions());
        // 标注图层
        this.labelLayer.update((0, label_layer_1.getLabelLayerOptions)(this.options));
        // 重置高亮/选中状态
        if (this.options.visible) {
            if (!(0, util_1.isUndefined)(options.state) && !(0, util_1.isEqual)(this.lastOptions.state, this.options.state)) {
                this.updateHighlightSubLayers();
            }
            if (this.layerState.active.strokeColor) {
                this.setHighlightLayerSource();
            }
            if (this.layerState.select.fillColor || this.layerState.select.strokeColor) {
                this.setSelectLayerSource();
            }
        }
    };
    /**
     * 更新高亮及选中子图层
     */
    BubbleLayer.prototype.updateHighlightSubLayers = function () {
        var defaultState = this.layerState;
        var lasetDefaultState = (0, adaptor_1.getDefaultState)(this.lastOptions.state);
        if (lasetDefaultState.active.strokeColor !== defaultState.active.strokeColor) {
            defaultState.active.strokeColor ? this.highlightStrokeLayer.show() : this.highlightStrokeLayer.hide();
        }
        if (lasetDefaultState.select.fillColor !== defaultState.select.fillColor) {
            defaultState.select.fillColor ? this.selectFillLayer.show() : this.selectFillLayer.hide();
        }
        if (lasetDefaultState.select.strokeColor !== defaultState.select.strokeColor) {
            defaultState.select.strokeColor ? this.selectStrokeLayer.show() : this.selectStrokeLayer.hide();
        }
    };
    /**
     * 设置图层 zIndex
     */
    BubbleLayer.prototype.setIndex = function (zIndex) {
        this.fillLayer.setIndex(zIndex);
        this.highlightStrokeLayer.setIndex(zIndex + 0.1);
        this.selectFillLayer.setIndex(zIndex + 0.1);
        this.selectStrokeLayer.setIndex(zIndex + 0.1);
        this.labelLayer.setIndex(zIndex + 0.1);
    };
    /**
     * 设置图层高亮状态
     */
    BubbleLayer.prototype.setActive = function (field, value) {
        var source = this.fillLayer.source;
        var featureId = source.getFeatureId(field, value);
        if ((0, util_1.isUndefined)(featureId)) {
            throw new Error('Feature non-existent' + field + value);
        }
        if (this.layerState.active.fillColor) {
            this.fillLayer.layer.setActive(featureId);
        }
        if (this.layerState.active.strokeColor) {
            var feature = source.getFeatureById(featureId);
            this.setHighlightLayerSource(feature, featureId);
        }
    };
    /**
     * 设置图层选中状态
     */
    BubbleLayer.prototype.setSelect = function (field, value) {
        var source = this.fillLayer.source;
        var featureId = source.getFeatureId(field, value);
        if ((0, util_1.isUndefined)(featureId)) {
            throw new Error('Feature non-existent' + field + value);
        }
        if (this.layerState.select.strokeColor === false || this.layerState.select.fillColor === false) {
            return;
        }
        var feature = source.getFeatureById(featureId);
        this.handleSelectData(featureId, feature);
    };
    /**
     * 图层框选数据
     */
    BubbleLayer.prototype.boxSelect = function (bounds, callback) {
        this.fillLayer.boxSelect(bounds, callback);
    };
    /**
     * 默认配置项
     */
    BubbleLayer.DefaultOptions = constants_2.DEFAULT_OPTIONS;
    return BubbleLayer;
}(composite_layer_1.CompositeLayer));
exports.BubbleLayer = BubbleLayer;
//# sourceMappingURL=index.js.map