import { defineComponent, getCurrentInstance, ref, watch, onUnmounted, h, nextTick, toRef, computed, reactive, provide, createCommentVNode } from 'vue';
import usePosition, { positionProps } from '../composables/private/use-position';
import { show, enableMouseEvent } from '../utils/cesium-props';
import { getDefaultOptionByProps, camelize, isString } from '../utils/util';
import { useCommon, usePolylineDrawing, useHandler } from '../composables';
import { $ } from '../utils/private/vm';
import { VcBtn, VcTooltip, VcFabAction, VcFab } from '../ui';
import { t } from '../locale';
import { vcKey } from '../utils/config';
import { mergeDescriptors } from '../utils/merge-descriptors';
import { VcCollectionPoint, VcCollectionPrimitive } from '../primitive-collections';
import { DrawStatus, VisibilityState } from '../vc-shared';
import { VcOverlayHtml } from '../overlays';
import { VcPrimitiveGroundPolyline, VcPrimitive, VcPrimitiveGround } from '../vc-primitives';
import { makeMaterial, getHeadingPitchRoll, getGeodesicDistance, getPolylineSegmentEndpoint } from '../utils/cesium-helpers';
import VcInstanceGeometry from '../vc-geometry-instance';
import { VcGeometryPolylineGround, VcGeometryPolyline, VcGeometryPolygon } from '../geometries';

