'use strict';

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

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

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);

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

function __awaiter(thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
}

var VcCollectionBillboard = vue.defineComponent({
    name: 'VcCollectionBillboard',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, 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 = composables.usePrimitiveCollections(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: 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.transformProp(prop, modify.newOptions[prop]);
                        }
                    });
                });
            }
            else {
                const adds = 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);
                });
                for (let i = 0; i < adds.length; i++) {
                    const billboardOptions = newVal[i];
                    billboardOptions.id = Cesium.defined(billboardOptions.id) ? billboardOptions.id : Cesium.createGuid();
                    const billboardOptionsTransform = primitiveCollectionsState.transformProps(billboardOptions);
                    const billboard = billboardCollection.add(billboardOptionsTransform);
                    primitiveCollectionsState.addCustomProp(billboard, billboardOptionsTransform);
                }
            }
        }, {
            deep: true
        }));
        instance.alreadyListening.push('billboards');
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = primitiveCollectionsState.transformProps(props);
            const billboardCollection = new Cesium.BillboardCollection(options);
            for (let i = 0; i < props.billboards.length; i++) {
                const billboardOptions = props.billboards[i];
                billboardOptions.id = Cesium.defined(billboardOptions.id) ? billboardOptions.id : Cesium.createGuid();
                const billboardOptionsTransform = primitiveCollectionsState.transformProps(billboardOptions);
                const billboard = billboardCollection.add(billboardOptionsTransform);
                primitiveCollectionsState.addCustomProp(billboard, billboardOptionsTransform);
            }
            return billboardCollection;
        });
        vue.onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcBillboard = vue.defineComponent({
    name: 'VcBillboard',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.alignedAxis), cesiumProps.color), cesiumProps.disableDepthTestDistance), cesiumProps.distanceDisplayCondition), cesiumProps.eyeOffset), cesiumProps.height), cesiumProps.heightReference), cesiumProps.horizontalOrigin), cesiumProps.id), cesiumProps.image), cesiumProps.pixelOffset), cesiumProps.pixelOffsetScaleByDistance), cesiumProps.position), cesiumProps.rotation), cesiumProps.scale), cesiumProps.scaleByDistance), cesiumProps.show), cesiumProps.sizeInMeters), cesiumProps.translucencyByDistance), cesiumProps.verticalOrigin), cesiumProps.width), cesiumProps.enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'Billboard';
        composables.usePrimitiveCollectionItems(props, ctx, instance);
        return () => vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcCollectionLabel = vue.defineComponent({
    name: 'VcCollectionLabel',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.modelMatrix), cesiumProps.debugShowBoundingVolume), cesiumProps.scene), cesiumProps.blendOption), cesiumProps.show), cesiumProps.enableMouseEvent), { labels: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'LabelCollection';
        const primitiveCollectionsState = composables.usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('labels');
        let unwatchFns = [];
        unwatchFns.push(vue.watch(() => cloneDeep__default['default'](props.labels), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const labelCollection = 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: oldOptions
                        });
                    }
                }
                modifies.forEach(modify => {
                    const modifyLabel = labelCollection._labels.find(v => v.id === modify.oldOptions.id);
                    modifyLabel && Object.keys(modify.newOptions).forEach(prop => {
                        if (modify.oldOptions[prop] !== modify.newOptions[prop]) {
                            modifyLabel[prop] = primitiveCollectionsState.transformProp(prop, modify.newOptions[prop]);
                        }
                    });
                });
            }
            else {
                const adds = differenceBy__default['default'](newVal, oldVal, 'id');
                const deletes = differenceBy__default['default'](oldVal, newVal, 'id');
                const deleteLabels = [];
                for (let i = 0; i < deletes.length; i++) {
                    const deleteLabel = labelCollection._labels.find(v => v.id === deletes[i].id);
                    deleteLabel && deleteLabels.push(deleteLabel);
                }
                deleteLabels.forEach(v => {
                    labelCollection.remove(v);
                });
                for (let i = 0; i < adds.length; i++) {
                    const labelOptions = newVal[i];
                    labelOptions.id = Cesium.defined(labelOptions.id) ? labelOptions.id : Cesium.createGuid();
                    const labelOptionsTransform = primitiveCollectionsState.transformProps(labelOptions);
                    const label = labelCollection.add(labelOptionsTransform);
                    primitiveCollectionsState.addCustomProp(label, labelOptionsTransform);
                }
            }
        }, {
            deep: true
        }));
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = primitiveCollectionsState.transformProps(props);
            const labelCollection = new Cesium.LabelCollection(options);
            for (let i = 0; i < props.labels.length; i++) {
                const labelOptions = props.labels[i];
                labelOptions.id = Cesium.defined(labelOptions.id) ? labelOptions.id : Cesium.createGuid();
                const labelOptionsTransform = primitiveCollectionsState.transformProps(labelOptions);
                const label = labelCollection.add(labelOptionsTransform);
                primitiveCollectionsState.addCustomProp(label, labelOptionsTransform);
            }
            return labelCollection;
        });
        vue.onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcLabel = vue.defineComponent({
    name: 'VcLabel',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.backgroundColor), cesiumProps.backgroundPadding), cesiumProps.disableDepthTestDistance), cesiumProps.distanceDisplayCondition), cesiumProps.eyeOffset), cesiumProps.fillColor), cesiumProps.font), cesiumProps.heightReference), cesiumProps.horizontalOrigin), cesiumProps.id), cesiumProps.outlineColor), cesiumProps.outlineWidth), cesiumProps.pixelOffset), cesiumProps.pixelOffsetScaleByDistance), cesiumProps.position), cesiumProps.scale), cesiumProps.scaleByDistance), cesiumProps.show), cesiumProps.showBackground), cesiumProps.labelStyle), cesiumProps.text), { totalScale: {
            type: Number,
            default: 1.0
        } }), cesiumProps.translucencyByDistance), cesiumProps.verticalOrigin), cesiumProps.enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'Label';
        composables.usePrimitiveCollectionItems(props, ctx, instance);
        return () => vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcCollectionPoint = vue.defineComponent({
    name: 'VcCollectionPoint',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.modelMatrix), cesiumProps.debugShowBoundingVolume), cesiumProps.blendOption), cesiumProps.show), cesiumProps.enableMouseEvent), { points: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'PointPrimitiveCollection';
        const primitiveCollectionsState = composables.usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('points');
        let unwatchFns = [];
        unwatchFns.push(vue.watch(() => cloneDeep__default['default'](props.points), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const pointCollection = 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: oldOptions
                        });
                    }
                }
                modifies.forEach(modify => {
                    const modifyPoint = pointCollection._pointPrimitives.find(v => v && v.id === modify.oldOptions.id);
                    modifyPoint && Object.keys(modify.newOptions).forEach(prop => {
                        if (modify.oldOptions[prop] !== modify.newOptions[prop]) {
                            modifyPoint[prop] = primitiveCollectionsState.transformProp(prop, modify.newOptions[prop]);
                        }
                    });
                });
            }
            else {
                const adds = differenceBy__default['default'](newVal, oldVal, 'id');
                const deletes = differenceBy__default['default'](oldVal, newVal, 'id');
                const deletePoints = [];
                for (let i = 0; i < deletes.length; i++) {
                    const deletePoint = pointCollection._pointPrimitives.find(v => v.id === deletes[i].id);
                    deletePoint && deletePoints.push(deletePoint);
                }
                deletePoints.forEach(v => {
                    pointCollection.remove(v);
                });
                for (let i = 0; i < adds.length; i++) {
                    const pointOptions = newVal[i];
                    pointOptions.id = Cesium.defined(pointOptions.id) ? pointOptions.id : Cesium.createGuid();
                    const pointOptionsTransform = primitiveCollectionsState.transformProps(pointOptions);
                    const point = pointCollection.add(pointOptionsTransform);
                    primitiveCollectionsState.addCustomProp(point, pointOptionsTransform);
                }
            }
        }, {
            deep: true
        }));
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = primitiveCollectionsState.transformProps(props);
            const pointCollection = new Cesium.PointPrimitiveCollection(options);
            for (let i = 0; i < props.points.length; i++) {
                const pointOptions = props.points[i];
                pointOptions.id = Cesium.defined(pointOptions.id) ? pointOptions.id : Cesium.createGuid();
                const pointOptionsTransform = primitiveCollectionsState.transformProps(pointOptions);
                const point = pointCollection.add(pointOptionsTransform);
                primitiveCollectionsState.addCustomProp(point, pointOptionsTransform);
            }
            return pointCollection;
        });
        vue.onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcPoint = vue.defineComponent({
    name: 'VcPoint',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.color), cesiumProps.disableDepthTestDistance), cesiumProps.distanceDisplayCondition), cesiumProps.id), cesiumProps.outlineColor), cesiumProps.outlineWidth), cesiumProps.pixelSize), cesiumProps.position), cesiumProps.scaleByDistance), cesiumProps.show), cesiumProps.translucencyByDistance), cesiumProps.enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'PointPrimitive';
        composables.usePrimitiveCollectionItems(props, ctx, instance);
        return () => vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcCollectionPolyline = vue.defineComponent({
    name: 'VcCollectionPolyline',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.modelMatrix), cesiumProps.debugShowBoundingVolume), cesiumProps.show), cesiumProps.enableMouseEvent), { polylines: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'PolylineCollection';
        const primitiveCollectionsState = composables.usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('polylines');
        let unwatchFns = [];
        unwatchFns.push(vue.watch(() => cloneDeep__default['default'](props.polylines), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const polylineCollection = 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: oldOptions
                        });
                    }
                }
                modifies.forEach(modify => {
                    const modifyPolyline = polylineCollection._polylines.find(v => v.id === modify.oldOptions.id);
                    modifyPolyline && Object.keys(modify.newOptions).forEach(prop => {
                        if (modify.oldOptions[prop] !== modify.newOptions[prop]) {
                            modifyPolyline[prop] = primitiveCollectionsState.transformProp(prop, modify.newOptions[prop]);
                        }
                    });
                });
            }
            else {
                const adds = differenceBy__default['default'](newVal, oldVal, 'id');
                const deletes = differenceBy__default['default'](oldVal, newVal, 'id');
                const deletePolylines = [];
                for (let i = 0; i < deletes.length; i++) {
                    const deletePolyline = polylineCollection._polylines.find(v => v.id === deletes[i].id);
                    deletePolyline && deletePolylines.push(deletePolyline);
                }
                deletePolylines.forEach(v => {
                    polylineCollection.remove(v);
                });
                for (let i = 0; i < adds.length; i++) {
                    const polylineOptions = newVal[i];
                    polylineOptions.id = Cesium.defined(polylineOptions.id) ? polylineOptions.id : Cesium.createGuid();
                    const polylineOptionsTransform = primitiveCollectionsState.transformProps(polylineOptions);
                    const polyline = polylineCollection.add(polylineOptionsTransform);
                    primitiveCollectionsState.addCustomProp(polyline, polylineOptionsTransform);
                }
            }
        }, {
            deep: true
        }));
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = primitiveCollectionsState.transformProps(props);
            const polylineCollection = new Cesium.PolylineCollection(options);
            for (let i = 0; i < props.polylines.length; i++) {
                const polylineOptions = props.polylines[i];
                polylineOptions.id = Cesium.defined(polylineOptions.id) ? polylineOptions.id : Cesium.createGuid();
                const polylineOptionsTransform = primitiveCollectionsState.transformProps(polylineOptions);
                const polyline = polylineCollection.add(polylineOptionsTransform);
                primitiveCollectionsState.addCustomProp(polyline, polylineOptionsTransform);
            }
            return polylineCollection;
        });
        vue.onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcPolyline = vue.defineComponent({
    name: 'VcPolyline',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, cesiumProps.distanceDisplayCondition), cesiumProps.id), cesiumProps.loop), cesiumProps.material), cesiumProps.positions), cesiumProps.show), cesiumProps.width), cesiumProps.enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'Polyline';
        composables.usePrimitiveCollectionItems(props, ctx, instance);
        return () => vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

