var config = require("./config");
var shallowCopy = require("./shallow-copy");

var SET_NOTHING = { headers: [] };

var handlers = {};

handlers.default = function() {
  return { headers: config.allHeaders };
};

handlers.IE = function (browser) {
  var header = browser.version < 12 ? "X-Content-Security-Policy" : "Content-Security-Policy";
  return { headers: [header] };
};

handlers.Firefox = function(browser, directives) {

  var version = parseFloat(browser.version);

  if (version >= 23) {
    return { headers: ["Content-Security-Policy"] };
  } else if (version >= 4 && version < 23) {

    var policy = shallowCopy(directives);

    policy.defaultSrc = policy.defaultSrc || ["*"];

    Object.keys(policy).forEach(function (key) {
      var value = policy[key];
      if (key === "connectSrc") {
        policy.xhrSrc = value;
      } else if (key === "defaultSrc") {
        if (version < 5) {
          policy.allow = value;
        } else {
          policy.defaultSrc = value;
        }
      } else if (key !== "sandbox") {
        policy[key] = value;
      }

      var index;
      if ((index = policy[key].indexOf("'unsafe-inline'")) !== -1) {
        if (key === "scriptSrc") {
          policy[key][index] = "'inline-script'";
        } else {
          policy[key].splice(index, 1);
        }
      }
      if ((index = policy[key].indexOf("'unsafe-eval'")) !== -1) {
        if (key === "scriptSrc") {
          policy[key][index] = "'eval-script'";
        } else {
          policy[key].splice(index, 1);
        }
      }
    });

    return {
      headers: ["X-Content-Security-Policy"],
      directives: policy
    };

  } else {

    return SET_NOTHING;

  }

};

handlers.Chrome = function(browser) {
  var version = parseFloat(browser.version);
  if ((version >= 14) && (version < 25)) {
    return { headers: ["X-WebKit-CSP"] };
  } else if (version >= 25) {
    return { headers: ["Content-Security-Policy"] };
  } else {
    return SET_NOTHING;
  }
};

handlers.Safari = function(browser, directives, options) {
  var version = parseFloat(browser.version);
  if (version >= 7) {
    return { headers: ["Content-Security-Policy"] };
  } else if ((version >= 6) || options.safari5) {
    return { headers: ["X-WebKit-CSP"] };
  } else {
    return SET_NOTHING;
  }
};

handlers.Opera = function(browser) {
  if (parseFloat(browser.version) >= 15) {
    return { headers: ["Content-Security-Policy"] };
  } else {
    return SET_NOTHING;
  }
};

handlers["Android Browser"] = function(browser, directives, options) {
  if ((parseFloat(browser.os.version) < 4.4) || options.disableAndroid) {
    return SET_NOTHING;
  } else {
    return { headers: ["Content-Security-Policy"] };
  }
};

handlers["Chrome Mobile"] = function(browser, directives) {
  if (browser.os.family === "iOS") {
    var result = { headers: ["Content-Security-Policy"] };
    var connect = directives.connectSrc || directives.connectSrc;
    if (!connect) {
      result.directives = shallowCopy(directives);
      result.directives.connectSrc = ["'self'"];
    } else if (connect.indexOf("'self'") === -1) {
      result.directives = shallowCopy(directives);
      result.directives.connectSrc.push("'self'");
    }
    return result;
  } else {
    return handlers["Android Browser"].apply(this, arguments);
  }
};

module.exports = handlers;

@@ -1775,25 +1775,24 @@
 sion);%0A  if 
-(
 (version %3E= 
@@ -1797,14 +1797,12 @@
 = 14
-)
  && 
-(
 vers
@@ -1810,17 +1810,16 @@
 on %3C 25)
-)
  %7B%0A    r
@@ -2172,17 +2172,16 @@
 else if 
-(
 (version
@@ -2185,17 +2185,16 @@
 ion %3E= 6
-)
  %7C%7C opti
@@ -2545,17 +2545,16 @@
  %7B%0A  if 
-(
 (parseFl
@@ -2582,17 +2582,16 @@
 n) %3C 4.4
-)
  %7C%7C opti

Remove some gratuitous parenthesis