import { defineComponent, getCurrentInstance, watch, onUnmounted, h, createCommentVNode } from 'vue';
import { usePrimitiveCollections, usePrimitiveCollectionItems } from '../composables';
import differenceWith from 'lodash/differenceWith';
import cloneDeep from 'lodash/cloneDeep';
import isEqual from 'lodash/isEqual';
import { scene, blendOption, enableMouseEvent, alignedAxis, color, disableDepthTestDistance, distanceDisplayCondition, eyeOffset, height, heightReference, horizontalOrigin, id, image, pixelOffset, pixelOffsetScaleByDistance, position, rotation, scale, scaleByDistance, show, sizeInMeters, translucencyByDistance, verticalOrigin, width, modelMatrix, debugShowBoundingVolume, backgroundColor, backgroundPadding, fillColor, font, outlineColor, outlineWidth, showBackground, labelStyle, text, loop, material, positions } from '../utils/cesium-props';
import { kebabCase } from '../utils/util';
import { hSlot } from '../utils/private/render';

/*! *****************************************************************************
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 = defineComponent({
    name: 'VcCollectionBillboard',
    props: Object.assign(Object.assign(Object.assign(Object.assign({}, scene), blendOption), enableMouseEvent), { billboards: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'BillboardCollection';
        const primitiveCollectionsState = usePrimitiveCollections(props, ctx, instance);
        let unwatchFns = [];
        unwatchFns.push(watch(() => cloneDeep(props.billboards), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const billboardCollection = instance.cesiumObject;
            const adds = differenceWith(newVal, oldVal, isEqual);
            const deletes = differenceWith(oldVal, newVal, isEqual);
            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;
        });
        onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (h('i', {
            class: kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, hSlot(ctx.slots.default))) : createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

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

var VcCollectionLabel = defineComponent({
    name: 'VcCollectionLabel',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, modelMatrix), debugShowBoundingVolume), scene), blendOption), show), enableMouseEvent), { labels: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'LabelCollection';
        const primitiveCollectionsState = usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('labels');
        let unwatchFns = [];
        unwatchFns.push(watch(() => cloneDeep(props.labels), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const labelCollection = instance.cesiumObject;
            const adds = differenceWith(newVal, oldVal, isEqual);
            const deletes = differenceWith(oldVal, newVal, isEqual);
            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;
        });
        onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (h('i', {
            class: kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, hSlot(ctx.slots.default))) : createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

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

var VcCollectionPoint = defineComponent({
    name: 'VcCollectionPoint',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, modelMatrix), debugShowBoundingVolume), blendOption), show), enableMouseEvent), { points: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'PointPrimitiveCollection';
        const primitiveCollectionsState = usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('points');
        let unwatchFns = [];
        unwatchFns.push(watch(() => cloneDeep(props.points), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const pointCollection = instance.cesiumObject;
            const adds = differenceWith(newVal, oldVal, isEqual);
            const deletes = differenceWith(oldVal, newVal, isEqual);
            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;
        });
        onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (h('i', {
            class: kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, hSlot(ctx.slots.default))) : createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

var VcPoint = 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({}, color), disableDepthTestDistance), distanceDisplayCondition), id), outlineColor), outlineWidth), position), scaleByDistance), show), translucencyByDistance), enableMouseEvent),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'PointPrimitive';
        usePrimitiveCollectionItems(props, ctx, instance);
        return () => createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

var VcCollectionPolyline = defineComponent({
    name: 'VcCollectionPolyline',
    props: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, modelMatrix), debugShowBoundingVolume), show), enableMouseEvent), { polylines: {
            type: Array,
            default: () => []
        } }),
    emits: ['beforeLoad', 'ready', 'destroyed'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'PolylineCollection';
        const primitiveCollectionsState = usePrimitiveCollections(props, ctx, instance);
        instance.alreadyListening.push('polylines');
        let unwatchFns = [];
        unwatchFns.push(watch(() => cloneDeep(props.polylines), (newVal, oldVal) => {
            if (!instance.mounted) {
                return;
            }
            const polylineCollection = instance.cesiumObject;
            const adds = differenceWith(newVal, oldVal, isEqual);
            const deletes = differenceWith(oldVal, newVal, isEqual);
            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;
        });
        onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        return () => ctx.slots.default ? (h('i', {
            class: kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, hSlot(ctx.slots.default))) : createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

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

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

export default index;
export { VcBillboard, VcCollectionBillboard, VcCollectionLabel, VcCollectionPoint, VcCollectionPolyline, VcCollectionPrimitive, VcLabel, VcPoint, VcPolyline };
