class AccelaSearchService {
    constructor() {
        this.readyScripts = []
        this.apiUrl = 'https://svc11.accelasearch.io'
        this.layerUrl = 'https://layer.accelasearch.io/versions/1.10.48'
    }
    addCss(name, version = 0) {
        var link = document.createElement('link')
        link.href = this.layerUrl + '/static/css/' + name + '.css?version=' + version
        if (name === 'shopify') {
            link.href = this.apiUrl + '/static/css/' + name + '.css'
        }
        link.type = 'text/css'
        link.rel = 'stylesheet'
        link.media = 'screen'
        document.head.appendChild(link)
        return this
    }
    addScript(name, version = 0) {
        var self = this
        var script = document.createElement('script')
        script.src = this.layerUrl + '/static/js/' + name + '.js?version=' + version
        script.onload = () => self.onScriptLoad(name)
        document.body.appendChild(script)
        return this
    }
    waitForDom() {
        var desktopSelector = '[name=' + window.AS_CONFIG_DATA.show.name.desktop + ']'
        if (window.AS_CONFIG_DATA.show.id && window.AS_CONFIG_DATA.show.id.desktop) {
            desktopSelector = '#' + window.AS_CONFIG_DATA.show.id.desktop
        }
        var mobileSelector = '[name=' + window.AS_CONFIG_DATA.show.name.mobile + ']'
        if (window.AS_CONFIG_DATA.show.id && window.AS_CONFIG_DATA.show.id.mobile) {
            mobileSelector = '#' + window.AS_CONFIG_DATA.show.id.mobile
        }
        return new Promise(resolve => {
            if (document.querySelector(desktopSelector)
            || document.querySelector(mobileSelector)) {
                return resolve()
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(desktopSelector)
                || document.querySelector(mobileSelector)) {
                    resolve()
                    observer.disconnect()
                }
            })

            observer.observe(document.body, {
                childList: true,
                subtree: true
            })
        })
    }
    async onScriptLoad(name) {
        this.readyScripts.push(name)
        if (this.readyScripts.length === 3) {
            await this.waitForDom()
            window.initLayer()
        }
        return this
    }
    loadConfiguration() {
        var self = this
        fetch(this.apiUrl + '/API/shops/b717030f678ab02286ea45ad7e7edfc7/configuration')
        .then(response => response.json())
        .then(function (config_data) {
            if (!config_data.isActive) {
                return
            }
            
            window.AS_CONFIG_DATA = config_data
            var version = config_data.version
            self.addScript('main', version).addScript('runtime-main', version).addScript('app', version)
                .addCss('app', version).addCss('main', version)
        })
        return this
    }
    init() {
        return this.loadConfiguration()
    }
}
var accelaSearchService = new AccelaSearchService()
accelaSearchService.init()