import { _ as __extends } from './MapEditView-24782753.js';
import * as maptalks from 'maptalks';
import simpleheat from 'simpleheat';
import 'react';
import 'lodash';
import 'uuid';
import 'mobx';
import 'lz-string';
import 'dayjs';
import 'react-dom';
import 'classnames';
import 'react-is';
import '@loadable/component';
import 'js-beautify';
import 'axios';
import 'superstruct';
import '@tanem/react-nprogress';
import 'jquery';
import 'chroma-js';
import 'js-cookie';
import 'is-email';
import 'is-uuid';
import 'is-url';
import 'anymatch';
import 'perf-request';
import '@syncpoint/wkx';
import 'buffer';
import '@turf/turf';
import '@mapbox/polylabel';
import 'turf-multipolygon';
import 'turf-polygon';
import 'gcoord';
import 'eventemitter2';
import 'geobuf';
import 'localforage';
import 'rc-menu';
import 'perfect-scrollbar';
import 'ahooks';
import 'jszip';
import 'file-saver';
import 'screenfull';
import 'sql-formatter-fork';
import 'array-move';
import 'turf-multilinestring';
import 'copy-to-clipboard';
import 'maptalks.three';
import 'ajv';
import 'spark-md5';
import 'react-draggable';
import 'react-virtualized';
import 'turf-point';
import 'mobx-react';
import 'decamelize';
import 'rbush';
import 'qs';
import 'maptalks.snap';
import 'react-use';
import 'react-rnd';
import 'react-beautiful-dnd';

