/**
 * This module includes code available for both the public website and
 * administration interface)
 */
//-- vendor
import '@fortawesome/fontawesome-free/css/all.min.css';


//-- aircox
import App, {PlayerApp} from './app'
import Builder from './appBuilder'
import Sound from './sound'
import {Set} from './model'

import './assets/styles.scss'


window.aircox = {
    // main application
    builder: new Builder(App),
    get app() { return this.builder.app  },

    // player application
    playerBuilder: new Builder(PlayerApp),
    get playerApp() { return this.playerBuilder && this.playerBuilder.app },
    get player() { return this.playerBuilder.vm && this.playerBuilder.vm.$refs.player },

    Set, Sound,


    /**
     * Initialize main application and player.
     */
    init(props=null, {config=null, builder=null, initBuilder=true,
                      initPlayer=true, hotReload=false, el=null}={})
    {
        if(initPlayer) {
            let playerBuilder = this.playerBuilder
            playerBuilder.mount()
        }

        if(initBuilder) {
            builder = builder || this.builder
            this.builder = builder
            if(config || window.App)
                builder.config = config || window.App
            if(el)
                builder.config.el = el

            builder.title = document.title
            builder.mount({props})

            if(hotReload)
                builder.enableHotReload(hotReload)
        }
    },

    /**
     * Filter navbar dropdown menu items
     */
    filter_menu(event) {
        var filter = new RegExp(event.target.value, 'gi');
        var container = event.target.closest('.navbar-dropdown');

        if(event.target.value)
            for(let item of container.querySelectorAll('a.navbar-item'))
                item.style.display = item.innerHTML.search(filter) == -1 ? 'none' : null;
        else
            for(let item of container.querySelectorAll('a.navbar-item'))
                item.style.display = null;
    }
}
