<template>
    
    <v-app id="main" v-if="! app.configLoaded">
        <v-progress-linear 
            indeterminate 
            height="12" 
            striped
        >
        </v-progress-linear>
    </v-app>

    <application-booted v-else 
        :app="app"
    />

</template>

<script>

    import App from '@CsClasses/App/App';

    const Appconfig = require('APP/~config/~Appconfig/Appconfig');
    const Appevents = require('APP/~config/~Appevents/Appevents');

    export default {
        props: {
            page_payload: {default: null},
            translations: {default: () => {}},
        },

        data: () => ({
            app: null,
        }),

        methods: {
            ApplyConfig(object, Appconfig) {
                _.each(Appconfig, (value, key) => {
                    if(_.has(object, key))
                    {                        
                        if(_.isObject(value))
                        {
                            this.ApplyConfig(object[key], value);
                        }
                        else
                        {
                            if(_.isString(value) || _.isBoolean(value))
                            {
                                object[key] = value;
                            }
                        }
                    }
                })
            }
        },

        created() {
            this.app = new App(this);
            this.app.translations = {...this.translations};
            
            let i = setInterval( () => {
                if(this.app.configLoaded)
                {
                    clearInterval(i);

                    /** 
                     * this.app.platform ==> platforma: admin | b2b
                     * this.app.vue.$route.name ==> ruta
                     * this.app.user
                     */

                    if(this.app.platform == 'admin')
                    {
                        this.app.AddMenusByContext(require.context('APP/~config/menus/Admin', true, /\.menu.js$/i));
                    }
                    else
                    {
                        if(this.app.platform == 'b2b')
                        {
                            this.app.AddMenusByContext(require.context('APP/~config/menus/B2B', true, /\.menu.js$/i)); 
                        }
                    }

                    /** 
                     * Se aplica configurarile locale
                     */
                    this.ApplyConfig(this.app, Appconfig(this.app));

                    this.app.echo.RegisterEvents(Appevents(this.app));
                }
            }, 1);

        },

        components: {
            ApplicationBooted: require('@CsVueComp/app/ApplicationBooted').default,
        },

        name: 'Bootstrap',
    }
</script>