/*! *****************************************************************************
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());
    });
}

const actionOptions = {
    externalLabel: false,
    label: '',
    labelPosition: 'right',
    hideLabel: false,
    tabindex: undefined,
    disable: false,
    outline: false,
    push: false,
    flat: false,
    unelevated: false,
    padding: undefined,
    color: 'primary',
    textColor: undefined,
    glossy: false,
    labelClass: undefined,
    labelStyle: undefined,
    square: false,
    tooltip: {
        delay: 500,
        anchor: 'bottom middle',
        offset: [0, 20],
        tip: undefined
    }
};
const mainFabDefault = Object.assign({}, actionOptions, {
    direction: 'left',
    icon: 'vc-icons-drawing-button',
    activeIcon: 'vc-icons-drawing-button',
    verticalActionsAlign: 'center',
    hideIcon: false,
    persistent: false,
    autoExpand: true,
    hideActionOnClick: false,
    color: 'info'
});
const pointOptsDefault = {
    show: true,
    color: 'rgb(255,229,0)',
    pixelSize: 8,
    outlineColor: 'black',
    outlineWidth: 1,
    disableDepthTestDistance: Number.POSITIVE_INFINITY
};
const polylineOptsDefault = {
    material: {
        fabric: {
            type: 'Color',
            uniforms: {
                color: '#51ff00'
            }
        }
    },
    depthFailMaterial: {
        fabric: {
            type: 'PolylineDash',
            uniforms: {
                color: [255, 0, 0, 127]
            }
        }
    },
    width: 2,
    arcType: 0,
    ellipsoid: undefined,
    show: true
};
const polygonOptsDefault = {
    material: {
        fabric: {
            type: 'Color',
            uniforms: {
                color: [255, 165, 0, 125]
            }
        }
    },
    depthFailMaterial: {
        fabric: {
            type: 'Color',
            uniforms: {
                color: [255, 165, 0, 125]
            }
        }
    },
    perPositionHeight: true,
    arcType: undefined,
    show: true
};
const editorOptsDefault = {
    icon: 'vc-icons-move',
    size: '24px',
    color: '#1296db',
    background: '#fff',
    round: true,
    flat: false,
    label: undefined,
    stack: false,
    dense: true,
    tooltip: {
        delay: 1000,
        anchor: 'bottom middle',
        offset: [0, 20]
    }
};
const pointActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-drawing-point',
});
const polylineActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-drawing-polyline',
});
const polygonActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-drawing-polygon',
});
const rectangleActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-drawing-rectangle',
});
const circleActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-drawing-circle',
});
const pointDrawingDefault = {
    show: true,
    drawtip: {
        show: true,
        pixelOffset: [32, 32]
    },
    pointOpts: pointOptsDefault,
    editorOpts: {
        pixelOffset: [4, -4],
        move: Object.assign({}, editorOptsDefault),
        remove: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-remove',
        })
    }
};
const polylineDrawingDefault = {
    show: true,
    drawtip: {
        show: true,
        pixelOffset: [32, 32]
    },
    pointOpts: pointOptsDefault,
    polylineOpts: polylineOptsDefault,
    editorOpts: {
        pixelOffset: [4, -4],
        move: Object.assign({}, editorOptsDefault),
        insert: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-insert',
        }),
        remove: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-remove',
        }),
        removeAll: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-delete',
        })
    },
    loop: false
};
const polygonDrawingDefault = {
    show: true,
    drawtip: {
        show: true,
        pixelOffset: [32, 32]
    },
    pointOpts: pointOptsDefault,
    polylineOpts: Object.assign({}, polylineOptsDefault, {
        depthFailMaterial: {
            fabric: {
                type: 'Color',
                uniforms: {
                    color: '#51ff00'
                }
            }
        }
    }),
    polygonOpts: polygonOptsDefault,
    editorOpts: {
        pixelOffset: [4, -4],
        move: Object.assign({}, editorOptsDefault),
        insert: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-insert',
        }),
        remove: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-remove',
        }),
        removeAll: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-delete',
        })
    },
    loop: true
};
const rectangleDrawingDefault = Object.assign({}, polygonDrawingDefault, {
    pointOpts: Object.assign({}, pointOptsDefault, {
        show: false
    }),
    drawtip: {
        show: true,
        pixelOffset: [32, 32]
    },
    editorOpts: {
        pixelOffset: [4, -4],
        move: Object.assign({}, editorOptsDefault),
        removeAll: Object.assign({}, editorOptsDefault, {
            icon: 'vc-icons-delete',
        })
    },
    edge: 4,
    regular: true
});
const circleDrawingDefault = Object.assign({}, rectangleDrawingDefault, {
    edge: 360
});
const clearActionDefault = Object.assign({}, actionOptions, {
    icon: 'vc-icons-clear',
    color: 'red'
});
const defaultProps = Object.assign(Object.assign(Object.assign({}, positionProps), show), { mode: {
        type: Number,
        default: 1
    }, drawings: {
        type: Array,
        default: () => ['point', 'polyline', 'polygon', 'rectangle', 'circle']
    }, activeColor: {
        type: String,
        default: 'positive'
    }, editable: {
        type: Boolean,
        default: false
    }, clampToGround: {
        type: Boolean,
        default: false
    }, mainFabOpts: {
        type: Object,
        default: () => mainFabDefault
    }, pointActionOpts: {
        type: Object,
        default: () => pointActionDefault
    }, pointDrawingOpts: {
        type: Object,
        default: () => pointDrawingDefault
    }, polylineActionOpts: {
        type: Object,
        default: () => polylineActionDefault
    }, polylineDrawingOpts: {
        type: Object,
        default: () => polylineDrawingDefault
    }, polygonActionOpts: {
        type: Object,
        default: () => polygonActionDefault
    }, polygonDrawingOpts: {
        type: Object,
        default: () => polygonDrawingDefault
    }, rectangleActionOpts: {
        type: Object,
        default: () => rectangleActionDefault
    }, rectangleDrawingOpts: {
        type: Object,
        default: () => rectangleDrawingDefault
    }, circleActionOpts: {
        type: Object,
        default: () => circleActionDefault
    }, circleDrawingOpts: {
        type: Object,
        default: () => circleDrawingDefault
    }, clearActionOpts: {
        type: Object,
        default: () => clearActionDefault
    } });
const defaultOptions = getDefaultOptionByProps(defaultProps);

var defaultProps$1 = Object.assign(Object.assign({}, enableMouseEvent), { show: Boolean, editable: Boolean, drawtip: Object, pointOpts: Object, editorOpts: Object, mode: Number });

var VcDrawingPoint = defineComponent({
    name: 'VcDrawingPoint',
    props: defaultProps$1,
    emits: ['beforeLoad', 'ready', 'destroyed', 'drawEvt', 'editorEvt', 'mouseEvt'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawingPoint';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const points = ref([]);
        const drawStatus = ref(DrawStatus.BeforeDraw);
        const canShowDrawTip = ref(false);
        const drawTipPosition = ref([0, 0, 0]);
        const drawTip = ref('');
        const showEditor = ref(false);
        const editorPosition = ref([0, 0, 0]);
        const mouseoverPoint = ref(null);
        const editingPoint = ref(null);
        const primitiveCollectionRef = ref(null);
        let restorePoint = undefined;
        let unwatchFns = [];
        let editorType = '';
        unwatchFns.push(watch(() => props.editable, val => {
            const { drawingVm, selectedDrawingOption } = $services;
            if (val && (selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.name) === 'point') {
                drawingVm.proxy.toggleAction(selectedDrawingOption);
            }
        }));
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            return primitiveCollectionRef;
        });
        const startNew = () => {
            const { Cartesian3 } = Cesium;
            const point = {
                drawStatus: DrawStatus.Drawing,
                show: false,
                position: new Cartesian3()
            };
            points.value.push(point);
            drawStatus.value = DrawStatus.Drawing;
            canShowDrawTip.value = true;
            drawTip.value = props.drawtip.drawingTip1 || t('vc.drawing.point.drawTip1');
        };
        const stop = () => {
            if (drawStatus.value === DrawStatus.Drawing) {
                points.value.pop();
            }
            drawStatus.value = DrawStatus.BeforeDraw;
            canShowDrawTip.value = false;
            drawTipPosition.value = [0, 0, 0];
        };
        const handleMouseClick = (movement, options) => {
            const { viewer, drawingVm, getWorldPosition, selectedDrawingOption } = $services;
            if (options.button === 2 && options.ctrl) {
                const drawingsOption = drawingVm.proxy.drawingsOptions.find(v => v.name === 'point');
                drawingVm.proxy.toggleAction(drawingsOption);
                return;
            }
            const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : points.value.length - 1;
            const point = points.value[index];
            if (options.button === 2 && editingPoint.value) {
                drawingVm.proxy.editingDrawingName = undefined;
                points.value[index] = restorePoint;
                drawStatus.value = DrawStatus.AfterDraw;
                points.value[index].drawStatus = DrawStatus.AfterDraw;
                editingPoint.value = undefined;
                drawTip.value = props.drawtip.drawTip1 || t('vc.drawing.point.drawTip1');
                return;
            }
            if (options.button !== 0) {
                return;
            }
            const { defined } = Cesium;
            let type = 'new';
            if (drawStatus.value === DrawStatus.BeforeDraw) {
                const scene = viewer.scene;
                const position = getWorldPosition(scene, movement, {});
                if (!defined(position)) {
                    return;
                }
                point.position = position;
                point.show = true;
                point.drawStatus = DrawStatus.AfterDraw;
                drawStatus.value = DrawStatus.AfterDraw;
                drawTip.value = props.drawtip.drawingTip1 || t('vc.drawing.point.drawTip1');
                nextTick(() => {
                    emit('drawEvt', {
                        index: index,
                        points: points,
                        name: 'point',
                        finished: true,
                        position: position,
                        windowPoistion: movement,
                        type: type
                    }, viewer);
                });
            }
            else {
                drawStatus.value = DrawStatus.AfterDraw;
                point.drawStatus = DrawStatus.AfterDraw;
                if (editingPoint.value) {
                    editingPoint.value = undefined;
                    drawingVm.proxy.editingDrawingName = undefined;
                    canShowDrawTip.value = false;
                    type = editorType;
                }
                else {
                    if (props.mode === 1) {
                        drawingVm.proxy.toggleAction(selectedDrawingOption);
                    }
                }
                if (selectedDrawingOption) {
                    drawTip.value = t('vc.drawing.point.drawTip1');
                    canShowDrawTip.value = true;
                }
                nextTick(() => {
                    emit('drawEvt', {
                        index: index,
                        points: points,
                        name: 'point',
                        finished: true,
                        position: points.value[index].position,
                        windowPoistion: movement,
                        type: type
                    }, viewer);
                });
            }
        };
        const handleMouseMove = movement => {
            if (!canShowDrawTip.value) {
                return;
            }
            const { viewer, getWorldPosition } = $services;
            const scene = viewer.scene;
            const { defined, SceneMode } = Cesium;
            if (scene.mode !== SceneMode.MORPHING) {
                const position = getWorldPosition(scene, movement, {});
                if (!defined(position)) {
                    return;
                }
                drawTipPosition.value = position;
                if (drawStatus.value === DrawStatus.AfterDraw) {
                    startNew();
                }
                if (drawStatus.value !== DrawStatus.Drawing) {
                    return;
                }
                const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : points.value.length - 1;
                const point = points.value[index];
                const type = editingPoint.value ? editorType : 'new';
                point.position = position;
                point.show = true;
                nextTick(() => {
                    emit('drawEvt', {
                        index: index,
                        points: point,
                        name: 'point',
                        finished: false,
                        position: position,
                        windowPoistion: movement,
                        type: type
                    }, viewer);
                });
            }
        };
        const onMouseoverPoints = e => {
            const { drawingHandlerActive, viewer } = $services;
            if (props.editable && drawStatus.value !== DrawStatus.Drawing && drawingHandlerActive) {
                e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.5;
                mouseoverPoint.value = e.pickedFeature.primitive;
                editorPosition.value = e.pickedFeature.primitive.position;
                showEditor.value = true;
                canShowDrawTip.value = false;
            }
            emit('mouseEvt', {
                type: e.type,
                name: 'point',
                target: e
            }, viewer);
        };
        const onMouseoutPoints = e => {
            const { viewer, selectedDrawingOption } = $services;
            if (props.editable) {
                e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.0;
                editorPosition.value = [0, 0, 0];
                mouseoverPoint.value = undefined;
                showEditor.value = false;
                selectedDrawingOption && (canShowDrawTip.value = true);
            }
            emit('mouseEvt', {
                type: e.type,
                name: 'point',
                target: e
            }, viewer);
        };
        const onEditorClick = e => {
            editorPosition.value = [0, 0, 0];
            showEditor.value = false;
            if (!props.editable) {
                return;
            }
            editorType = e;
            const { viewer, drawingVm } = $services;
            if (e === 'move') {
                drawTip.value = t('vc.drawing.point.drawTip3');
                drawStatus.value = DrawStatus.Drawing;
                editingPoint.value = mouseoverPoint.value;
                canShowDrawTip.value = true;
                restorePoint = Object.assign({}, points.value[editingPoint.value._vcPolylineIndx]);
                drawingVm.proxy.editingDrawingName = 'point';
            }
            else if (e === 'remove') {
                const index = mouseoverPoint.value._vcPolylineIndx;
                points.value.splice(index, 1);
            }
            emit('editorEvt', {
                type: e,
                name: 'point',
                points: points,
                index: mouseoverPoint.value._vcPolylineIndx
            }, viewer);
        };
        const clear = () => {
            points.value = [];
            stop();
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawingPoint';
        };
        onUnmounted(() => {
            unwatchFns.forEach(item => item());
            unwatchFns = [];
        });
        const publicMethods = { points, startNew, stop, clear, handleMouseClick, handleMouseMove };
        Object.assign(instance.proxy, publicMethods);
        return () => {
            var _a;
            const { createGuid } = Cesium;
            const children = [];
            const pointsRender = [];
            points.value.forEach((point, index) => {
                pointsRender.push(Object.assign({ show: point.show, position: point.position, id: createGuid(), _vcPolylineIndx: index }, props.pointOpts));
            });
            children.push(h(VcCollectionPoint, {
                enableMouseEvent: props.enableMouseEvent,
                points: pointsRender,
                onMouseover: onMouseoverPoints,
                onMouseout: onMouseoutPoints
            }));
            if (props.drawtip.show && canShowDrawTip.value) {
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: drawTipPosition.value,
                    pixelOffset: props.drawtip.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-drawtip vc-tooltip--style'
                }, drawTip.value)));
            }
            if (showEditor.value) {
                const buttons = [];
                if (mouseoverPoint.value) {
                    const editorOpts = props.editorOpts;
                    for (const key in editorOpts) {
                        if (!Array.isArray(editorOpts[key])) {
                            const opts = Object.assign({}, editorOpts[key]);
                            delete opts.color;
                            buttons.push(h(VcBtn, Object.assign(Object.assign({ style: { color: editorOpts[key].color, background: editorOpts[key].background } }, opts), { onclick: onEditorClick.bind(undefined, key) }), () => h(VcTooltip, Object.assign({}, editorOpts[key].tooltip), () => { var _a; return h('strong', null, ((_a = editorOpts[key].tooltip) === null || _a === void 0 ? void 0 : _a.tip) || t(`vc.drawing.editor.${key}`)); })));
                        }
                    }
                }
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: editorPosition.value,
                    pixelOffset: (_a = props.editorOpts) === null || _a === void 0 ? void 0 : _a.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-editor'
                }, buttons)));
            }
            return h(VcCollectionPrimitive, {
                ref: primitiveCollectionRef,
                show: props.show,
                onReady: onPrimitiveCollectionReady
            }, () => children);
        };
    }
});

var defaultProps$2 = Object.assign(Object.assign({}, enableMouseEvent), { editable: Boolean, show: Boolean, drawtip: Object, pointOpts: Object, polylineOpts: Object, editorOpts: Object, loop: Boolean, clampToGround: Boolean, mode: Number });

var VcDrawingPolyline = defineComponent({
    name: 'VcDrawingPolyline',
    props: defaultProps$2,
    emits: ['beforeLoad', 'ready', 'destroyed', 'drawEvt', 'editorEvt', 'mouseEvt'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawingPolyline';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const drawTip = toRef(props, 'drawtip');
        drawTip.value.drawTip1 = drawTip.value.drawingTip1 || t('vc.drawing.polyline.drawTip1');
        drawTip.value.drawTip2 = drawTip.value.drawingTip2 || t('vc.drawing.polyline.drawTip2');
        drawTip.value.drawTip3 = drawTip.value.drawingTip3 || t('vc.drawing.polyline.drawTip3');
        const polylineDrawingState = usePolylineDrawing(props, $services, drawTip.value, ctx);
        const primitiveCollectionRef = ref(null);
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            return primitiveCollectionRef;
        });
        const handleMouseClick = (movement, options) => {
            const result = polylineDrawingState.handleMouseClick(movement, options);
            const { defined } = Cesium;
            if (defined(result)) {
                const { drawingVm, selectedDrawingOption, viewer } = $services;
                if (defined(result.position)) {
                    if (result.type !== 'new') {
                        drawingVm.proxy.editingDrawingName = undefined;
                        polylineDrawingState.canShowDrawTip.value = defined(selectedDrawingOption);
                    }
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polyline'
                        }), viewer);
                    });
                }
                else {
                    const drawingsOption = drawingVm.proxy.drawingsOptions.find(v => v.name === 'polyline');
                    drawingVm.proxy.toggleAction(drawingsOption);
                }
            }
        };
        const handleMouseMove = (movement) => {
            const result = polylineDrawingState.handleMouseMove(movement);
            const { defined } = Cesium;
            if (defined(result)) {
                const { viewer } = $services;
                if (defined(result.position)) {
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polyline'
                        }), viewer);
                    });
                }
            }
        };
        const handleDoubleClick = movement => {
            const { drawingVm, selectedDrawingOption, viewer } = $services;
            const result = polylineDrawingState.handleDoubleClick(movement);
            const { defined } = Cesium;
            if (defined(result)) {
                if (defined(result.position)) {
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polyline'
                        }), viewer);
                        if (props.mode === 1) {
                            drawingVm.proxy.toggleAction(selectedDrawingOption);
                        }
                    });
                }
            }
        };
        const startNew = () => {
            polylineDrawingState.startNew();
        };
        const stop = () => {
            polylineDrawingState.stop();
        };
        const clear = () => {
            polylineDrawingState.polylines.value = [];
            stop();
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawingPolyline';
        };
        const onEditorClick = e => {
            polylineDrawingState.onEditorClick(e);
            const { drawingVm } = $services;
            drawingVm.proxy.editingDrawingName = 'polyline';
        };
        const publicMethods = {
            polylines: polylineDrawingState.polylines, startNew, stop, clear, handleMouseClick, handleMouseMove, handleDoubleClick
        };
        Object.assign(instance.proxy, publicMethods);
        return () => {
            var _a;
            const { PolylineMaterialAppearance, createGuid } = Cesium;
            const polylineOpts = Object.assign(Object.assign({}, props.polylineOpts), { vertexFormat: PolylineMaterialAppearance.VERTEX_FORMAT });
            props.clampToGround && delete polylineOpts.arcType;
            const children = [];
            polylineDrawingState.polylines.value.forEach((polyline, index) => {
                const positions = polyline.positions.slice();
                if (positions.length > 1) {
                    polyline.loop && positions.push(positions[0]);
                    children.push(h(props.clampToGround ? VcPrimitiveGroundPolyline : VcPrimitive, {
                        show: polyline.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.material)
                        }),
                        depthFailAppearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.depthFailMaterial)
                        }),
                        asynchronous: false
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid()
                    }, () => h(props.clampToGround ? VcGeometryPolylineGround : VcGeometryPolyline, Object.assign({ positions: positions }, polylineOpts)))));
                }
                children.push(h(VcCollectionPoint, {
                    enableMouseEvent: props.enableMouseEvent,
                    show: polyline.show,
                    points: polyline.positions.map(position => (Object.assign(Object.assign({ position: position, id: createGuid(), _vcPolylineIndx: index }, props.pointOpts), { show: props.pointOpts.show || props.editable || polyline.drawStatus === DrawStatus.Drawing }))),
                    onMouseover: polylineDrawingState.onMouseoverPoints.bind('polyline'),
                    onMouseout: polylineDrawingState.onMouseoutPoints.bind('polyline')
                }));
            });
            if (props.drawtip.show && polylineDrawingState.canShowDrawTip.value) {
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: polylineDrawingState.drawTipPosition.value,
                    pixelOffset: props.drawtip.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-drawtip vc-tooltip--style'
                }, polylineDrawingState.drawTip.value)));
            }
            if (polylineDrawingState.showEditor.value) {
                const buttons = [];
                if (polylineDrawingState.mouseoverPoint.value) {
                    const editorOpts = props.editorOpts;
                    for (const key in editorOpts) {
                        if (!Array.isArray(editorOpts[key])) {
                            const opts = Object.assign({}, editorOpts[key]);
                            delete opts.color;
                            buttons.push(h(VcBtn, Object.assign(Object.assign({ style: { color: editorOpts[key].color, background: editorOpts[key].background } }, opts), { onclick: onEditorClick.bind('polyline', key) }), () => h(VcTooltip, Object.assign({}, editorOpts[key].tooltip), () => { var _a; return h('strong', null, ((_a = editorOpts[key].tooltip) === null || _a === void 0 ? void 0 : _a.tip) || t(`vc.drawing.editor.${key}`)); })));
                        }
                    }
                }
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: polylineDrawingState.editorPosition.value,
                    pixelOffset: (_a = props.editorOpts) === null || _a === void 0 ? void 0 : _a.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-editor'
                }, buttons)));
            }
            return h(VcCollectionPrimitive, {
                ref: primitiveCollectionRef,
                show: props.show,
                onReady: onPrimitiveCollectionReady
            }, () => children);
        };
    }
});

var defaultProps$3 = Object.assign(Object.assign({}, enableMouseEvent), { editable: Boolean, show: Boolean, drawtip: Object, pointOpts: Object, polylineOpts: Object, polygonOpts: Object, editorOpts: Object, loop: Boolean, clampToGround: Boolean, mode: Number });

var VcDrawingPolygon = defineComponent({
    name: 'VcDrawingPolygon',
    props: defaultProps$3,
    emits: ['beforeLoad', 'ready', 'destroyed', 'drawEvt', 'editorEvt', 'mouseEvt'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawingPolygon';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const drawTip = toRef(props, 'drawtip');
        drawTip.value.drawTip1 = drawTip.value.drawingTip1 || t('vc.drawing.polygon.drawTip1');
        drawTip.value.drawTip2 = drawTip.value.drawingTip2 || t('vc.drawing.polygon.drawTip2');
        drawTip.value.drawTip3 = drawTip.value.drawingTip3 || t('vc.drawing.polygon.drawTip3');
        const polylineDrawingState = usePolylineDrawing(props, $services, drawTip.value, ctx);
        const primitiveCollectionRef = ref(null);
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            return primitiveCollectionRef;
        });
        const handleMouseClick = (movement, options) => {
            const result = polylineDrawingState.handleMouseClick(movement, options);
            const { defined } = Cesium;
            if (defined(result)) {
                const { drawingVm, selectedDrawingOption, viewer } = $services;
                if (defined(result.position)) {
                    if (result.type !== 'new') {
                        drawingVm.proxy.editingDrawingName = undefined;
                        polylineDrawingState.canShowDrawTip.value = defined(selectedDrawingOption);
                    }
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polygon'
                        }), viewer);
                    });
                }
                else {
                    const drawingsOption = drawingVm.proxy.drawingsOptions.find(v => v.name === 'polygon');
                    drawingVm.proxy.toggleAction(drawingsOption);
                }
            }
        };
        const handleMouseMove = (movement) => {
            const result = polylineDrawingState.handleMouseMove(movement);
            const { defined } = Cesium;
            if (defined(result)) {
                if (defined(result.position)) {
                    const { viewer } = $services;
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polygon'
                        }), viewer);
                    });
                }
            }
        };
        const handleDoubleClick = movement => {
            const { drawingVm, selectedDrawingOption, viewer } = $services;
            const result = polylineDrawingState.handleDoubleClick(movement);
            const { defined } = Cesium;
            if (defined(result)) {
                if (defined(result.position)) {
                    nextTick(() => {
                        emit('drawEvt', Object.assign(result, {
                            name: 'polygon'
                        }), viewer);
                        if (props.mode === 1) {
                            drawingVm.proxy.toggleAction(selectedDrawingOption);
                        }
                    });
                }
            }
        };
        const startNew = () => {
            polylineDrawingState.startNew();
        };
        const stop = () => {
            polylineDrawingState.stop();
        };
        const clear = () => {
            polylineDrawingState.polylines.value = [];
            stop();
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawingPolygon';
        };
        const onEditorClick = e => {
            polylineDrawingState.onEditorClick(e);
            const { drawingVm } = $services;
            drawingVm.proxy.editingDrawingName = 'polygon';
        };
        const publicMethods = {
            polylines: polylineDrawingState.polylines, startNew, stop, clear, handleMouseClick, handleMouseMove, handleDoubleClick
        };
        Object.assign(instance.proxy, publicMethods);
        return () => {
            var _a;
            const { PolylineMaterialAppearance, EllipsoidSurfaceAppearance, createGuid } = Cesium;
            const polylineOpts = Object.assign(Object.assign({}, props.polylineOpts), { vertexFormat: PolylineMaterialAppearance.VERTEX_FORMAT });
            props.clampToGround && delete polylineOpts.arcType;
            const children = [];
            polylineDrawingState.polylines.value.forEach((polyline, index) => {
                children.push(h(VcCollectionPoint, {
                    enableMouseEvent: props.enableMouseEvent,
                    show: polyline.show,
                    points: polyline.positions.map(position => (Object.assign(Object.assign({ position: position, id: createGuid(), _vcPolylineIndx: index }, props.pointOpts), { show: props.pointOpts.show || props.editable || polyline.drawStatus === DrawStatus.Drawing }))),
                    onMouseover: polylineDrawingState.onMouseoverPoints.bind('polygon'),
                    onMouseout: polylineDrawingState.onMouseoutPoints.bind('polygon')
                }));
                const positions = polyline.positions.slice();
                if (positions.length > 1) {
                    positions.push(positions[0]);
                    children.push(h(props.clampToGround ? VcPrimitiveGroundPolyline : VcPrimitive, {
                        show: polyline.show && polylineOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.material)
                        }),
                        depthFailAppearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.depthFailMaterial)
                        }),
                        asynchronous: false
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid()
                    }, () => h(props.clampToGround ? VcGeometryPolylineGround : VcGeometryPolyline, Object.assign({ positions: positions }, polylineOpts)))));
                }
                if (positions.length > 2) {
                    children.push(h(props.clampToGround ? VcPrimitiveGround : VcPrimitive, {
                        show: polyline.show && props.polygonOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new EllipsoidSurfaceAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.material),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        depthFailAppearance: new EllipsoidSurfaceAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.depthFailMaterial),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        asynchronous: false
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid(),
                    }, () => h(VcGeometryPolygon, Object.assign({ polygonHierarchy: positions }, props.polygonOpts)))));
                }
            });
            if (props.drawtip.show && polylineDrawingState.canShowDrawTip.value) {
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: polylineDrawingState.drawTipPosition.value,
                    pixelOffset: props.drawtip.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-drawtip vc-tooltip--style'
                }, polylineDrawingState.drawTip.value)));
            }
            if (polylineDrawingState.showEditor.value) {
                const buttons = [];
                if (polylineDrawingState.mouseoverPoint.value) {
                    const editorOpts = props.editorOpts;
                    for (const key in editorOpts) {
                        if (!Array.isArray(editorOpts[key])) {
                            const opts = Object.assign({}, editorOpts[key]);
                            delete opts.color;
                            buttons.push(h(VcBtn, Object.assign(Object.assign({ style: { color: editorOpts[key].color, background: editorOpts[key].background } }, opts), { onclick: onEditorClick.bind('polygon', key) }), () => h(VcTooltip, Object.assign({}, editorOpts[key].tooltip), () => { var _a; return h('strong', null, ((_a = editorOpts[key].tooltip) === null || _a === void 0 ? void 0 : _a.tip) || t(`vc.drawing.editor.${key}`)); })));
                        }
                    }
                }
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: polylineDrawingState.editorPosition.value,
                    pixelOffset: (_a = props.editorOpts) === null || _a === void 0 ? void 0 : _a.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-editor'
                }, buttons)));
            }
            return h(VcCollectionPrimitive, {
                ref: primitiveCollectionRef,
                show: props.show,
                onReady: onPrimitiveCollectionReady
            }, () => children);
        };
    }
});

var defaultProps$4 = Object.assign(Object.assign({}, enableMouseEvent), { editable: Boolean, show: Boolean, drawtip: Object, pointOpts: Object, polylineOpts: Object, polygonOpts: Object, editorOpts: Object, clampToGround: Boolean, edge: Number, mode: Number });

var VcDrawingRegularPolygon = defineComponent({
    name: 'VcDrawingRegularPolygon',
    props: defaultProps$4,
    emits: ['beforeLoad', 'ready', 'destroyed', 'drawEvt', 'editorEvt', 'mouseEvt'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawingRegularPolygon';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const polylines = ref([]);
        const drawStatus = ref(DrawStatus.BeforeDraw);
        const canShowDrawTip = ref(false);
        const drawTipPosition = ref([0, 0, 0]);
        const drawTip = ref('');
        const showEditor = ref(false);
        const editorPosition = ref([0, 0, 0]);
        const mouseoverPoint = ref(null);
        const editingPoint = ref(null);
        const primitiveCollectionRef = ref(null);
        let restorePosition = undefined;
        let drawName = 'rectangle';
        if (props.edge === 4) {
            drawName = 'rectangle';
        }
        else if (props.edge === 360) {
            drawName = 'circle';
        }
        let editorType = '';
        const polylinesRender = computed(() => {
            const results = [];
            const { defined, Cartographic } = Cesium;
            const { viewer } = $services;
            polylines.value.forEach((polylineSegment, index) => {
                const startPosition = polylineSegment.positions[0];
                const endPosition = polylineSegment.positions[1];
                const hpr = getHeadingPitchRoll(startPosition, endPosition, $services.viewer.scene);
                if (defined(hpr)) {
                    const positions = [];
                    const startCartographic = Cartographic.fromCartesian(startPosition, viewer.scene.globe.ellipsoid);
                    const endCartographic = Cartographic.fromCartesian(endPosition, viewer.scene.globe.ellipsoid);
                    !props.clampToGround && (endCartographic.height = startCartographic.height);
                    positions.push(Cartographic.toCartesian(endCartographic, viewer.scene.globe.ellipsoid));
                    const distance = getGeodesicDistance(startPosition, endPosition, viewer.scene.globe.ellipsoid);
                    for (let i = 0; i < props.edge - 1; i++) {
                        const position = getPolylineSegmentEndpoint(startPosition, hpr[0] += Math.PI * 2 / props.edge, distance, viewer.scene.globe.ellipsoid);
                        positions.push(position);
                    }
                    const polyline = Object.assign(Object.assign({}, polylineSegment), { polygonPositions: positions, height: startCartographic.height });
                    results.push(polyline);
                }
            });
            return results;
        });
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            return primitiveCollectionRef;
        });
        const startNew = () => {
            const polyline = {
                positions: [],
                polygonPositions: [],
                show: false,
                drawStatus: DrawStatus.BeforeDraw
            };
            polylines.value.push(polyline);
            drawStatus.value = DrawStatus.BeforeDraw;
            canShowDrawTip.value = true;
            drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
        };
        const stop = () => {
            if (drawStatus.value === DrawStatus.Drawing) {
                polylines.value.pop();
            }
            drawStatus.value = DrawStatus.BeforeDraw;
            canShowDrawTip.value = false;
            drawTipPosition.value = [0, 0, 0];
        };
        const handleMouseClick = (movement, options) => {
            const { viewer, drawingVm: drawingVm, selectedDrawingOption, getWorldPosition } = $services;
            if (options.button === 2 && options.ctrl) {
                const drawingsOption = drawingVm.proxy.drawingsOptions.find(v => v.name === drawName);
                drawingVm.proxy.toggleAction(drawingsOption);
                return;
            }
            if (drawStatus.value === DrawStatus.AfterDraw) {
                startNew();
            }
            const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : polylines.value.length - 1;
            const polyline = polylines.value[index];
            const positions = polyline.positions;
            if (options.button === 2 && editingPoint.value) {
                drawingVm.proxy.editingDrawingName = undefined;
                polyline.positions[editingPoint.value._index] = restorePosition;
                drawStatus.value = DrawStatus.AfterDraw;
                polyline.drawStatus = DrawStatus.AfterDraw;
                editingPoint.value = undefined;
                drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
                return;
            }
            if (options.button !== 0) {
                return;
            }
            const { defined } = Cesium;
            let type = 'new';
            if (drawStatus.value === DrawStatus.BeforeDraw) {
                const scene = viewer.scene;
                const position = getWorldPosition(scene, movement, {});
                if (!defined(position)) {
                    return;
                }
                positions[0] = position;
                positions[1] = position;
                polyline.show = true;
                drawStatus.value = DrawStatus.Drawing;
                polyline.drawStatus = DrawStatus.Drawing;
                drawTip.value = props.drawtip.drawTip2 || t(`vc.drawing.${drawName}.drawTip2`);
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: false,
                        position: position,
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
            else {
                polyline.drawStatus = DrawStatus.AfterDraw;
                drawStatus.value = DrawStatus.AfterDraw;
                if (editingPoint.value) {
                    editingPoint.value = undefined;
                    drawingVm.proxy.editingDrawingName = undefined;
                    canShowDrawTip.value = false;
                    drawTipPosition.value = [0, 0, 0];
                    type = editorType;
                }
                else {
                    if (props.mode === 1) {
                        drawingVm.proxy.toggleAction(selectedDrawingOption);
                    }
                }
                if (selectedDrawingOption) {
                    drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
                    canShowDrawTip.value = true;
                }
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: true,
                        position: polyline.positions[1],
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
        };
        const handleMouseMove = movement => {
            if (!canShowDrawTip.value) {
                return;
            }
            const { viewer, getWorldPosition } = $services;
            const scene = viewer.scene;
            const position = getWorldPosition(scene, movement, {});
            const { defined, Cartographic } = Cesium;
            if (!defined(position)) {
                return;
            }
            drawTipPosition.value = position;
            if (drawStatus.value !== DrawStatus.Drawing) {
                return;
            }
            if (defined(position)) {
                const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : polylines.value.length - 1;
                const polyline = polylines.value[index];
                const positions = polyline.positions;
                const startPosition = positions[0];
                const startCartographic = Cartographic.fromCartesian(startPosition, viewer.scene.globe.ellipsoid);
                const endCartographic = Cartographic.fromCartesian(position, viewer.scene.globe.ellipsoid);
                !props.clampToGround && (endCartographic.height = startCartographic.height);
                positions[editingPoint.value ? editingPoint.value._index : 1] = Cartographic.toCartesian(endCartographic, viewer.scene.globe.ellipsoid);
                const type = editingPoint.value ? editorType : 'new';
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: false,
                        position: polyline.positions[1],
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
        };
        const onMouseoverPoints = e => {
            const { drawingHandlerActive, viewer } = $services;
            if (props.editable && drawStatus.value !== DrawStatus.Drawing && drawingHandlerActive) {
                e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.5;
                mouseoverPoint.value = e.pickedFeature.primitive;
                editorPosition.value = e.pickedFeature.primitive.position;
                showEditor.value = true;
                canShowDrawTip.value = false;
                drawTipPosition.value = [0, 0, 0];
            }
            emit('mouseEvt', {
                type: e.type,
                target: e,
                name: drawName
            }, viewer);
        };
        const onMouseoutPoints = e => {
            const { viewer, selectedDrawingOption } = $services;
            if (props.editable) {
                if (!editingPoint.value && drawStatus.value !== DrawStatus.Drawing) {
                    e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.0;
                    editorPosition.value = [0, 0, 0];
                    mouseoverPoint.value = undefined;
                    showEditor.value = false;
                }
                selectedDrawingOption && (canShowDrawTip.value = true);
            }
            emit('mouseEvt', {
                type: e.type,
                target: e,
                name: drawName
            }, viewer);
        };
        const onEditorClick = e => {
            editorPosition.value = [0, 0, 0];
            showEditor.value = false;
            if (!props.editable) {
                return;
            }
            editorType = e;
            const { viewer, drawingVm } = $services;
            if (e === 'move') {
                drawTip.value = props.drawtip.drawTip3 || t(`vc.drawing.${drawName}.drawTip3`);
                drawStatus.value = DrawStatus.Drawing;
                editingPoint.value = mouseoverPoint.value;
                restorePosition = polylines.value[editingPoint.value._vcPolylineIndx].positions[editingPoint.value._index];
                canShowDrawTip.value = true;
                drawingVm.proxy.editingDrawingName = drawName;
            }
            else if (e === 'remove') {
                const index = mouseoverPoint.value._vcPolylineIndx;
                const polyline = polylines.value[index];
                polyline.positions.splice(mouseoverPoint.value._index, 1);
            }
            else if (e === 'removeAll') {
                const index = mouseoverPoint.value._vcPolylineIndx;
                polylines.value.splice(index, 1);
            }
            emit('editorEvt', {
                type: e,
                polylines: polylines,
                name: drawName,
                index: mouseoverPoint.value._vcPolylineIndx
            }, viewer);
        };
        const clear = () => {
            polylines.value = [];
            stop();
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawingRegularPolygon';
        };
        const publicMethods = { polylines, polylinesRender, startNew, stop, clear, handleMouseClick, handleMouseMove };
        Object.assign(instance.proxy, publicMethods);
        return () => {
            var _a;
            const { PolylineMaterialAppearance, MaterialAppearance, createGuid } = Cesium;
            const polylineOpts = Object.assign(Object.assign({}, props.polylineOpts), { vertexFormat: PolylineMaterialAppearance.VERTEX_FORMAT });
            props.clampToGround && delete polylineOpts.arcType;
            const children = [];
            polylinesRender.value.forEach((polyline, index) => {
                if (polyline.polygonPositions.length > 1) {
                    const positions = polyline.polygonPositions.slice();
                    positions.push(positions[0]);
                    children.push(h(props.clampToGround ? VcPrimitiveGroundPolyline : VcPrimitive, {
                        show: polyline.show && polylineOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.material)
                        }),
                        depthFailAppearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.depthFailMaterial)
                        }),
                        asynchronous: false,
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid()
                    }, () => h(props.clampToGround ? VcGeometryPolylineGround : VcGeometryPolyline, Object.assign({ positions: positions }, polylineOpts)))));
                }
                children.push(h(VcCollectionPoint, {
                    enableMouseEvent: props.enableMouseEvent,
                    show: polyline.show,
                    points: polyline.positions.map(position => (Object.assign(Object.assign({ position: position, id: createGuid(), _vcPolylineIndx: index }, props.pointOpts), { show: props.pointOpts.show || props.editable || polyline.drawStatus === DrawStatus.Drawing }))),
                    onMouseover: onMouseoverPoints,
                    onMouseout: onMouseoutPoints
                }));
                if (polyline.polygonPositions.length > 2) {
                    children.push(h(props.clampToGround ? VcPrimitiveGround : VcPrimitive, {
                        show: polyline.show && props.polygonOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new MaterialAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.material),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        depthFailAppearance: new MaterialAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.depthFailMaterial),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        asynchronous: false,
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid(),
                    }, () => h(VcGeometryPolygon, Object.assign({ polygonHierarchy: polyline.polygonPositions }, props.polygonOpts)))));
                }
            });
            if (props.drawtip.show && canShowDrawTip.value) {
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: drawTipPosition.value,
                    pixelOffset: props.drawtip.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-drawtip vc-tooltip--style'
                }, drawTip.value)));
            }
            if (showEditor.value) {
                const buttons = [];
                if (mouseoverPoint.value) {
                    const editorOpts = props.editorOpts;
                    for (const key in editorOpts) {
                        if (!Array.isArray(editorOpts[key])) {
                            const opts = Object.assign({}, editorOpts[key]);
                            delete opts.color;
                            buttons.push(h(VcBtn, Object.assign(Object.assign({ style: { color: editorOpts[key].color, background: editorOpts[key].background } }, opts), { onclick: onEditorClick.bind(undefined, key) }), () => h(VcTooltip, Object.assign({}, editorOpts[key].tooltip), () => { var _a; return h('strong', null, ((_a = editorOpts[key].tooltip) === null || _a === void 0 ? void 0 : _a.tip) || t(`vc.drawing.editor.${key}`)); })));
                        }
                    }
                }
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: editorPosition.value,
                    pixelOffset: (_a = props.editorOpts) === null || _a === void 0 ? void 0 : _a.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-editor'
                }, buttons)));
            }
            return h(VcCollectionPrimitive, {
                ref: primitiveCollectionRef,
                show: props.show,
                onReady: onPrimitiveCollectionReady
            }, () => children);
        };
    }
});

var defaultProps$5 = Object.assign(Object.assign({}, enableMouseEvent), { editable: Boolean, show: Boolean, drawtip: Object, pointOpts: Object, polylineOpts: Object, polygonOpts: Object, editorOpts: Object, clampToGround: Boolean, mode: Number });

var VcDrawingRectangle = defineComponent({
    name: 'VcDrawingRectangle',
    props: defaultProps$5,
    emits: ['beforeLoad', 'ready', 'destroyed', 'drawEvt', 'editorEvt', 'mouseEvt'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawingRectangle';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const polylines = ref([]);
        const drawStatus = ref(DrawStatus.BeforeDraw);
        const canShowDrawTip = ref(false);
        const drawTipPosition = ref([0, 0, 0]);
        const drawTip = ref('');
        const showEditor = ref(false);
        const editorPosition = ref([0, 0, 0]);
        const mouseoverPoint = ref(null);
        const editingPoint = ref(null);
        const primitiveCollectionRef = ref(null);
        let restorePosition = undefined;
        const drawName = 'rectangle';
        let editorType = '';
        const polylinesRender = computed(() => {
            const results = [];
            const { Cartographic, Rectangle, Cartesian3 } = Cesium;
            const { viewer } = $services;
            polylines.value.forEach(polylineSegment => {
                const startPosition = polylineSegment.positions[0];
                const endPosition = polylineSegment.positions[1];
                if (Cartesian3.equals(startPosition, endPosition)) {
                    return;
                }
                const startCartographic = Cartographic.fromCartesian(startPosition, viewer.scene.globe.ellipsoid);
                const endCartographic = Cartographic.fromCartesian(endPosition, viewer.scene.globe.ellipsoid);
                const height = startCartographic.height;
                !props.clampToGround && (endCartographic.height = height);
                const rectangle = Rectangle.fromCartesianArray(polylineSegment.positions, viewer.scene.globe.ellipsoid);
                const arr = [rectangle.west, rectangle.north, height, rectangle.east, rectangle.north, height, rectangle.east,
                    rectangle.south, height, rectangle.west, rectangle.south, height, rectangle.west, rectangle.north, height];
                const positions = Cartesian3.fromRadiansArrayHeights(arr, viewer.scene.globe.ellipsoid);
                const polyline = Object.assign(Object.assign({}, polylineSegment), { polygonPositions: positions, height: height });
                results.push(polyline);
            });
            return results;
        });
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            return primitiveCollectionRef;
        });
        const startNew = () => {
            const polyline = {
                positions: [],
                polygonPositions: [],
                show: false,
                drawStatus: DrawStatus.BeforeDraw
            };
            polylines.value.push(polyline);
            drawStatus.value = DrawStatus.BeforeDraw;
            canShowDrawTip.value = true;
            drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
        };
        const stop = () => {
            if (drawStatus.value === DrawStatus.Drawing) {
                polylines.value.pop();
            }
            drawStatus.value = DrawStatus.BeforeDraw;
            canShowDrawTip.value = false;
            drawTipPosition.value = [0, 0, 0];
        };
        const handleMouseClick = (movement, options) => {
            const { viewer, drawingVm: drawingVm, selectedDrawingOption, getWorldPosition } = $services;
            if (options.button === 2 && options.ctrl) {
                const drawingsOption = drawingVm.proxy.drawingsOptions.find(v => v.name === drawName);
                drawingVm.proxy.toggleAction(drawingsOption);
                return;
            }
            if (drawStatus.value === DrawStatus.AfterDraw) {
                startNew();
            }
            const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : polylines.value.length - 1;
            const polyline = polylines.value[index];
            const positions = polyline.positions;
            if (options.button === 2 && editingPoint.value) {
                drawingVm.proxy.editingDrawingName = undefined;
                polyline.positions[editingPoint.value._index] = restorePosition;
                drawStatus.value = DrawStatus.AfterDraw;
                polyline.drawStatus = DrawStatus.AfterDraw;
                editingPoint.value = undefined;
                drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
                return;
            }
            if (options.button !== 0) {
                return;
            }
            const { defined } = Cesium;
            let type = 'new';
            if (drawStatus.value === DrawStatus.BeforeDraw) {
                const scene = viewer.scene;
                const position = getWorldPosition(scene, movement, {});
                if (!defined(position)) {
                    return;
                }
                positions[0] = position;
                positions[1] = position;
                polyline.show = true;
                drawStatus.value = DrawStatus.Drawing;
                polyline.drawStatus = DrawStatus.Drawing;
                drawTip.value = props.drawtip.drawTip2 || t(`vc.drawing.${drawName}.drawTip2`);
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: false,
                        position: position,
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
            else {
                polyline.drawStatus = DrawStatus.AfterDraw;
                drawStatus.value = DrawStatus.AfterDraw;
                if (editingPoint.value) {
                    editingPoint.value = undefined;
                    drawingVm.proxy.editingDrawingName = undefined;
                    canShowDrawTip.value = false;
                    drawTipPosition.value = [0, 0, 0];
                    type = editorType;
                }
                else {
                    if (props.mode === 1) {
                        drawingVm.proxy.toggleAction(selectedDrawingOption);
                    }
                }
                if (selectedDrawingOption) {
                    drawTip.value = props.drawtip.drawTip1 || t(`vc.drawing.${drawName}.drawTip1`);
                    canShowDrawTip.value = true;
                }
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: true,
                        position: polyline.positions[1],
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
        };
        const handleMouseMove = movement => {
            if (!canShowDrawTip.value) {
                return;
            }
            const { viewer, getWorldPosition } = $services;
            const scene = viewer.scene;
            const position = getWorldPosition(scene, movement, {});
            const { defined, Cartographic } = Cesium;
            if (!defined(position)) {
                return;
            }
            drawTipPosition.value = position;
            if (drawStatus.value !== DrawStatus.Drawing) {
                return;
            }
            if (defined(position)) {
                const index = editingPoint.value ? editingPoint.value._vcPolylineIndx : polylines.value.length - 1;
                const polyline = polylines.value[index];
                const positions = polyline.positions;
                const startPosition = positions[0];
                const startCartographic = Cartographic.fromCartesian(startPosition, viewer.scene.globe.ellipsoid);
                const endCartographic = Cartographic.fromCartesian(position, viewer.scene.globe.ellipsoid);
                !props.clampToGround && (endCartographic.height = startCartographic.height);
                positions[editingPoint.value ? editingPoint.value._index : 1] = Cartographic.toCartesian(endCartographic, viewer.scene.globe.ellipsoid);
                const type = editingPoint.value ? editorType : 'new';
                nextTick(() => {
                    emit('drawEvt', Object.assign({
                        index: index,
                        polylines: polylines,
                        name: drawName,
                        finished: false,
                        position: polyline.positions[1],
                        windowPoistion: movement,
                        type: type
                    }, polylinesRender.value[index]), viewer);
                });
            }
        };
        const onMouseoverPoints = e => {
            const { drawingHandlerActive, viewer } = $services;
            if (props.editable && drawStatus.value !== DrawStatus.Drawing && drawingHandlerActive) {
                e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.5;
                mouseoverPoint.value = e.pickedFeature.primitive;
                editorPosition.value = e.pickedFeature.primitive.position;
                showEditor.value = true;
                canShowDrawTip.value = false;
                drawTipPosition.value = [0, 0, 0];
            }
            emit('mouseEvt', {
                type: e.type,
                target: e,
                name: drawName
            }, viewer);
        };
        const onMouseoutPoints = e => {
            const { viewer, selectedDrawingOption } = $services;
            if (props.editable) {
                if (!editingPoint.value && drawStatus.value !== DrawStatus.Drawing) {
                    e.pickedFeature.primitive.pixelSize = props.pointOpts.pixelSize * 1.0;
                    editorPosition.value = [0, 0, 0];
                    mouseoverPoint.value = undefined;
                    showEditor.value = false;
                }
                selectedDrawingOption && (canShowDrawTip.value = true);
            }
            emit('mouseEvt', {
                type: e.type,
                target: e,
                name: drawName
            }, viewer);
        };
        const onEditorClick = e => {
            editorPosition.value = [0, 0, 0];
            showEditor.value = false;
            if (!props.editable) {
                return;
            }
            editorType = e;
            const { viewer, drawingVm } = $services;
            if (e === 'move') {
                drawTip.value = props.drawtip.drawTip3 || t(`vc.drawing.${drawName}.drawTip3`);
                drawStatus.value = DrawStatus.Drawing;
                editingPoint.value = mouseoverPoint.value;
                restorePosition = polylines.value[editingPoint.value._vcPolylineIndx].positions[editingPoint.value._index];
                canShowDrawTip.value = true;
                drawingVm.proxy.editingDrawingName = drawName;
            }
            else if (e === 'remove') {
                const index = mouseoverPoint.value._vcPolylineIndx;
                const polyline = polylines.value[index];
                polyline.positions.splice(mouseoverPoint.value._index, 1);
            }
            else if (e === 'removeAll') {
                const index = mouseoverPoint.value._vcPolylineIndx;
                polylines.value.splice(index, 1);
            }
            emit('editorEvt', {
                type: e,
                polylines: polylines,
                name: drawName,
                index: mouseoverPoint.value._vcPolylineIndx
            }, viewer);
        };
        const clear = () => {
            polylines.value = [];
            stop();
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawingRegularPolygon';
        };
        const publicMethods = { polylines, polylinesRender, startNew, stop, clear, handleMouseClick, handleMouseMove };
        Object.assign(instance.proxy, publicMethods);
        return () => {
            var _a;
            const { PolylineMaterialAppearance, MaterialAppearance, createGuid } = Cesium;
            const polylineOpts = Object.assign(Object.assign({}, props.polylineOpts), { vertexFormat: PolylineMaterialAppearance.VERTEX_FORMAT });
            props.clampToGround && delete polylineOpts.arcType;
            const children = [];
            polylinesRender.value.forEach((polyline, index) => {
                if (polyline.polygonPositions.length > 1) {
                    const positions = polyline.polygonPositions.slice();
                    positions.push(positions[0]);
                    children.push(h(props.clampToGround ? VcPrimitiveGroundPolyline : VcPrimitive, {
                        show: polyline.show && polylineOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.material)
                        }),
                        depthFailAppearance: new PolylineMaterialAppearance({
                            material: makeMaterial.call(instance, props.polylineOpts.depthFailMaterial)
                        }),
                        asynchronous: false,
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid()
                    }, () => h(props.clampToGround ? VcGeometryPolylineGround : VcGeometryPolyline, Object.assign({ positions: positions }, polylineOpts)))));
                }
                children.push(h(VcCollectionPoint, {
                    enableMouseEvent: props.enableMouseEvent,
                    show: polyline.show,
                    points: polyline.positions.map(position => (Object.assign(Object.assign({ position: position, id: createGuid(), _vcPolylineIndx: index }, props.pointOpts), { show: props.pointOpts.show || props.editable || polyline.drawStatus === DrawStatus.Drawing }))),
                    onMouseover: onMouseoverPoints,
                    onMouseout: onMouseoutPoints
                }));
                if (polyline.polygonPositions.length > 2) {
                    children.push(h(props.clampToGround ? VcPrimitiveGround : VcPrimitive, {
                        show: polyline.show && props.polygonOpts.show,
                        enableMouseEvent: props.enableMouseEvent,
                        appearance: new MaterialAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.material),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        depthFailAppearance: new MaterialAppearance({
                            material: makeMaterial.call(instance, props.polygonOpts.depthFailMaterial),
                            renderState: {
                                cull: {
                                    enabled: false
                                }
                            }
                        }),
                        asynchronous: false,
                    }, () => h(VcInstanceGeometry, {
                        id: createGuid(),
                    }, () => h(VcGeometryPolygon, Object.assign({ polygonHierarchy: polyline.polygonPositions }, props.polygonOpts)))));
                }
            });
            if (props.drawtip.show && canShowDrawTip.value) {
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: drawTipPosition.value,
                    pixelOffset: props.drawtip.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-drawtip vc-tooltip--style'
                }, drawTip.value)));
            }
            if (showEditor.value) {
                const buttons = [];
                if (mouseoverPoint.value) {
                    const editorOpts = props.editorOpts;
                    for (const key in editorOpts) {
                        if (!Array.isArray(editorOpts[key])) {
                            const opts = Object.assign({}, editorOpts[key]);
                            delete opts.color;
                            buttons.push(h(VcBtn, Object.assign(Object.assign({ style: { color: editorOpts[key].color, background: editorOpts[key].background } }, opts), { onclick: onEditorClick.bind(undefined, key) }), () => h(VcTooltip, Object.assign({}, editorOpts[key].tooltip), () => { var _a; return h('strong', null, ((_a = editorOpts[key].tooltip) === null || _a === void 0 ? void 0 : _a.tip) || t(`vc.drawing.editor.${key}`)); })));
                        }
                    }
                }
                const { viewer } = $services;
                children.push(h(VcOverlayHtml, {
                    position: editorPosition.value,
                    pixelOffset: (_a = props.editorOpts) === null || _a === void 0 ? void 0 : _a.pixelOffset,
                    teleport: {
                        to: viewer.container
                    }
                }, () => h('div', {
                    class: 'vc-editor'
                }, buttons)));
            }
            return h(VcCollectionPrimitive, {
                ref: primitiveCollectionRef,
                show: props.show,
                onReady: onPrimitiveCollectionReady
            }, () => children);
        };
    }
});

var VcDrawings = defineComponent({
    name: 'VcDrawings',
    props: defaultProps,
    emits: ['beforeLoad', 'ready', 'destroyed', 'activeEvt', 'editorEvt', 'drawEvt', 'mouseEvt', 'fabUpdated'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'VcDrawings';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        const canRender = ref(false);
        const containerStyle = reactive({});
        const positionState = usePosition(props, $services);
        const containerRef = ref(null);
        const fabRef = ref(null);
        let selectedDrawingOption = undefined;
        const fabExtanded = ref(false);
        const mounted = ref(false);
        const primitiveCollection = ref(null);
        let visibilityState = void 0;
        const options = {};
        const mainFabOpts = reactive(Object.assign({}, defaultOptions.mainFabOpts, props.mainFabOpts));
        const pointActionOpts = reactive(Object.assign({}, defaultOptions.pointActionOpts, props.pointActionOpts));
        const pointDrawingOpts = reactive(Object.assign({}, defaultOptions.pointDrawingOpts, props.pointDrawingOpts));
        const polylineActionOpts = reactive(Object.assign({}, defaultOptions.polylineActionOpts, props.polylineActionOpts));
        const polylineDrawingOpts = reactive(Object.assign({}, defaultOptions.polylineDrawingOpts, props.polylineDrawingOpts));
        const polygonActionOpts = reactive(Object.assign({}, defaultOptions.polygonActionOpts, props.polygonActionOpts));
        const polygonDrawingOpts = reactive(Object.assign({}, defaultOptions.polygonDrawingOpts, props.polygonDrawingOpts));
        const rectangleActionOpts = reactive(Object.assign({}, defaultOptions.rectangleActionOpts, props.rectangleActionOpts));
        const rectangleDrawingOpts = reactive(Object.assign({}, defaultOptions.rectangleDrawingOpts, props.rectangleDrawingOpts));
        const circleActionOpts = reactive(Object.assign({}, defaultOptions.circleActionOpts, props.circleActionOpts));
        const circleDrawingOpts = reactive(Object.assign({}, defaultOptions.circleDrawingOpts, props.circleDrawingOpts));
        const clearActionOpts = reactive(Object.assign({}, defaultOptions.clearActionOpts, props.clearActionOpts));
        options.pointActionOpts = pointActionOpts;
        options.pointDrawingOpts = pointDrawingOpts;
        options.polylineActionOpts = polylineActionOpts;
        options.polylineDrawingOpts = polylineDrawingOpts;
        options.polygonActionOpts = polygonActionOpts;
        options.polygonDrawingOpts = polygonDrawingOpts;
        options.rectangleActionOpts = rectangleActionOpts;
        options.rectangleDrawingOpts = rectangleDrawingOpts;
        options.circleActionOpts = circleActionOpts;
        options.circleDrawingOpts = circleDrawingOpts;
        options.clearActionOpts = clearActionOpts;
        const drawingsOptions = props.drawings.map(drawing => ({
            name: drawing,
            actionRef: ref(null),
            actionOpts: options[`${camelize(drawing)}ActionOpts`],
            drawingRef: ref(null),
            drawingOpts: options[`${camelize(drawing)}DrawingOpts`],
            actionStyle: {
                background: options[`${camelize(drawing)}ActionOpts`].color,
                color: options[`${camelize(drawing)}ActionOpts`].textColor
            },
            actionClass: `vc-draw-${drawing} vc-draw-button${drawing === (selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.name) ? ' active' : ''}`,
            tip: options[`${camelize(drawing)}ActionOpts`].tooltip.tip || t(`vc.drawing.${camelize(drawing)}.tip`),
            cmp: getDrawingCmp(drawing),
            isActive: false
        }));
        const handleMouseClick = (movement, options) => {
            var _a, _b;
            const drawingCmp = ((selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef.value) || (selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef));
            (_a = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleMouseClick) === null || _a === void 0 ? void 0 : _a.call(drawingCmp, movement.position, options);
            let drawingOptions;
            if (instance.proxy.editingDrawingName) {
                drawingOptions = drawingsOptions.find(v => v.name === instance.proxy.editingDrawingName);
            }
            if (drawingOptions !== selectedDrawingOption) {
                const drawingCmp = ((drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef.value) || (drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef));
                (_b = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleMouseClick) === null || _b === void 0 ? void 0 : _b.call(drawingCmp, movement.position, options);
            }
        };
        const handleMouseMove = (movement, options) => {
            var _a, _b;
            const drawingCmp = ((selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef.value) || (selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef));
            (_a = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleMouseMove) === null || _a === void 0 ? void 0 : _a.call(drawingCmp, movement.endPosition, options);
            let drawingOptions;
            if (instance.proxy.editingDrawingName) {
                drawingOptions = drawingsOptions.find(v => v.name === instance.proxy.editingDrawingName);
            }
            if (drawingOptions !== selectedDrawingOption) {
                const drawingCmp = ((drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef.value) || (drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef));
                (_b = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleMouseMove) === null || _b === void 0 ? void 0 : _b.call(drawingCmp, movement.endPosition, options);
            }
        };
        const handleDoubleClick = (movement, options) => {
            var _a, _b;
            const drawingCmp = ((selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef.value) || (selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.drawingRef));
            (_a = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleDoubleClick) === null || _a === void 0 ? void 0 : _a.call(drawingCmp, movement.position, options);
            let drawingOptions;
            if (instance.proxy.editingDrawingName) {
                drawingOptions = drawingsOptions.find(v => v.name === instance.proxy.editingDrawingName);
            }
            if (drawingOptions !== selectedDrawingOption) {
                const drawingCmp = ((drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef.value) || (drawingOptions === null || drawingOptions === void 0 ? void 0 : drawingOptions.drawingRef));
                (_b = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.handleDoubleClick) === null || _b === void 0 ? void 0 : _b.call(drawingCmp, movement.position, options);
            }
        };
        const { activate, deactivate, destroy: destroyHandler, isActive } = useHandler($services, {
            handleMouseClick,
            handleMouseMove,
            handleDoubleClick
        });
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            canRender.value = true;
            visibilityState = new VisibilityState();
            return drawingsOptions;
        });
        instance.mount = () => __awaiter(this, void 0, void 0, function* () {
            updateRootStyle();
            mounted.value = true;
            nextTick(() => {
                var _a;
                mainFabOpts.autoExpand && ((_a = fabRef.value) === null || _a === void 0 ? void 0 : _a.toggle());
            });
            activate();
            return true;
        });
        instance.unmount = () => __awaiter(this, void 0, void 0, function* () {
            if (selectedDrawingOption) {
                toggleAction(selectedDrawingOption);
                selectedDrawingOption = undefined;
            }
            deactivate();
            destroyHandler();
            mounted.value = false;
            return true;
        });
        function getDrawingCmp(name) {
            switch (name) {
                case 'point':
                    return VcDrawingPoint;
                case 'polyline':
                    return VcDrawingPolyline;
                case 'polygon':
                    return VcDrawingPolygon;
                case 'rectangle':
                    if (rectangleDrawingOpts.regular) {
                        return VcDrawingRegularPolygon;
                    }
                    else {
                        return VcDrawingRectangle;
                    }
                case 'circle':
                    return VcDrawingRegularPolygon;
                default:
                    return void 0;
            }
        }
        const getWorldPosition = (scene, windowPosition, result) => {
            const { Cesium3DTileFeature, Cesium3DTileset, Cartesian3, defined, Model, Ray } = Cesium;
            let position = void 0;
            const cartesianScratch = {};
            const rayScratch = new Ray();
            if (scene.pickPositionSupported) {
                visibilityState.hide(scene);
                const pickObj = scene.pick(windowPosition, 1, 1);
                visibilityState.restore(scene);
                if (defined(pickObj)) {
                    if (pickObj instanceof Cesium3DTileFeature || pickObj.primitive instanceof Cesium3DTileset || pickObj.primitive instanceof Model) {
                        position = scene.pickPosition(windowPosition, cartesianScratch);
                        if (Cesium.defined(position)) {
                            return Cartesian3.clone(position, result);
                        }
                    }
                }
            }
            if (defined(scene.globe)) {
                const ray = scene.camera.getPickRay(windowPosition, rayScratch);
                position = scene.globe.pick(ray, scene, cartesianScratch);
                return defined(position) ? Cartesian3.clone(position, result) : void 0;
            }
            return void 0;
        };
        const updateRootStyle = () => {
            var _a;
            const css = positionState.style.value;
            containerStyle.left = css.left;
            containerStyle.top = css.top;
            containerStyle.transform = css.transform;
            const side = positionState.attach.value;
            const fabTarget = (_a = $(fabRef)) === null || _a === void 0 ? void 0 : _a.$el;
            if (fabTarget !== void 0) {
                const clientRect = fabTarget.getBoundingClientRect();
                css.width = `${clientRect.width}px`;
                css.height = `${clientRect.height}px`;
                if ((side.bottom || side.top) && !side.left && !side.right) {
                    css.left = '50%';
                    css.transform = 'translate(-50%, 0)';
                }
                if ((side.left || side.right) && !side.top && !side.bottom) {
                    css.top = '50%';
                    css.transform = 'translate(0, -50%)';
                }
            }
            Object.assign(containerStyle, css);
        };
        const restoreColor = ref(null);
        const toggleAction = (drawingOption) => {
            var _a;
            const { viewer } = $services;
            if (isString(drawingOption)) {
                drawingOption = drawingsOptions.find(v => v.name === drawingOption);
            }
            if (!drawingOption) {
                commonState.logger.error('Invalid drawingOption or drawingOption name');
            }
            if (selectedDrawingOption !== void 0) {
                selectedDrawingOption.actionOpts.color = restoreColor.value;
                const drawingCmp = (selectedDrawingOption.drawingRef.value || selectedDrawingOption.drawingRef);
                (_a = drawingCmp === null || drawingCmp === void 0 ? void 0 : drawingCmp.stop) === null || _a === void 0 ? void 0 : _a.call(drawingCmp);
                selectedDrawingOption.isActive = false;
                emit('activeEvt', {
                    type: selectedDrawingOption.name,
                    option: selectedDrawingOption,
                    isActive: false
                }, viewer);
            }
            if ((selectedDrawingOption === null || selectedDrawingOption === void 0 ? void 0 : selectedDrawingOption.name) === drawingOption.name) {
                selectedDrawingOption = undefined;
                drawingOption.actionOpts.color = restoreColor.value;
            }
            else {
                selectedDrawingOption = drawingOption;
                const drawingCmp = (selectedDrawingOption.drawingRef.value || selectedDrawingOption.drawingRef);
                drawingCmp.startNew();
                restoreColor.value = selectedDrawingOption.actionOpts.color;
                selectedDrawingOption.actionOpts.color = props.activeColor;
                selectedDrawingOption.isActive = true;
                emit('activeEvt', {
                    type: selectedDrawingOption.name,
                    option: selectedDrawingOption,
                    isActive: true
                }, viewer);
            }
        };
        const onUpdateFab = value => {
            fabExtanded.value = value;
            if (value) {
                activate();
            }
            else {
                if (selectedDrawingOption) {
                    toggleAction(selectedDrawingOption);
                }
                deactivate();
            }
            emit('fabUpdated', value);
        };
        const clearAll = () => {
            drawingsOptions.forEach(drawingsOption => {
                var _a;
                (_a = drawingsOption.drawingRef.value) === null || _a === void 0 ? void 0 : _a.clear();
            });
            selectedDrawingOption && toggleAction(selectedDrawingOption);
        };
        const getServices = () => {
            return mergeDescriptors(commonState.getServices(), {
                get drawingVm() {
                    return instance;
                },
                get selectedDrawingOption() {
                    return selectedDrawingOption;
                },
                get getWorldPosition() {
                    return getWorldPosition;
                },
                get drawingHandlerActive() {
                    return isActive;
                }
            });
        };
        const onPrimitiveCollectionReady = ({ cesiumObject }) => {
            cesiumObject._vcId = 'VcDrawings';
        };
        provide(vcKey, getServices());
        Object.assign(instance.proxy, { drawingsOptions, clearAll, deactivate, activate, toggleAction });
        return () => {
            if (canRender.value) {
                const fabActionChildren = [];
                const drawingChildren = [];
                drawingsOptions.forEach(drawingOptions => {
                    const drawing = camelize(drawingOptions.name);
                    if (options[`${drawing}ActionOpts`]) {
                        fabActionChildren.push(h(VcFabAction, Object.assign(Object.assign({ ref: drawingOptions.actionRef, style: drawingOptions.actionStyle, class: drawingOptions.actionClass }, drawingOptions.actionOpts), { onClick: () => {
                                toggleAction(drawingOptions);
                            } }), () => h(VcTooltip, Object.assign({}, drawingOptions.actionOpts.tooltip), () => h('strong', null, drawingOptions.tip))));
                        drawingOptions.cmp && drawingChildren.push(h(drawingOptions.cmp, Object.assign({ ref: drawingOptions.drawingRef, editable: props.editable, clampToGround: props.clampToGround, mode: props.mode, onDrawEvt: (e, viewer) => {
                                emit('drawEvt', e, viewer);
                            }, onEditorEvt: (e, viewer) => {
                                emit('editorEvt', e, viewer);
                            }, onMouseEvt: (e, viewer) => {
                                emit('mouseEvt', e, viewer);
                            } }, drawingOptions.drawingOpts)));
                    }
                });
                drawingsOptions.length && fabActionChildren.push(h(VcFabAction, Object.assign(Object.assign({ style: {
                        background: clearActionOpts.color,
                        color: clearActionOpts.textColor
                    }, class: 'vc-draw-button vc-draw-clear' }, clearActionOpts), { onClick: clearAll }), () => h(VcTooltip, Object.assign({}, clearActionOpts.tooltip), () => h('strong', null, clearActionOpts.tooltip.tip || t('vc.drawing.clear.tip')))));
                const root = [];
                if (mounted.value) {
                    root.push(h('div', {
                        ref: containerRef,
                        class: 'vc-drawings-container ' + positionState.classes.value,
                        style: containerStyle
                    }, ctx.slots.body !== void 0 ? ctx.slots.body() : h(VcFab, Object.assign({ ref: fabRef, class: 'vc-draw-button', style: {
                            background: mainFabOpts.color,
                            color: mainFabOpts.textColor
                        }, 'onUpdate:modelValue': onUpdateFab }, mainFabOpts), {
                        default: () => fabActionChildren,
                        tooltip: () => h(VcTooltip, Object.assign({}, mainFabOpts.tooltip), () => h('strong', null, mainFabOpts.tooltip.tip || (fabExtanded.value ? t('vc.drawing.collapse') : t('vc.drawing.expand'))))
                    })));
                }
                root.push(h(VcCollectionPrimitive, {
                    ref: primitiveCollection,
                    show: props.show,
                    onReady: onPrimitiveCollectionReady
                }, () => drawingChildren));
                return root;
            }
            else {
                return createCommentVNode('v-if');
            }
        };
    }
});

VcDrawings.install = (app) => {
    app.component(VcDrawings.name, VcDrawings);
};

export default VcDrawings;
