import { __extends } from "tslib";
import * as Cesium from '@taotieh/cesium_extend';
import { EventEmitter } from 'events';
export var PickModeEvents;
(function (PickModeEvents) {
    PickModeEvents["INTERSECT"] = "intersect";
})(PickModeEvents || (PickModeEvents = {}));
var PickMode = /** @class */ (function (_super) {
    __extends(PickMode, _super);
    function PickMode(conPointer, viewer) {
        var _this = _super.call(this) || this;
        _this._isActive = false;
        _this._handlePick = function (evt) {
            var viewer = _this._viewer;
            if (!_this.isActive || !evt) {
                return;
            }
            var x = evt.x, y = evt.y, target = evt.target;
            var pScreen = new Cesium.Cartesian2(x, y);
            var ellipsoid = viewer.scene.globe.ellipsoid;
            var cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(pScreen), viewer.scene);
            // 鼠标点击位置不一定在地球上
            if (!cartesian)
                return;
            var cartographic = ellipsoid.cartesianToCartographic(cartesian);
            var lat = Cesium.Math.toDegrees(cartographic.latitude);
            var lng = Cesium.Math.toDegrees(cartographic.longitude);
            var intersections = [];
            // console.log('click', {
            //   screen: { x, y },
            //   scene: { cartesian, cartographic, degrees: { lat, lng } }
            // });
            _this.emit(PickModeEvents.INTERSECT, intersections);
        };
        _this._conPointer = conPointer;
        _this._viewer = viewer;
        return _this;
    }
    PickMode.prototype._installEvents = function () {
        if (this._isActive) {
            return;
        }
        var conPointer = this._conPointer;
        conPointer.on('click', this._handlePick);
        this._isActive = true;
    };
    PickMode.prototype._uninstallEvents = function () { };
    Object.defineProperty(PickMode.prototype, "isActive", {
        get: function () {
            return this._isActive;
        },
        set: function (val) {
            var conP = this._conPointer;
            if (val === this._isActive)
                return;
            this._isActive = val;
            if (val) {
                this._installEvents();
            }
            else {
                this._uninstallEvents();
            }
        },
        enumerable: true,
        configurable: true
    });
    return PickMode;
}(EventEmitter));
export default PickMode;
