var _gaq = _gaq || [];

function scope() {
    var J = chrome.runtime.getManifest().version;
    var e = "11/22/2017";
    _gaq.push(["_setAccount", "UA-64913318-2"]);
    _gaq.push(["_trackPageview"]);
    _gaq.push(["_trackEvent", "backgroundOpen", J]);
    var U = "VERSION";
    var i = "PRESETS";
    var a = "PRESETS.";
    var s = {
        get: function(e, t) {
            t({})
        },
        set: function(e, t) {
            t()
        },
        remove: function(e, t) {
            t()
        }
    };
    var W = null;
    var t = "ears_pro";
    var n = "ears_pro_1";
    var r = "ACTIVE";
    localStorage[" "] = " ";
    s = chrome.storage.sync;
    W = null;
    var o = false;

    function D(e) {
        if (o) {
            console.log(e)
        }
    }(function() {
        var e = document.createElement("script");
        e.type = "text/javascript";
        e.async = true;
        e.src = "https://ssl.google-analytics.com/ga.js";
        var t = document.getElementsByTagName("script")[0];
        t.parentNode.insertBefore(e, t)
    })();
    var M = null;
    var B = null;
    var G = null;
    var Q = null;
    var L = null;
    var V = [];
    var z = [20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240, 20480];
    var H = [.7071, .7071, .7071, .7071, .7071, .7071, .7071, .7071, .7071, .7071, .7071];
    var K = 11;
    var X = function() {};
    var Y = {};
    var Z = false;
    var $;

    function l(e) {
        return a + e
    }

    function ee(n) {
        if (!localStorage[i]) {
            localStorage[i] = JSON.stringify({})
        }
        var r = JSON.parse(localStorage[i]);
        if (r == null || typeof r != "object") {
            r = {}
        }
        s.get(null, function(e) {
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError)
            }
            for (var t in e) {
                if (t.startsWith(a)) {
                    r[t.slice(a.length)] = e[t]
                }
            }
            n(r)
        })
    }

    function te(e, t, n) {
        var r = JSON.parse(localStorage[i]);
        if (r == null || typeof r != "object") {
            r = {}
        }
        var a = Object.keys(r).length;
        r[e] = t;
        var o = Object.keys(r).length;
        if (W && Object.keys(r) > W && o >= a) {
            n();
            return
        }
        localStorage[i] = JSON.stringify(r);
        syncPresets = {};
        syncPresets[l(e)] = t;
        s.set(syncPresets, function() {
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError)
            }
            n()
        })
    }

    function ne(e, t) {
        _gaq.push(["_trackEvent", "deletePreset", e.preset]);
        var n = JSON.parse(localStorage[i]);
        if (n == null || typeof n != "object") {
            n = {}
        }
        delete n[e.preset];
        localStorage[i] = JSON.stringify(n);
        s.remove(l(e.preset), t)
    }
    chrome.storage.onChanged.addListener(function(e, t) {});

    function c() {
        if (Z) {
            M.close();
            M = null;
            Z = false;
            chrome.runtime.onMessage.removeListener($);
            var e = Object.keys(Y);
            for (var t = 0; t < e.length; t++) {
                var n = e[t];
                Y[n].stream.getTracks()[0].stop();
                delete Y[n]
            }
        }
    }

    function re() {
        return new AudioContext({
            latencyHint: "playback"
        })
    }

    function ae() {
        if (Z) {
            M.close();
            M = null;
            Z = false;
            Q = null;
            L = null;
            var e = Object.values(Y);
            Y = {};
            for (var t = 0; t < e.length; t++) {
                var n = e[t].audioSource;
                n.disconnect(B)
            }
            u(e)
        }
    }

    function u(e) {
        var t = null;
        if (Z) {
            return
        }
        Z = true;
        M = re();
        M.suspend();
        console.log("starting sampleRate: " + M.sampleRate);
        var n = false;
        X = function() {
            if (n) {
                return
            }
            var e = re();
            var t = M.sampleRate && e.sampleRate && M.sampleRate != e.sampleRate;
            if (t) {
                console.log(e);
                console.log(M);
                n = true
            }
            e.close();
            if (t) {
                console.log("sampleRate changed... reloading AudioContext");
                ae()
            }
        };
        B = M.createGain();
        B.gain.value = 1;
        G = M.createGain();
        G.gain.value = m();
        if (t) {
            var r = M.createScriptProcessor(t);
            r.onaudioprocess = function(e) {
                var t = e.inputBuffer;
                var n = e.outputBuffer;
                for (var r = 0; r < n.numberOfChannels; r++) {
                    n.copyToChannel(t.getChannelData(r), r)
                }
            };
            G.connect(r);
            r.connect(M.destination)
        } else {
            G.connect(M.destination)
        }
        if (!localStorage[U]) {
            localStorage[U] = "0.0.0"
        }
        if (localStorage[" "] == " ") {
            if (localStorage["sync"] != " ") {
                ee(function(e) {
                    var t = Object.keys(e);
                    for (var n = 0; n < t.length; n++) {
                        var r = t[n];
                        te(r, e[r], function() {})
                    }
                });
                localStorage["sync"] = " "
            }
        }
        localStorage[U] = J;
        V = [];
        var a = {};

        function o() {
            var e = G;
            var t = false;
            for (var n = V.length - 1; n >= 0; n--) {
                if (V[n].gain.value != 0) {
                    if (!a[n]) {
                        t = true
                    }
                    if (t) {
                        D(n + " -> " + e.idx);
                        V[n].disconnect();
                        V[n].connect(e)
                    }
                    if (a[n]) {
                        t = false
                    }
                    a[n] = true;
                    e = V[n];
                    e.idx = n
                } else {
                    if (a[n]) {
                        a[n] = false;
                        t = true
                    }
                }
            }
            if (t) {
                D("preGain -> " + e.idx);
                B.disconnect();
                B.connect(e)
            }
        }
        for (var i = 0; i < K; i++) {
            var s = M.createBiquadFilter();
            if (i == 0) {
                s.type = "lowshelf"
            } else if (i == K - 1) {
                s.type = "highshelf"
            } else {
                s.type = "peaking"
            }
            var l = "filter" + i;
            var c = localStorage[l];
            if (c) {
                c = JSON.parse(c);
                s.frequency.value = c.f;
                s.gain.value = c.g;
                s.Q.value = c.q
            } else {
                s.frequency.value = z[i];
                s.gain.value = 0;
                s.Q.value = H[i]
            }
            V.push(s);
            localStorage[l] = JSON.stringify({
                f: s.frequency.value,
                g: s.gain.value,
                q: s.Q.value
            })
        }
        B.connect(G);

        function u(e) {
            f = V[e.index];
            f.gain.value = v(e.gain);
            f.frequency.value = p(e.frequency);
            f.Q.value = d(e.q);
            var t = "filter" + e.index;
            localStorage[t] = JSON.stringify({
                f: f.frequency.value,
                g: f.gain.value,
                q: f.Q.value
            });
            o()
        }

        function g(e) {
            if (e > 10) {
                return 10
            }
            if (e < .00316) {
                return .00316
            }
            return e
        }

        function v(e) {
            if (e < -30) {
                return -30
            }
            if (e > 30) {
                return 30
            }
            return e
        }

        function p(e) {
            if (e < 5) {
                return 5
            }
            if (e > 2e4) {
                return 2e4
            }
            return e
        }

        function d(e) {
            if (e < .2) {
                e = .2
            }
            if (e > 11) {
                e = 11
            }
            return e
        }

        function y(e) {
            var t = g(e.gain);
            G.gain.value = g(t);
            h(t)
        }

        function m() {
            if (!localStorage["GAIN"]) {
                localStorage["GAIN"] = JSON.stringify(1)
            }
            return JSON.parse(localStorage["GAIN"])
        }

        function h(e) {
            localStorage["GAIN"] = JSON.stringify(e)
        }

        function S(e) {
            _gaq.push(["_trackEvent", "preset", "save"]);
            var t = [];
            var n = [];
            var r = [];
            for (var a = 0; a < V.length; a++) {
                var o = V[a];
                t.push(o.frequency.value);
                n.push(o.gain.value);
                r.push(o.Q.value)
            }
            var i = {
                frequencies: t,
                gains: n,
                qs: r
            };
            te(e.preset, i, E)
        }