"use strict";
var filtersJSON = {
white: [],
black: []};
var cachedResults = Object.create(null);
function clearCachedResults() {
cachedResults = Object.create(null);
}

setInterval(function () {
clearCachedResults();
}
,3600000);
function checkURL(url) {
if (url in cachedResults)
return cachedResults[url];
var result = [null, null];
var blackList = filtersJSON.black;
for (var i = 0, length = blackList.length;i < length;i++) {
var el = blackList[i];
if (el.r.test(url))
{
result = [true, el.uri];
}

}

if (result[0] !== true)
{
return cachedResults[url] = result;
}

var whiteList = filtersJSON.white;
for (var i = 0, length = whiteList.length;i < length;i++) {
var el = whiteList[i];
if (el.r.test(url))
{
result = [false, null];
}

}

return cachedResults[url] = result;
}

function setFilters(json) {
filtersJSON = json;
["white", "black"].forEach(function (key) {
filtersJSON[key].forEach(function (regexpItem) {
try {
regexpItem.r = new RegExp(regexpItem.re, "i");
}
catch (e) {

}

}
);
}
);
}

onmessage = function onmessage(event) {
var name = event.data.name;
var data = event.data.data;
switch (name) {
case "setFilters":
setFilters(data);
clearCachedResults();
break;
case "checkURL":
{
var [block, redirectURL] = checkURL(data);
postMessage({
name: name,
data: {
url: data,
block: block,
redirectURL: redirectURL}});
break;
}

default:
throw new Error("Unexpected name ('" + (name + "')"));
}

}
;
