/**
 * 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 _regeneratorRuntime from '@babel/runtime-corejs2/regenerator';
import _JSON$stringify from '@babel/runtime-corejs2/core-js/json/stringify';
import _Object$keys from '@babel/runtime-corejs2/core-js/object/keys';
import { show } from '../mixins/mixinProps';
import mixinPrimitiveCollection from '../mixins/primitives/mixinPrimitiveCollection';
import PolygonPrimitive from '../exts/PolygonPrimitive';
import { addCustomProperty } from '../utils/util';
import { differenceBy } from 'lodash-es';

var script = {
  name: 'vc-collection-primitive',
  mixins: [show, mixinPrimitiveCollection],
  props: {
    destroyPrimitives: {
      type: Boolean,
      default: true
    },
    polygons: {
      type: Array,
      default: function _default() {
        return [];
      }
    }
  },
  watch: {
    polygons: {
      handler: function handler(newVal, oldVal) {
        var _this = this;

        if (!this.mounted) {
          return;
        }

        var transformProp = this.transformProp,
            primitiveCollection = this.collection;

        if (newVal.length === oldVal.length) {
          // 视为修改操作
          // Treated as modified
          var modifies = [];

          for (var i = 0; i < newVal.length; i++) {
            var options = newVal[i];
            var oldOptions = oldVal[i];

            if (_JSON$stringify(options) !== _JSON$stringify(oldOptions)) {
              modifies.push({
                newOptions: options,
                oldOptions: oldOptions
              });
            }
          }

          modifies.forEach(function (modify) {
            var modifyPolygon = primitiveCollection._primitives.find(function (v) {
              return v._id === modify.oldOptions.id;
            });

            modifyPolygon && _Object$keys(modify.newOptions).forEach(function (prop) {
              if (modify.oldOptions[prop] !== modify.newOptions[prop]) {
                modifyPolygon[prop] = transformProp(prop, modify.newOptions[prop]);
              }
            });
          });
        } else {
          (function () {
            var addeds = differenceBy(newVal, oldVal, 'id');
            var deletes = differenceBy(oldVal, newVal, 'id');
            var deletePolygons = [];

            var _loop = function _loop(_i) {
              var deletePolygon = primitiveCollection._primitives.find(function (v) {
                return v.id === deletes[_i].id;
              });

              deletePolygon && deletePolygons.push(deletePolygon);
            };

            for (var _i = 0; _i < deletes.length; _i++) {
              _loop(_i);
            }

            deletePolygons.forEach(function (v) {
              primitiveCollection.remove(v);
            });

            _this.addPolygons(primitiveCollection, addeds);
          })();
        }
      },
      deep: true
    }
  },
  methods: {
    createCesiumObject: function createCesiumObject() {
      var _this2 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var $props, transformProps, options, primitiveCollection;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                $props = _this2.$props, transformProps = _this2.transformProps;
                options = transformProps($props);
                primitiveCollection = new Cesium.PrimitiveCollection(options);

                _this2.addPolygons(primitiveCollection, _this2.polygons);

                return _context.abrupt("return", primitiveCollection);

              case 5:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    addPolygons: function addPolygons(primitiveCollection, polygons) {
      var transformProps = this.transformProps;

      for (var i = 0; i < polygons.length; i++) {
        var polygonOptions = polygons[i];
        polygonOptions.id = Cesium.defined(polygonOptions.id) ? polygonOptions.id : Cesium.createGuid();
        var polygonOptionsTransform = transformProps(polygonOptions);
        var polygonPrimitive = new PolygonPrimitive(polygonOptionsTransform);
        polygonPrimitive._vcParent = primitiveCollection;
        addCustomProperty(polygonPrimitive, polygonOptionsTransform);
        primitiveCollection.add(polygonPrimitive);
      }
    }
  }
};

/* script */
var __vue_script__ = script;
/* template */

/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* functional template */

var __vue_is_functional_template__ = undefined;
/* component normalizer */

function __vue_normalize__(template, style, script, scope, functional, moduleIdentifier, createInjector, createInjectorSSR) {
  var component = (typeof script === 'function' ? script.options : script) || {}; // For security concerns, we use only base name in production mode.

  component.__file = "PrimitiveCollection.vue";

  if (!component.render) {
    component.render = template.render;
    component.staticRenderFns = template.staticRenderFns;
    component._compiled = true;
    if (functional) component.functional = true;
  }

  component._scopeId = scope;

  return component;
}
/* style inject */

/* style inject SSR */


var PrimitiveCollection = __vue_normalize__({}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__);

function plugin(Vue) {

  if (plugin.installed) {
    return;
  }

  plugin.installed = true;
  Vue.component(PrimitiveCollection.name, PrimitiveCollection);
}

export default plugin;
export { PrimitiveCollection, plugin as install };
