'use strict';

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

var vue = require('vue');
var composables = require('../composables');
var differenceWith = require('lodash/differenceWith');
var cloneDeep = require('lodash/cloneDeep');
var isEqual = require('lodash/isEqual');
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 differenceWith__default = /*#__PURE__*/_interopDefaultLegacy(differenceWith);
var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);

/*! *****************************************************************************
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({}, cesiumProps.scene), cesiumProps.blendOption), 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;
            const adds = differenceWith__default['default'](newVal, oldVal, isEqual__default['default']);
            const deletes = differenceWith__default['default'](oldVal, newVal, isEqual__default['default']);
            if (newVal.length === oldVal.length && adds.length === deletes.length) {
                for (let i = 0; i < adds.length; i++) {
                    const options = adds[i];
                    const modifyBillboard = billboardCollection._billboards.find(v => v.id === deletes[i].id);
                    modifyBillboard && Object.keys(options).forEach(prop => {
                        modifyBillboard[prop] = primitiveCollectionsState.transformProp(prop, options[prop]);
                    });
                }
            }
            else {
                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];
                    const billboardOptionsTransform = primitiveCollectionsState.transformProps(billboardOptions);
                    const billboardAdded = billboardCollection.add(billboardOptionsTransform);
                    billboardAdded.id !== billboardOptions.id && (billboardOptions.id = billboardAdded.id);
                }
            }
        }, {
            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];
                const billboardOptionsTransform = primitiveCollectionsState.transformProps(billboardOptions);
                const billboard = billboardCollection.add(billboardOptionsTransform);
                billboardOptions.id !== billboard.id && (billboardOptions.id = billboard.id);
            }
            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;
            const adds = differenceWith__default['default'](newVal, oldVal, isEqual__default['default']);
            const deletes = differenceWith__default['default'](oldVal, newVal, isEqual__default['default']);
            if (newVal.length === oldVal.length && adds.length === deletes.length) {
                for (let i = 0; i < adds.length; i++) {
                    const options = adds[i];
                    const modifyLabel = labelCollection._labels.find(v => v.id === deletes[i].id);
                    modifyLabel && Object.keys(options).forEach(prop => {
                        modifyLabel[prop] = primitiveCollectionsState.transformProp(prop, options[prop]);
                    });
                }
            }
            else {
                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];
                    const labelOptionsTransform = primitiveCollectionsState.transformProps(labelOptions);
                    const labelAdded = labelCollection.add(labelOptionsTransform);
                    labelAdded.id !== labelOptions.id && (labelOptions.id = labelAdded.id);
                }
            }
        }, {
            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];
                const labelOptionsTransform = primitiveCollectionsState.transformProps(labelOptions);
                const label = labelCollection.add(labelOptionsTransform);
                labelOptions.id !== label.id && (labelOptions.id = label.id);
            }
            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;
            const adds = differenceWith__default['default'](newVal, oldVal, isEqual__default['default']);
            const deletes = differenceWith__default['default'](oldVal, newVal, isEqual__default['default']);
            if (newVal.length === oldVal.length && adds.length === deletes.length) {
                for (let i = 0; i < adds.length; i++) {
                    const options = adds[i];
                    const modifyPoint = pointCollection._points.find(v => v.id === deletes[i].id);
                    modifyPoint && Object.keys(options).forEach(prop => {
                        modifyPoint[prop] = primitiveCollectionsState.transformProp(prop, options[prop]);
                    });
                }
            }
            else {
                const deletePoints = [];
                for (let i = 0; i < deletes.length; i++) {
                    const deletePoint = pointCollection._points.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];
                    const pointOptionsTransform = primitiveCollectionsState.transformProps(pointOptions);
                    const pointAdded = pointCollection.add(pointOptionsTransform);
                    pointAdded.id !== pointOptions.id && (pointOptions.id = pointAdded.id);
                }
            }
        }, {
            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];
                const pointOptionsTransform = primitiveCollectionsState.transformProps(pointOptions);
                const point = pointCollection.add(pointOptionsTransform);
                pointOptions.id !== point.id && (pointOptions.id = point.id);
            }
            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({}, cesiumProps.color), cesiumProps.disableDepthTestDistance), cesiumProps.distanceDisplayCondition), cesiumProps.id), cesiumProps.outlineColor), cesiumProps.outlineWidth), 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;
            const adds = differenceWith__default['default'](newVal, oldVal, isEqual__default['default']);
            const deletes = differenceWith__default['default'](oldVal, newVal, isEqual__default['default']);
            if (newVal.length === oldVal.length && adds.length === deletes.length) {
                for (let i = 0; i < adds.length; i++) {
                    const options = adds[i];
                    const modifyPolyline = polylineCollection._polylines.find(v => v.id === deletes[i].id);
                    modifyPolyline && Object.keys(options).forEach(prop => {
                        modifyPolyline[prop] = primitiveCollectionsState.transformProp(prop, options[prop]);
                    });
                }
            }
            else {
                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];
                    const polylineOptionsTransform = primitiveCollectionsState.transformProps(polylineOptions);
                    const polylineAdded = polylineCollection.add(polylineOptionsTransform);
                    polylineAdded.id !== polylineOptions.id && (polylineOptions.id = polylineAdded.id);
                }
            }
        }, {
            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];
                const polylineOptionsTransform = primitiveCollectionsState.transformProps(polylineOptions);
                const polyline = polylineCollection.add(polylineOptionsTransform);
                polylineOptions.id !== polyline.id && (polylineOptions.id = polyline.id);
            }
            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;