var VcCollectionPrimitive = vue.defineComponent({
    name: 'VcCollectionPrimitive',
    props: Object.assign(Object.assign(Object.assign({}, cesiumProps.show), { destroyPrimitives: {
            type: Boolean,
            default: true
        } }), cesiumProps.enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.cesiumClass = 'PrimitiveCollection';
        composables.usePrimitiveCollections(props, ctx, instance);
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

const components = [
    VcCollectionBillboard,
    VcCollectionLabel,
    VcCollectionPoint,
    VcCollectionPolyline,
    VcCollectionPrimitive,
    VcBillboard,
    VcLabel,
    VcPoint,
    VcPolyline
];
const install = (app) => {
    components.forEach(cmp => {
        app.component(cmp.name, cmp);
    });
};
var index = {
    install
};

exports.VcBillboard = VcBillboard;
exports.VcCollectionBillboard = VcCollectionBillboard;
exports.VcCollectionLabel = VcCollectionLabel;
exports.VcCollectionPoint = VcCollectionPoint;
exports.VcCollectionPolyline = VcCollectionPolyline;
exports.VcCollectionPrimitive = VcCollectionPrimitive;
exports.VcLabel = VcLabel;
exports.VcPoint = VcPoint;
exports.VcPolyline = VcPolyline;
exports.default = index;
