"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 _propTypes = _interopRequireDefault(require("prop-types"));
var _useProps2 = _interopRequireDefault(require("../use-props"));
var _cesium = require("cesium");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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; }
var InundateAnalysis = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var data = props.data,
    setProgress = props.setProgress;
  var polygonRef = (0, _react.useRef)();
  var playRef = (0, _react.useRef)({});
  var layerRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      removePolygon: removePolygon,
      addPolygon: addPolygon,
      onFinish: onFinish,
      onReset: onReset,
      onProgress: onProgress,
      polygonRef: polygonRef,
      playRef: playRef,
      layerRef: layerRef
    };
  });
  (0, _react.useEffect)(function () {
    layerRef.current = new _cesium.CustomDataSource('measureLayer');
    layerRef.current.unclickable = true;
    layerRef.current.cancelBubble = true;
    layerRef.current.layerControl = false;
    map.dataSources.add(layerRef.current);
    return function () {
      map.dataSources.remove(layerRef.current);
    };
  }, []);
  var removePolygon = function removePolygon() {
    if (polygonRef.current) {
      layerRef.current.entities.remove(polygonRef.current);
      polygonRef.current = null;
    }
  };
  var addPolygon = function addPolygon(_ref) {
    var min = _ref.min,
      max = _ref.max,
      speed = _ref.speed,
      color = _ref.color;
    removePolygon();
    var _p = data.map(function (v) {
      return [].concat(_toConsumableArray(v), [10]);
    });
    var total = max - min;
    playRef.current = {
      color: color || "rgba(0,191,255,0.5)",
      // 水面颜色
      targetHeight: max,
      // 目标高度
      currentHeight: min,
      // 当前高度
      speed: speed,
      // 速度
      progress: 0,
      // 进度
      play: true
    };
    polygonRef.current = layerRef.current.entities.add({
      polygon: {
        hierarchy: _cesium.Cartesian3.fromDegreesArrayHeights(_p.flat(Infinity)),
        extrudedHeight: new _cesium.CallbackProperty(function () {
          var _currentHeight = playRef.current.currentHeight;
          if (playRef.current.play) {
            _currentHeight += speed / 60;
            var _progress = _currentHeight / total * 100;
            if (_currentHeight > playRef.current.targetHeight) {
              _currentHeight = 0;
              _progress = 0;
            }
            playRef.current.currentHeight = _currentHeight;
            setProgress === null || setProgress === void 0 || setProgress(_progress);
          }
          return _currentHeight;
        }, false),
        perPositionHeight: true,
        closeTop: false,
        material: new _cesium.Color.fromCssColorString(playRef.current.color)
      }
    });
  };
  var onFinish = function onFinish(values) {
    addPolygon(values);
  };
  var onReset = function onReset() {
    removePolygon();
  };
  var onProgress = function onProgress(_progress) {
    playRef.current.progress = _progress;
    playRef.current.currentHeight = _progress / 100 * playRef.current.targetHeight;
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null);
});
InundateAnalysis.propTypes = {
  data: _propTypes["default"].array,
  perspective: _propTypes["default"].string,
  model: _propTypes["default"].object
};
var _default = exports["default"] = InundateAnalysis;
//# sourceMappingURL=index.js.map