var options = {
    max: 1,
    gradient: {
        0.4: 'blue',
        0.6: 'cyan',
        0.7: 'lime',
        0.8: 'yellow',
        1.0: 'red',
    },
    radius: 25,
    blur: 15,
    heatValueScale: 1,
    minOpacity: 0.05,
};
var HeatLayer = /** @class */ (function (_super) {
    __extends(HeatLayer, _super);
    function HeatLayer(id, heats, options) {
        var _this = this;
        if (!Array.isArray(heats)) {
            options = heats;
            // @ts-ignore
            heats = null;
        }
        _this = _super.call(this, id, options) || this;
        _this._heats = heats || [];
        return _this;
    }
    HeatLayer.prototype.getData = function () {
        return this._heats;
    };
    HeatLayer.prototype.setData = function (heats) {
        this._heats = heats || [];
        return this.redraw();
    };
    HeatLayer.prototype.addPoint = function (heat) {
        if (!heat) {
            return this;
        }
        if (heat[0] && Array.isArray(heat[0])) {
            maptalks.Util.pushIn(this._heats, heat);
        }
        else {
            this._heats.push(heat);
        }
        return this.redraw();
    };
    HeatLayer.prototype.onConfig = function (conf) {
        for (var p in conf) {
            // @ts-ignore
            if (options[p]) {
                return this.redraw();
            }
        }
        return this;
    };
    HeatLayer.prototype.redraw = function () {
        var renderer = this._getRenderer();
        if (renderer) {
            renderer.clearHeatCache();
            renderer.setToRedraw();
        }
        return this;
    };
    HeatLayer.prototype.isEmpty = function () {
        if (!this._heats.length) {
            return true;
        }
        return false;
    };
    HeatLayer.prototype.clear = function () {
        this._heats = [];
        this.redraw();
        this.fire('clear');
        return this;
    };
    /**
     * Export the HeatLayer's JSON.
     * @return {Object} layer's JSON
     */
    HeatLayer.prototype.toJSON = function (options) {
        if (!options) {
            options = {};
        }
        var json = {
            type: this.getJSONType(),
            id: this.getId(),
            options: this.config(),
        };
        var data = this.getData();
        if (options['clipExtent']) {
            var clipExtent = new maptalks.Extent(options['clipExtent']);
            var r = this._getHeatRadius();
            if (r) {
                clipExtent = clipExtent._expand(r);
            }
            var clipped = [];
            for (var i = 0, len = data.length; i < len; i++) {
                if (clipExtent.contains(new maptalks.Coordinate(data[i][0], data[i][1]))) {
                    clipped.push(data[i]);
                }
            }
            // @ts-ignore
            json['data'] = clipped;
        }
        else {
            // @ts-ignore
            json['data'] = data;
        }
        return json;
    };
    /**
     * Reproduce a HeatLayer from layer's JSON.
     * @param  {Object} json - layer's JSON
     * @return {maptalks.HeatLayer}
     * @static
     * @private
     * @function
     */
    HeatLayer.fromJSON = function (json) {
        if (!json || json['type'] !== 'HeatLayer') {
            return null;
        }
        return new HeatLayer(json['id'], json['data'], json['options']);
    };
    HeatLayer.prototype._getHeatRadius = function () {
        if (!this._getRenderer()) {
            return null;
        }
        return this._getRenderer()._heatRadius;
    };
    return HeatLayer;
}(maptalks.Layer));
HeatLayer.mergeOptions(options);
HeatLayer.registerJSONType('HeatLayer');
HeatLayer.registerRenderer('canvas', /** @class */ (function (_super) {
    __extends(class_1, _super);
    function class_1() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    class_1.prototype.draw = function () {
        var map = this.getMap(), layer = this.layer, extent = map.getContainerExtent();
        var maskExtent = this.prepareCanvas(), displayExtent = extent;
        if (maskExtent) {
            maskExtent = maskExtent.convertTo(function (c) { return map._pointToContainerPoint(c); });
            //out of layer mask
            if (!maskExtent.intersects(extent)) {
                this.completeRender();
                return;
            }
            displayExtent = extent.intersection(maskExtent);
        }
        if (!this._heater) {
            this._heater = simpleheat(this.canvas);
        }
        this._heater.radius(layer.options['radius'] || this._heater.defaultRadius, layer.options['blur']);
        if (layer.options['gradient']) {
            this._heater.gradient(layer.options['gradient']);
        }
        this._heater.max(layer.options['max']);
        //a cache of heat points' viewpoints.
        if (!this._heatViews) {
            this._heatViews = [];
        }
        var heats = layer.getData();
        if (heats.length === 0) {
            this.completeRender();
            return;
        }
        var data = this._heatData(heats, displayExtent);
        this._heater.data(data).draw(layer.options['minOpacity']);
        this.completeRender();
    };
    class_1.prototype.drawOnInteracting = function () {
        this.draw();
    };
    class_1.prototype._heatData = function (heats, displayExtent) {
        var map = this.getMap(), layer = this.layer;
        var projection = map.getProjection();
        var data = [], r = this._heater._r, max = layer.options['max'] === undefined ? 1 : layer.options['max'], cellSize = r / 2, 
        // @ts-ignore
        grid = [], panePos = map.offsetPlatform(), offsetX = panePos.x % cellSize, offsetY = panePos.y % cellSize;
        var heat, p, cell, x, y, k;
        displayExtent = displayExtent
            .expand(r)
            .convertTo(function (c) { return new maptalks.Point(map._containerPointToPrj(c)); });
        this._heatRadius = r;
        for (var i = 0, l = heats.length; i < l; i++) {
            heat = heats[i];
            if (!this._heatViews[i]) {
                this._heatViews[i] = projection.project(new maptalks.Coordinate(heat[0], heat[1]));
            }
            p = this._heatViews[i];
            if (displayExtent.contains(p)) {
                p = map._prjToContainerPoint(p);
                x = Math.floor((p.x - offsetX) / cellSize) + 2;
                y = Math.floor((p.y - offsetY) / cellSize) + 2;
                k = (heat[2] !== undefined ? +heat[2] : 0.1) * layer.options['heatValueScale'];
                // @ts-ignore
                grid[y] = grid[y] || [];
                cell = grid[y][x];
                if (!cell) {
                    grid[y][x] = [p.x, p.y, k];
                }
                else {
                    cell[0] = (cell[0] * cell[2] + p.x * k) / (cell[2] + k); // x
                    cell[1] = (cell[1] * cell[2] + p.y * k) / (cell[2] + k); // y
                    cell[2] += k; // cumulated intensity value
                }
            }
        }
        for (var i = 0, l = grid.length; i < l; i++) {
            if (grid[i]) {
                for (var j = 0, ll = grid[i].length; j < ll; j++) {
                    cell = grid[i][j];
                    if (cell) {
                        data.push([Math.round(cell[0]), Math.round(cell[1]), Math.min(cell[2], max)]);
                    }
                }
            }
        }
        return data;
    };
    class_1.prototype.onZoomEnd = function () {
        delete this._heatViews;
        // eslint-disable-next-line prefer-rest-params
        _super.prototype.onZoomEnd.apply(this, arguments);
    };
    class_1.prototype.onResize = function () {
        // eslint-disable-next-line prefer-rest-params
        _super.prototype.onResize.apply(this, arguments);
        if (this.canvas) {
            this._heater._width = this.canvas.width;
            this._heater._height = this.canvas.height;
        }
    };
    class_1.prototype.onRemove = function () {
        this.clearHeatCache();
        delete this._heater;
    };
    class_1.prototype.clearHeatCache = function () {
        delete this._heatViews;
    };
    return class_1;
}(maptalks.renderer.CanvasRenderer)));
var HeatLayer$1 = HeatLayer;

export { HeatLayer$1 as default };
