"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = require("react");
var _cesium = require("../../_util/cesium");
var _cesium2 = require("cesium");
var _TilesetCut = _interopRequireDefault(require("../TilesetCut"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var DrawHandler = function DrawHandler(props) {
  var viewer = props.viewer,
    type = props.type,
    sceneId = props.sceneId;
  var viewerObj = (0, _react.useRef)();
  var drawLayer = (0, _react.useRef)(null);
  var listnerEvent = (0, _react.useRef)({
    drawStart: [],
    drawEnd: []
  });
  var cutToolRef = (0, _react.useRef)();
  if (viewer) viewerObj.current = viewer;

  // 初始化绘图控件
  var initDrawHandler = function initDrawHandler(viewer) {
    // 取消双击事件-追踪该位置
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(_cesium2.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    drawLayer.current = new _cesium2.CustomDataSource('measureLayer');
    viewer.dataSources.add(drawLayer.current);
  };
  var activate = function activate() {
    if (type) {
      draw(type);
    }
  };

  // 绘制多边形
  var drawPolygon = function drawPolygon(clampToGround) {
    if (!viewer) return;
    var handler = new _cesium2.ScreenSpaceEventHandler(viewer.scene.canvas);
    var positions = [];
    var polygon = new _cesium2.PolygonHierarchy();
    var polyObj = null;
    handler.setInputAction(function (ev) {
      var cartesian = (0, _cesium.getCartesian3FromXY)(viewer, ev.position);
      if (cartesian && cartesian.x) {
        if (positions.length === 0) {
          polygon.positions.push(cartesian.clone());
          positions.push(cartesian.clone());
        }
        positions.push(cartesian.clone());
        polygon.positions.push(cartesian.clone());
        if (!polyObj) {
          var _createPolygonEntity = createPolygonEntity(positions, polygon, clampToGround),
            entity = _createPolygonEntity.entity;
          polyObj = entity;
        }
      }
    }, _cesium2.ScreenSpaceEventType.LEFT_CLICK);
    handler.setInputAction(function (ev) {
      var catesian = (0, _cesium.getCartesian3FromXY)(viewer, ev.endPosition);
      if (positions.length >= 2) {
        if (catesian && catesian.x) {
          positions.pop();
          positions.push(catesian);
          polygon.positions.pop();
          polygon.positions.push(catesian);
        }
      }
    }, _cesium2.ScreenSpaceEventType.MOUSE_MOVE);
    handler.setInputAction(function () {
      handler.destroy();
      positions.push(positions[0]);
      // 执行绘制生命周期函数
      if (listnerEvent.current.drawEnd.length) {
        listnerEvent.current.drawEnd.forEach(function (fuc) {
          fuc(positions);
        });
      }
    }, _cesium2.ScreenSpaceEventType.RIGHT_CLICK);
  };

  // 创建多边形
  var createPolygonEntity = function createPolygonEntity(positions, polygon, clampToGround) {
    var _drawLayer$current;
    var polygonEntity = new _cesium2.Entity({
      polyline: {
        positions: new _cesium2.CallbackProperty(function () {
          return positions;
        }, false),
        width: 3,
        material: new _cesium2.Color(0, 255, 255, 0.5),
        clampToGround: clampToGround
      },
      polygon: {
        hierarchy: new _cesium2.CallbackProperty(function () {
          return polygon;
        }, false),
        material: _cesium2.Color.WHITE.withAlpha(0.1),
        heightReference: _cesium2.HeightReference.CLAMP_TO_GROUND
      }
    });
    var entity = (_drawLayer$current = drawLayer.current) === null || _drawLayer$current === void 0 ? void 0 : _drawLayer$current.entities.add(polygonEntity);
    return {
      polygonEntity: polygonEntity,
      entity: entity
    };
  };
  var draw = function draw(type) {
    switch (type) {
      case 'polygon':
        drawPolygon(true);
        break;
    }
  };
  var on = function on(type, callBack) {
    listnerEvent.current[type].push(callBack);
  };
  var clear = function clear() {
    if (drawLayer.current) {
      drawLayer.current.entities.removeAll();
    }
  };
  (0, _react.useEffect)(function () {
    if (viewer && type) {
      initDrawHandler(viewer, type);
    } else {
      console.warn('请使用 initDrawHandler 函数初始化绘图控件');
    }
    return function () {
      clear();
    };
  }, []);
  return {
    activate: activate,
    on: on,
    clear: clear
  };
};
var _default = exports["default"] = DrawHandler;
//# sourceMappingURL=useDrawHandler.js.map
