"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useProps4 = _interopRequireDefault(require("../_util/useProps"));
var _cesium = require("cesium");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var prefix = 'cs-map-navigation';
var useMouse = function useMouse(props) {
  var timeRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    return function () {
      onClear();
    };
  }, []);

  // 鼠标按下
  var onMouseDown = function onMouseDown(type, event) {
    onClear();
    timeRef.current = window.setInterval(function () {
      var _props$callback;
      (_props$callback = props.callback) === null || _props$callback === void 0 || _props$callback.call(props, type, event);
    });
  };

  // 鼠标抬起
  var onMouseUp = function onMouseUp() {
    onClear();
  };

  // 清除定时器
  var onClear = function onClear() {
    if (timeRef.current) {
      window.clearInterval(timeRef.current);
      timeRef.current = null;
    }
  };
  return {
    onMouseDown: onMouseDown,
    onMouseUp: onMouseUp
  };
};

// 视角移动
var useViewMove = function useViewMove() {
  var _useProps = (0, _useProps4["default"])(),
    map = _useProps.map;
  var _useMouse = useMouse({
      callback: function callback(type) {
        // 获取相机到椭球表面的距离
        var cameraHeight = map.scene.globe.ellipsoid.cartesianToCartographic(map.camera.position).height;

        // 根据相机到椭球表面的距离，计算移动速度
        var moveRate = cameraHeight / 200.0;
        map.camera[type](moveRate);
      }
    }),
    onMouseDown = _useMouse.onMouseDown,
    onMouseUp = _useMouse.onMouseUp;
  return {
    onMouseDown: onMouseDown,
    onMouseUp: onMouseUp
  };
};

// 角度
var Angle = function Angle() {
  var _useProps2 = (0, _useProps4["default"])(),
    map = _useProps2.map;
  var barRef = (0, _react.useRef)();

  // // 监听角度变化
  // useEffect(() => {
  //     const move = ()=> {
  //         // 将角度转换为弧度
  //         const rotation = CesiumMath.toDegrees(map.camera.pitch).toFixed(2);
  //         let radians = (rotation * Math.PI) / 180;
  //
  //         // 获取元素中心点位置
  //         const rect = domRef.current.getBoundingClientRect();
  //         const centerX = rect.width / 2;
  //         const centerY = rect.height / 2;
  //
  //         let distance = rect.width - 6;
  //
  //         // 计算目标点的x和y坐标
  //         let left = centerX + distance * Math.cos(radians);
  //         let top = centerY + (distance / 2) * Math.sin(radians);
  //
  //         barRef.current.style.left = left + "px";
  //         barRef.current.style.top = top + "px";
  //
  //     }
  //
  //     map.scene.postRender.addEventListener(move)
  //     return () => {
  //         map.scene.postRender.removeEventListener(move)
  //     }
  // }, []);

  var domRef = (0, _react.useRef)(null);
  var onMouseDown = function onMouseDown(event) {
    event.preventDefault();
    var startX = event.clientX;
    var startY = event.clientY;
    var handleRotate = function handleRotate(e) {
      var curX = e.clientX;
      var curY = e.clientY;
      var rect = domRef.current.getBoundingClientRect();
      // 获取元素中心点位置
      var centerX = rect.left + rect.width / 2;
      var centerY = rect.top + rect.height / 2;

      // 旋转前的角度
      var rotateDegreeBefore = Math.atan2(startY - centerY, startX - centerX) / (Math.PI / 180);

      // 旋转后的角度
      var rotateDegreeAfter = Math.atan2(curY - centerY, curX - centerX) / (Math.PI / 180);

      // 获取旋转的角度值
      var nowRotate = rotateDegreeAfter - rotateDegreeBefore;
      var radians = _cesium.Math.toRadians(nowRotate);
      map.camera.lookUp(radians);
      startX = curX;
      startY = curY;
    };
    var handleMouseUp = function handleMouseUp() {
      document.removeEventListener('mousemove', handleRotate);
      document.removeEventListener('mouseup', handleMouseUp);
    };
    document.addEventListener('mousemove', handleRotate);
    document.addEventListener('mouseup', handleMouseUp);
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: domRef,
    className: (0, _classnames["default"])("".concat(prefix, "-angle"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    ref: barRef
    // onMouseDown={onMouseDown}
    ,
    className: (0, _classnames["default"])("".concat(prefix, "-angle-bar")),
    style: {
      left: 58,
      top: 5
    }
  }));
};

