import {
    defaultValue,
    defined,
    DeveloperError,
    WebMercatorProjection,
    WebMercatorTilingScheme,
    Cartesian2,
    defineProperties,
    Cartographic,
    Cartesian3,
    when
} from "cesium/Cesium";

function t(t) {
    var i = (t = defaultValue(t, {})).data;
    if (!defined(i) || 0 == i.length)
        throw new DeveloperError("data is required.");
    this._options = defaultValue(t.heatmapoptions, {}),
        this._options.xField = defaultValue(this._options.xField, "x"),
        this._options.yField = defaultValue(this._options.yField, "y"),
        this._options.valueField = defaultValue(this._options.valueField, "value");
    for (var e = i[0][this._options.xField], s = i[0][this._options.yField], h = i[0][this._options.valueField], r = {
            east: e,
            west: e,
            north: s,
            south: s
        }, o = h, n = h, a = 0; a < i.length; a++)
        e = i[a][this._options.xField],
        s = i[a][this._options.yField],
        (h = i[a][this._options.valueField]) < o && (o = h),
        n < h && (n = h),
        e > r.east && (r.east = e),
        e < r.west && (r.west = e),
        s > r.north && (r.north = s),
        s < r.south && (r.south = s);
    defined(t.min) && (o = t.min),
        defined(t.max) && (n = t.max),
        this._min = o,
        this._max = n,
        this._data = i;
    var u = t.chInstance || h337;
    this._wmp = new WebMercatorProjection,
        this._mbounds = this.wgs84ToMercatorBB(r),
        this._options.gradient = defaultValue(this._options.gradient, {
            .25: "rgb(0,0,255)",
            .55: "rgb(0,255,0)",
            .85: "yellow",
            1: "rgb(255,0,0)"
        }),
        this._setWidthAndHeight(this._mbounds),
        this._options.radius = defaultValue(this._options.radius, Math.round(defaultValue(this._options.radius, this.width > this.height ? this.width / 60 : this.height / 60))),
        this._spacing = 1.5 * this._options.radius,
        this._xoffset = this._mbounds.west,
        this._yoffset = this._mbounds.south,
        this.width = Math.round(this.width + 2 * this._spacing),
        this.height = Math.round(this.height + 2 * this._spacing),
        this._mbounds.west -= this._spacing * this._factor,
        this._mbounds.east += this._spacing * this._factor,
        this._mbounds.south -= this._spacing * this._factor,
        this._mbounds.north += this._spacing * this._factor,
        this.bounds = this.mercatorToWgs84BB(this._mbounds),
        this._container = this._getContainer(this.width, this.height),
        this._options.container = this._container,
        this._heatmap = u.create(this._options),
        this._image = this._canvas = this._container.children[0],
        this._tilingScheme = new WebMercatorTilingScheme({
            rectangleSouthwestInMeters: new Cartesian2(this._mbounds.west, this._mbounds.south),
            rectangleNortheastInMeters: new Cartesian2(this._mbounds.east, this._mbounds.north)
        }),
        this._texture = void 0,
        this._tileWidth = this.width,
        this._tileHeight = this.height,
        this._ready = !1,
        this.readyPromise = when.defer(),
        this._ready = this.setWGS84Data(this._min, this._max, this._data),
        this.readyPromise.resolve(!0)
        this._name = 'heatmap'
}
defineProperties(t.prototype, {
    url: {
        get: function() {
            return this._url
        }
    },
    tileWidth: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("tileWidth must not be called before the imagery provider is ready.");
            return this._tileWidth
        }
    },
    tileHeight: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("tileHeight must not be called before the imagery provider is ready.");
            return this._tileHeight
        }
    },
    maximumLevel: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("maximumLevel must not be called before the imagery provider is ready.");
            return 0
        }
    },
    minimumLevel: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("minimumLevel must not be called before the imagery provider is ready.");
            return 0
        }
    },
    tilingScheme: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("tilingScheme must not be called before the imagery provider is ready.");
            return this._tilingScheme
        }
    },
    rectangle: {
        get: function() {
            return this._tilingScheme.rectangle
        }
    },
    tileDiscardPolicy: {
        get: function() {
            if (!this._ready)
                throw new DeveloperError("tileDiscardPolicy must not be called before the imagery provider is ready.")
        }
    },
    errorEvent: {
        get: function() {
            return this._errorEvent
        }
    },
    ready: {
        get: function() {
            return this._ready
        }
    },
    credit: {
        get: function() {
            return this._credit
        }
    },
    hasAlphaChannel: {
        get: function() {
            return !0
        }
    }
});
t.prototype._setWidthAndHeight = function(t) {
    var i = 2e3,
        e = 700;
    this.width = 0 < t.east && t.west < 0 ? t.east + Math.abs(t.west) : Math.abs(t.east - t.west),
        this.height = 0 < t.north && t.south < 0 ? t.north + Math.abs(t.south) : Math.abs(t.north - t.south),
        this._factor = 1,
        this.width > this.height && this.width > i ? (this._factor = this.width / i,
            this.height / this._factor < e && (this._factor = this.height / e)) : this.height > this.width && this.height > i ? (this._factor = this.height / i,
            this.width / this._factor < e && (this._factor = this.width / e)) : this.width < this.height && this.width < e ? (this._factor = this.width / e,
            this.height / this._factor > i && (this._factor = this.height / i)) : this.height < this.width && this.height < e && (this._factor = this.height / e,
            this.width / this._factor > i && (this._factor = this.width / i)),
        this.width = this.width / this._factor,
        this.height = this.height / this._factor
}
t.prototype._getContainer = function(t, i, e) {
    var s = document.createElement("div");
    return e && s.setAttribute("id", e),
        s.setAttribute("style", "width: " + t + "px; height: " + i + "px; margin: 0px; display: none;"),
        document.body.appendChild(s),
        s
}
t.prototype.wgs84ToMercator = function(t) {
    return this._wmp.project(Cartographic.fromDegrees(t[this._options.xField], t[this._options.yField]))
}
t.prototype.wgs84ToMercatorBB = function(t) {
    var i = this._wmp.project(Cartographic.fromDegrees(t.east, t.north)),
        e = this._wmp.project(Cartographic.fromDegrees(t.west, t.south));
    return {
        north: i.y,
        south: e.y,
        east: i.x,
        west: e.x
    }
}
t.prototype.mercatorToWgs84 = function(t) {
    var i = this._wmp.unproject(new Cartesian3(t.x, t.y));
    return {
        x: i.longitude,
        y: i.latitude
    }
}
t.prototype.mercatorToWgs84BB = function(t) {
    var i = this._wmp.unproject(new Cartesian3(t.west, t.south)),
        e = this._wmp.unproject(new Cartesian3(t.east, t.north));
    return {
        north: this.rad2deg(e.latitude),
        east: this.rad2deg(e.longitude),
        south: this.rad2deg(i.latitude),
        west: this.rad2deg(i.longitude)
    }
}
t.prototype.deg2rad = function(t) {
    return t * (Math.PI / 180)
}
t.prototype.rad2deg = function(t) {
    return t / (Math.PI / 180)
}
t.prototype.wgs84PointToHeatmapPoint = function(t) {
    return this.mercatorPointToHeatmapPoint(this.wgs84ToMercator(t))
}
t.prototype.mercatorPointToHeatmapPoint = function(t) {
    var i = {};
    return i[this._options.xField] = Math.round((t.x - this._xoffset) / this._factor + this._spacing),
        i[this._options.yField] = Math.round((t.y - this._yoffset) / this._factor + this._spacing),
        i[this._options.yField] = this.height - i[this._options.yField],
        i
}
t.prototype.setData = function(t, i, e) {
    return !!(e && 0 < e.length && null !== t && !1 !== t && null !== i && !1 !== i) && (this._heatmap.setData({
            min: t,
            max: i,
            data: e
        }),
        !0)
}
t.prototype.setWGS84Data = function(t, i, e) {
    if (e && 0 < e.length && null !== t && !1 !== t && null !== i && !1 !== i) {
        for (var s = [], h = 0; h < e.length; h++) {
            var r = e[h],
                o = this.wgs84PointToHeatmapPoint(r),
                n = r[this._options.valueField];
            n && (o[this._options.valueField] = n),
                s.push(o)
        }
        return this.setData(t, i, s)
    }
    return !1
}
t.prototype.getTileCredits = function(t, i, e) {}
t.prototype.requestImage = function(t, i, e) {
    if (!this._ready)
        throw new DeveloperError("requestImage must not be called before the imagery provider is ready.");
    return this._image
}
t.prototype.pickFeatures = function() {}

export default t