'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../composables/index.js');
var cloneDeep = require('lodash/cloneDeep');
var differenceBy = require('lodash/differenceBy');
var cesiumProps = require('../../../utils/cesium-props.js');
var util = require('../../../utils/util.js');
var render = require('../../../utils/private/render.js');
var index = require('../../../composables/use-primitive-collections/index.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);
var differenceBy__default = /*#__PURE__*/_interopDefaultLegacy(differenceBy);

var CollectionBillboard = vue.defineComponent({
  name: "VcCollectionBillboard",
  props: {
    ...cesiumProps.scene,
    ...cesiumProps.blendOption,
    ...cesiumProps.show,
    ...cesiumProps.enableMouseEvent,
    billboards: {
      type: Array,
      default: () => []
    }
  },
  emits: ["beforeLoad", "ready", "destroyed"],
  setup(props, ctx) {
    const instance = vue.getCurrentInstance();
    instance.cesiumClass = "BillboardCollection";
    const primitiveCollectionsState = index["default"](props, ctx, instance);
    let unwatchFns = [];
    unwatchFns.push(vue.watch(() => cloneDeep__default["default"](props.billboards), (newVal, oldVal) => {
      if (!instance.mounted) {
        return;
      }
      const billboardCollection = instance.cesiumObject;
      if (newVal.length === oldVal.length) {
        const modifies = [];
        for (let i = 0; i < newVal.length; i++) {
          const options = newVal[i];
          const oldOptions = oldVal[i];
          if (JSON.stringify(options) !== JSON.stringify(oldOptions)) {
            modifies.push({
              newOptions: options,
              oldOptions
            });
          }
        }
        modifies.forEach((modify) => {
          const modifyBillboard = billboardCollection._billboards.find((v) => v.id === modify.oldOptions.id);
          modifyBillboard && Object.keys(modify.newOptions).forEach((prop) => {
            if (modify.oldOptions[prop] !== modify.newOptions[prop]) {
              modifyBillboard[prop] = primitiveCollectionsState == null ? void 0 : primitiveCollectionsState.transformProp(prop, modify.newOptions[prop]);
            }
          });
        });
      } else {
        const addeds = differenceBy__default["default"](newVal, oldVal, "id");
        const deletes = differenceBy__default["default"](oldVal, newVal, "id");
        const deleteBillboards = [];
        for (let i = 0; i < deletes.length; i++) {
          const deleteBillboard = billboardCollection._billboards.find((v) => v.id === deletes[i].id);
          deleteBillboard && deleteBillboards.push(deleteBillboard);
        }
        deleteBillboards.forEach((v) => {
          billboardCollection.remove(v);
        });
        addBillboards(billboardCollection, addeds);
      }
    }, {
      deep: true
    }));
    instance.alreadyListening.push("billboards");
    const addBillboards = (billboardCollection, billboards) => {
      for (let i = 0; i < billboards.length; i++) {
        const billboardOptions = billboards[i];
        billboardOptions.id = Cesium.defined(billboardOptions.id) ? billboardOptions.id : Cesium.createGuid();
        const billboardOptionsTransform = primitiveCollectionsState == null ? void 0 : primitiveCollectionsState.transformProps(billboardOptions);
        const billboard = billboardCollection.add(billboardOptionsTransform);
        util.addCustomProperty(billboard, billboardOptionsTransform);
      }
    };
    instance.createCesiumObject = async () => {
      const options = primitiveCollectionsState == null ? void 0 : primitiveCollectionsState.transformProps(props);
      const billboardCollection = new Cesium.BillboardCollection(options);
      addBillboards(billboardCollection, props.billboards);
      return billboardCollection;
    };
    vue.onUnmounted(() => {
      unwatchFns.forEach((item) => item());
      unwatchFns = [];
    });
    return () => {
      var _a, _b;
      return ctx.slots.default ? vue.h("i", {
        class: util.kebabCase(((_a = instance.proxy) == null ? void 0 : _a.$options.name) || ""),
        style: { display: "none !important" }
      }, render.hSlot(ctx.slots.default)) : vue.createCommentVNode(util.kebabCase(((_b = instance.proxy) == null ? void 0 : _b.$options.name) || ""));
    };
  }
});

exports["default"] = CollectionBillboard;
//# sourceMappingURL=index.js.map
