'use strict';

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

var vue = require('vue');
var composables = require('../composables');
var util = require('../utils/util');
var cesiumProps = require('../utils/cesium-props');
var vm = require('../utils/private/vm');
var mergeDescriptors = require('../utils/merge-descriptors');
var render = require('../utils/private/render');
var config = require('../utils/config');

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

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

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

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

var VcInstanceGeometry = vue.defineComponent({
    name: 'VcInstanceGeometry',
    props: Object.assign(Object.assign(Object.assign({ geometry: Object }, cesiumProps.modelMatrix), cesiumProps.id), { attributes: Object }),
    emits: ['beforeLoad', 'ready', 'destroyed', 'update:geometry'],
    setup(props, ctx) {
        const instance = vue.getCurrentInstance();
        instance.renderByParent = true;
        instance.cesiumClass = 'GeometryInstance';
        instance.cesiumEvents = [];
        const commonState = composables.useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { emit } = ctx;
        const vcIndex = vue.ref(0);
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = commonState.transformProps(props);
            if (!options.geometry) {
                options.geometry = new Cesium.Geometry({ attributes: new Cesium.GeometryAttributes() });
            }
            return new Cesium.GeometryInstance(options);
        });
        instance.mount = () => __awaiter(this, void 0, void 0, function* () {
            const parentVM = vm.getVcParentInstance(instance).proxy;
            vcIndex.value = parentVM.__childCount.value;
            parentVM.__childCount.value += 1;
            const geometryInstance = instance.cesiumObject;
            parentVM.__updateGeometryInstances(geometryInstance, vcIndex.value);
            return true;
        });
        const updateGeometry = geometry => {
            const listener = vm.getInstanceListener(instance, 'update:geometry');
            if (listener) {
                emit('update:geometry', geometry);
            }
            else {
                const geometryInstance = instance.cesiumObject;
                geometryInstance.geometry = geometry;
            }
            return true;
        };
        const getServices = () => {
            return mergeDescriptors.mergeDescriptors(commonState.getServices(), {
                get geometryInstance() {
                    return instance.cesiumObject;
                }
            });
        };
        vue.provide(config.vcKey, getServices());
        Object.assign(instance.proxy, {
            __updateGeometry: updateGeometry
        });
        return () => ctx.slots.default ? (vue.h('i', {
            class: util.kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, render.hSlot(ctx.slots.default))) : vue.createCommentVNode(util.kebabCase(instance.proxy.$options.name));
    }
});

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

exports.default = VcInstanceGeometry;
