<!-- webview only -->
<template>
    <section class="bl___webview-container"></section>
</template>

<script>
const parseDataset = function (dataset) {
    let result = {};
    for (let key in dataset) {
        try {
            result[key] = JSON.parse(dataset[key]);
        } catch (e) {
            result[key] = dataset[key];
        }
    }
    return result;
};

export default {
    name: 'Bili_WebView',
    props: {
        src: {
            type: String,
            default: ''
        }
    },
    watch: {
        src(value) {
            this.postMessage();
        },
    },
    mounted() {
        this.postMessage();
    },
    methods: {
        postMessage() {
            const options = {
                name: 'webview',
                id: this._uid,
                destroy: false,
                src: this.src
            };

            SmallApp.nativeComponent(options);
        },
        handleMessage(message) {
            if (message.type === 'native-component') {
                const elementInfo = { id: this.$el.id, dataset: parseDataset(this.$el.dataset) };
                try {
                    if (message.event.type === 'load') {
                        const accountInfo = bl.getAccountInfoSync()
                        const launchOptions = bl.getLaunchOptionsSync()
                        bl && bl.internal && bl.internal.reportNeuron({
                            event: 'mall.miniapp-window.webview.url.show',
                            type: 3,
                            params: {
                                url: message.event.detail.src,
                                appid: accountInfo.miniProgram.appId,
                                vappid: accountInfo.miniProgram.virtualId,
                                buildtype: launchOptions.envVersion
                            }
                        });
                    }
                } catch (error) { 
                }
                this.$emit(message.event.type, Object.assign({ target: elementInfo, currentTarget: elementInfo }, message.event));
            }
        }
    },
    destroyed() {
        const options = {
            name: 'webview',
            id: this._uid,
            destroy: true
        };

        SmallApp.nativeComponent(options);
    }
}
</script>

<style>
</style>
