/**
 * vue-cesium - https://github.com/zouyaoji/vue-cesium
 * Vue 2.x components for CesiumJS.
 *
 * @package VueCesium
 * @author zouyaoji <370681295@qq.com>
 * @version 2.3.12
 * @license MIT
 * @homepage https://zouyaoji.top/vue-cesium
 * @copyright (c) 2018-2022, zouyaoji <370681295@qq.com>
 */
import _asyncToGenerator from '@babel/runtime-corejs2/helpers/esm/asyncToGenerator';
import _classCallCheck from '@babel/runtime-corejs2/helpers/esm/classCallCheck';
import _createClass from '@babel/runtime-corejs2/helpers/esm/createClass';
import _regeneratorRuntime from '@babel/runtime-corejs2/regenerator';

/*
 * @Author: zouyaoji@https://github.com/zouyaoji
 * @Date: 2021-11-19 14:20:47
 * @LastEditTime: 2022-05-06 10:38:41
 * @LastEditors: zouyaoji
 * @Description:
 * @FilePath: \vue-cesium-v2\src\exts\PolygonPrimitive.js
 */
var PolygonPrimitive = /*#__PURE__*/function () {
  function PolygonPrimitive(options) {
    _classCallCheck(this, PolygonPrimitive);

    var _Cesium = Cesium,
        defined = _Cesium.defined,
        defaultValue = _Cesium.defaultValue,
        createGuid = _Cesium.createGuid,
        BoundingSphere = _Cesium.BoundingSphere,
        Ellipsoid = _Cesium.Ellipsoid,
        ClassificationType = _Cesium.ClassificationType;
    options = defaultValue(options, {});
    this.show = defaultValue(options.show, true);
    this._id = defined(options.id) ? options.id : createGuid();
    this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
    this._appearance = defaultValue(options.appearance, new Cesium.MaterialAppearance());
    this._depthFailAppearance = options.depthFailAppearance;
    this._positions = defaultValue(options.positions, []);
    this._polygonHierarchy = options.polygonHierarchy;
    this._clampToGround = defaultValue(options.clampToGround, false);
    this._classificationType = defaultValue(options.classificationType, ClassificationType.BOTH);
    this._allowPicking = defaultValue(options.allowPicking, true);
    this._asynchronous = defaultValue(options.asynchronous, false);
    this._boundingSphere = new BoundingSphere();
    this._primitive = undefined;
    this._update = true;
  }

  _createClass(PolygonPrimitive, [{
    key: "positions",
    get: function get() {
      return this._positions;
    },
    set: function set(val) {
      this._positions = val;
      this._update = true;
    }
  }, {
    key: "polygonHierarchy",
    get: function get() {
      return this._polygonHierarchy;
    },
    set: function set(val) {
      this._polygonHierarchy = val;
      this._update = true;
    }
  }, {
    key: "appearance",
    get: function get() {
      return this._appearance;
    },
    set: function set(val) {
      this._appearance = val;

      if (this._primitive !== undefined) {
        this._primitive.appearance = val;
      }
    }
  }, {
    key: "depthFailAppearance",
    get: function get() {
      return this._depthFailAppearance;
    },
    set: function set(val) {
      this._depthFailAppearance = val;

      if (this._primitive !== undefined && this._primitive instanceof Cesium.Primitive) {
        this._primitive.depthFailAppearance = val;
      }
    }
  }, {
    key: "id",
    get: function get() {
      return this._id;
    },
    set: function set(id) {
      this._id = id;
    }
  }, {
    key: "boundingVolume",
    get: function get() {
      return this._boundingSphere;
    }
  }, {
    key: "ellipsoid",
    get: function get() {
      return this._ellipsoid;
    }
  }, {
    key: "clampToGround",
    get: function get() {
      return this._clampToGround;
    },
    set: function set(val) {
      this._clampToGround = val;
    }
  }, {
    key: "classificationType",
    get: function get() {
      return this._classificationType;
    },
    set: function set(e) {
      this._classificationType = e;
      this._update = true;
    }
  }, {
    key: "allowPicking",
    get: function get() {
      return this._allowPicking;
    },
    set: function set(val) {
      this._allowPicking = val;
    }
  }, {
    key: "asynchronous",
    get: function get() {
      return this._asynchronous;
    },
    set: function set(val) {
      this._asynchronous = val;
    }
  }, {
    key: "update",
    value: function () {
      var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(frameState) {
        var _this = this;

        var positions, promise;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (this.show) {
                  positions = this._polygonHierarchy ? this._polygonHierarchy.positions : this._positions;

                  if (positions.length < 3) {
                    this._primitive && this._primitive.destroy();
                    this._primitive = undefined;
                  } else {
                    if (this._update) {
                      this._update = false;

                      if (this._clampToGround) {
                        promise = this._createGroundPolygon();
                      } else {
                        promise = this._createPolygon();
                      }

                      promise.then(function (primitive) {
                        _this._primitive && _this._primitive.destroy();
                        _this._primitive = undefined;
                        _this._primitive = primitive;
                        _this._primitive._vcParent = _this;
                        _this._boundingSphere = Cesium.BoundingSphere.fromPoints(positions, _this._boundingSphere);
                      });
                    }

                    this._primitive && this._primitive.update(frameState);
                  }
                }

              case 1:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function update(_x) {
        return _update.apply(this, arguments);
      }

      return update;
    }()
  }, {
    key: "_createPolygon",
    value: function () {
      var _createPolygon2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var _Cesium2, Primitive, GeometryInstance, CoplanarPolygonGeometry, Cartesian3;

        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _Cesium2 = Cesium, Primitive = _Cesium2.Primitive, GeometryInstance = _Cesium2.GeometryInstance, CoplanarPolygonGeometry = _Cesium2.CoplanarPolygonGeometry, Cartesian3 = _Cesium2.Cartesian3;
                return _context2.abrupt("return", new Primitive({
                  geometryInstances: new GeometryInstance({
                    geometry: this._polygonHierarchy ? new CoplanarPolygonGeometry({
                      polygonHierarchy: this._polygonHierarchy,
                      ellipsoid: this._ellipsoid
                    }) : CoplanarPolygonGeometry.fromPositions({
                      positions: this._positions.map(function (e) {
                        return Cartesian3.clone(e);
                      }),
                      ellipsoid: this._ellipsoid
                    }),
                    id: this._id
                  }),
                  appearance: this._appearance,
                  depthFailAppearance: this._depthFailAppearance,
                  allowPicking: this._allowPicking,
                  asynchronous: this._asynchronous
                }));

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function _createPolygon() {
        return _createPolygon2.apply(this, arguments);
      }

      return _createPolygon;
    }()
  }, {
    key: "_createGroundPolygon",
    value: function () {
      var _createGroundPolygon2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        var _Cesium3, GroundPrimitive, GeometryInstance, PolygonGeometry, Cartesian3;

        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _Cesium3 = Cesium, GroundPrimitive = _Cesium3.GroundPrimitive, GeometryInstance = _Cesium3.GeometryInstance, PolygonGeometry = _Cesium3.PolygonGeometry, Cartesian3 = _Cesium3.Cartesian3;
                _context3.next = 3;
                return Cesium.GroundPrimitive.initializeTerrainHeights();

              case 3:
                return _context3.abrupt("return", new GroundPrimitive({
                  geometryInstances: new GeometryInstance({
                    geometry: this._polygonHierarchy ? new PolygonGeometry({
                      polygonHierarchy: this._polygonHierarchy,
                      ellipsoid: this._ellipsoid
                    }) : PolygonGeometry.fromPositions({
                      positions: this._positions.map(function (e) {
                        return Cartesian3.clone(e);
                      }),
                      ellipsoid: this._ellipsoid
                    }),
                    id: this._id
                  }),
                  appearance: this._appearance,
                  allowPicking: this._allowPicking,
                  asynchronous: this._asynchronous,
                  classificationType: this._classificationType
                }));

              case 4:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function _createGroundPolygon() {
        return _createGroundPolygon2.apply(this, arguments);
      }

      return _createGroundPolygon;
    }()
  }, {
    key: "isDestroyed",
    value: function isDestroyed() {
      return false;
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this._primitive && this._primitive.destroy();
      this._primitive = undefined;
      return Cesium.destroyObject(this);
    }
  }]);

  return PolygonPrimitive;
}();

export default PolygonPrimitive;
