import { defineComponent, getCurrentInstance, watch, onUnmounted, h, createCommentVNode } from 'vue';
import { usePrimitiveCollections, usePrimitiveCollectionItems } from '../composables';
import cloneDeep from 'lodash/cloneDeep';
import differenceBy from 'lodash/differenceBy';
import { scene, blendOption, show, enableMouseEvent, alignedAxis, color, disableDepthTestDistance, distanceDisplayCondition, eyeOffset, height, heightReference, horizontalOrigin, id, image, pixelOffset, pixelOffsetScaleByDistance, position, rotation, scale, scaleByDistance, sizeInMeters, translucencyByDistance, verticalOrigin, width, modelMatrix, debugShowBoundingVolume, backgroundColor, backgroundPadding, fillColor, font, outlineColor, outlineWidth, showBackground, labelStyle, text, pixelSize, 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(Object.assign({}, scene), blendOption), show), 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;
            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(newVal, oldVal, 'id');
                const deletes = differenceBy(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;
        });
        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;
            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(newVal, oldVal, 'id');
                const deletes = differenceBy(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;
        });
        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;
            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(newVal, oldVal, 'id');
                const deletes = differenceBy(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;
        });
        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(Object.assign({}, color), disableDepthTestDistance), distanceDisplayCondition), id), outlineColor), outlineWidth), pixelSize), 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;
            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(newVal, oldVal, 'id');
                const deletes = differenceBy(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;
        });
        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 };
