/**
 * vue-cesium - https://github.com/zouyaoji/vue-cesium
 * Vue 2.x components for CesiumJS.
 *
 * @package VueCesium
 * @author zouyaoji <370681295@qq.com>
 * @version 2.2.12
 * @license MIT
 * @homepage https://zouyaoji.top/vue-cesium
 * @copyright (c) 2018-2021, zouyaoji <370681295@qq.com>
 */
import _Object$defineProperties from '@babel/runtime-corejs2/core-js/object/define-properties';
import _regeneratorRuntime from '@babel/runtime-corejs2/regenerator';
import cmp from '../virtualCmp';
import mergeDescriptors from '../../utils/mergeDescriptors';
import bindEvents from '../../utils/bindEvent';
import mixinPickEvent from '../event/mixinPickEvent';

var methods = {
  mount: function mount() {
    var _this = this;

    var primitives, primitive, registerEvents;
    return _regeneratorRuntime.async(function mount$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            primitives = this.primitives, primitive = this.primitive, registerEvents = this.registerEvents;
            primitive.readyPromise && primitive.readyPromise.then(function (primitive) {
              var listener = _this.$listeners.readyPromise;
              listener && _this.$emit('readyPromise', primitive);
            }).otherwise(function (error) {
              throw new Cesium.DeveloperError(error);
            });
            bindEvents.call(this, primitive, undefined, true);
            registerEvents(true);
            primitive._vcParent = primitives;
            return _context.abrupt("return", primitives && primitives.add(primitive));

          case 6:
          case "end":
            return _context.stop();
        }
      }
    }, null, this);
  },
  unmount: function unmount() {
    var primitives, primitive, registerEvents;
    return _regeneratorRuntime.async(function unmount$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            primitives = this.primitives, primitive = this.primitive, registerEvents = this.registerEvents;
            this.childCount = 0;
            this.instances = [];
            bindEvents.call(this, primitive, undefined, false);
            registerEvents(false);
            return _context2.abrupt("return", primitives && primitives.remove(primitive));

          case 6:
          case "end":
            return _context2.stop();
        }
      }
    }, null, this);
  },
  setGeometryInstances: function setGeometryInstances(geometryInstance, index) {
    var listener;
    return _regeneratorRuntime.async(function setGeometryInstances$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            this.instances.push(geometryInstance);

            if (index === this.childCount - 1) {
              listener = this.$listeners['update:geometryInstances'];

              if (listener) {
                this.$emit('update:geometryInstances', this.instances);
              } else {
                this.primitive.geometryInstances = index === 0 ? geometryInstance : this.instances;
              }
            }

            return _context3.abrupt("return", true);

          case 3:
          case "end":
            return _context3.stop();
        }
      }
    }, null, this);
  },
  getServices: function getServices() {
    var vm = this;
    return mergeDescriptors(cmp.methods.getServices.call(this), {
      get primitive() {
        return vm.primitive;
      },

      get primitiveContainer() {
        return vm;
      }

    });
  }
};
var mixinPrimitive = {
  data: function data() {
    return {
      childCount: 0,
      instances: []
    };
  },
  props: {
    enableEvent: {
      type: Boolean,
      default: true
    }
  },
  mixins: [cmp, mixinPickEvent],
  methods: methods,
  stubVNode: {
    attrs: function attrs() {
      return {
        class: this.$options.name
      };
    }
  },
  created: function created() {
    var _this2 = this;

    _Object$defineProperties(this, {
      primitive: {
        enumerable: true,
        get: function get() {
          return _this2.cesiumObject;
        }
      },
      primitives: {
        enumerable: true,
        get: function get() {
          return _this2.$services && _this2.$services.primitives;
        }
      },
      groundPrimitives: {
        enumerable: true,
        get: function get() {
          return _this2.$services && _this2.$services.groundPrimitives;
        }
      }
    });
  }
};

export default mixinPrimitive;
