import { defineComponent, getCurrentInstance, h, createCommentVNode } from 'vue';
import { hSlot } from '../utils/private/render';
import { useCommon } from '../composables';
import { rectangle, cutoutRectangle, colorToAlpha } from '../utils/cesium-props';
import { getInstanceListener } from '../utils/private/vm';
import { kebabCase, isUndefined } from '../utils/util';

/*! *****************************************************************************
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 defaultProps = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ imageryProvider: Object }, rectangle), { alpha: {
        type: Number,
        default: 1.0
    }, nightAlpha: {
        type: Number,
        default: 1.0
    }, dayAlpha: {
        type: Number,
        default: 1.0
    }, brightness: {
        type: Number,
        default: 1.0
    }, contrast: {
        type: Number,
        default: 1.0
    }, hue: {
        type: Number,
        default: 0.0
    }, saturation: {
        type: Number,
        default: 1.0
    }, gamma: {
        type: Number,
        default: 1.0
    }, splitDirection: Number, minificationFilter: Number, magnificationFilter: Number, show: {
        type: Boolean,
        default: true
    }, maximumAnisotropy: Number, minimumTerrainLevel: Number, maximumTerrainLevel: Number }), cutoutRectangle), colorToAlpha), { colorToAlphaThreshold: {
        type: Number,
        default: 0.004
    }, sortOrder: Number });

var ImageryLayer = defineComponent({
    name: 'VcLayerImagery',
    props: defaultProps,
    emits: ['beforeLoad', 'ready', 'destroyed', 'update:imageryProvider'],
    setup(props, ctx) {
        const instance = getCurrentInstance();
        instance.cesiumClass = 'ImageryLayer';
        instance.cesiumEvents = [];
        const commonState = useCommon(props, ctx, instance);
        if (commonState === void 0) {
            return;
        }
        const { $services } = commonState;
        const { emit } = ctx;
        instance.createCesiumObject = () => __awaiter(this, void 0, void 0, function* () {
            const options = commonState.transformProps(props);
            const imageryProvider = (props.imageryProvider || {});
            return new Cesium.ImageryLayer(imageryProvider, options);
        });
        instance.mount = () => __awaiter(this, void 0, void 0, function* () {
            const { viewer } = $services;
            const imageryLayer = instance.cesiumObject;
            imageryLayer.sortOrder = props.sortOrder;
            viewer.imageryLayers.add(imageryLayer);
            return !viewer.isDestroyed() && viewer.imageryLayers.contains(imageryLayer);
        });
        instance.unmount = () => __awaiter(this, void 0, void 0, function* () {
            const { viewer } = $services;
            const imageryLayer = instance.cesiumObject;
            return !viewer.isDestroyed() && viewer.imageryLayers.remove(imageryLayer);
        });
        const updateProvider = provider => {
            if (isUndefined(provider)) {
                return instance.unmount();
            }
            else {
                const imageryLayer = instance.cesiumObject;
                imageryLayer._imageryProvider = provider;
                const listener = getInstanceListener(instance, 'update:imageryProvider');
                if (listener)
                    emit('update:imageryProvider', provider);
            }
            return true;
        };
        Object.assign(instance.proxy, {
            __updateProvider: updateProvider
        });
        return () => ctx.slots.default ? (h('i', {
            class: kebabCase(instance.proxy.$options.name),
            style: { display: 'none !important' }
        }, hSlot(ctx.slots.default))) : createCommentVNode(kebabCase(instance.proxy.$options.name));
    }
});

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

export default ImageryLayer;