// 缩放
var Zoom = function Zoom() {
  var _useViewMove = useViewMove(),
    _onMouseDown = _useViewMove.onMouseDown,
    _onMouseUp = _useViewMove.onMouseUp;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-zoom"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-zoom-in")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown('moveForward');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp('moveForward');
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-zoom-bar"))
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-zoom-out")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown('moveBackward');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp('moveBackward');
    }
  }));
};

// 箭头
var Arrows = function Arrows() {
  var _useViewMove2 = useViewMove(),
    _onMouseDown2 = _useViewMove2.onMouseDown,
    _onMouseUp2 = _useViewMove2.onMouseUp;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-arrows"))
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-arrows-e-active")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown2('moveRight');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp2('moveRight');
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-arrows-n-active")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown2('moveUp');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp2('moveUp');
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-arrows-s-active")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown2('moveDown');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp2('moveDown');
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-arrows-w-active")),
    onMouseDown: function onMouseDown() {
      return _onMouseDown2('moveLeft');
    },
    onMouseUp: function onMouseUp() {
      return _onMouseUp2('moveLeft');
    }
  }));
};

// 指南针
var Compass = function Compass() {
  var _useProps3 = (0, _useProps4["default"])(),
    map = _useProps3.map;
  var _useState = (0, _react.useState)(0),
    _useState2 = _slicedToArray(_useState, 2),
    rotation = _useState2[0],
    setRotation = _useState2[1];

  // 监听角度变化
  (0, _react.useEffect)(function () {
    var move = function move() {
      setRotation(_cesium.Math.toDegrees(map.camera.heading).toFixed(2));
    };
    map.scene.postRender.addEventListener(move);
    return function () {
      map.scene.postRender.removeEventListener(move);
    };
  }, []);
  var domRef = (0, _react.useRef)(null);
  var onMouseDown = function onMouseDown(event) {
    event.preventDefault();
    var startX = event.clientX;
    var startY = event.clientY;
    var handleRotate = function handleRotate(e) {
      var curX = e.clientX;
      var curY = e.clientY;
      var rect = domRef.current.getBoundingClientRect();
      // 获取元素中心点位置
      var centerX = rect.left + rect.width / 2;
      var centerY = rect.top + rect.height / 2;

      // 旋转前的角度
      var rotateDegreeBefore = Math.atan2(startY - centerY, startX - centerX) / (Math.PI / 180);

      // 旋转后的角度
      var rotateDegreeAfter = Math.atan2(curY - centerY, curX - centerX) / (Math.PI / 180);

      // 获取旋转的角度值
      var nowRotate = rotateDegreeAfter - rotateDegreeBefore;
      var radians = _cesium.Math.toRadians(nowRotate);
      map.camera.twistRight(radians);
      startX = curX;
      startY = curY;
    };
    var handleMouseUp = function handleMouseUp() {
      document.removeEventListener('mousemove', handleRotate);
      document.removeEventListener('mouseup', handleMouseUp);
    };
    document.addEventListener('mousemove', handleRotate);
    document.addEventListener('mouseup', handleMouseUp);
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-compass"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    ref: domRef,
    className: (0, _classnames["default"])("".concat(prefix, "-compass-ring")),
    onMouseDown: onMouseDown,
    style: {
      transform: "rotate(".concat(rotation, "deg)")
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix, "-compass-ring-center"))
  })));
};
var Navigation = function Navigation(props) {
  var className = props.className,
    style = props.style;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefix), className),
    style: style
  }, /*#__PURE__*/_react["default"].createElement(Compass, null), /*#__PURE__*/_react["default"].createElement(Arrows, null), /*#__PURE__*/_react["default"].createElement(Zoom, null));
};
var _default = exports["default"] = Navigation;
//# sourceMappingURL=index.js.map
