// See COPYING for copyright and license details
(function () {
  var configDir = data.configDir;
  var profile = data.profile;
  function filterNull(object) { return object !== null; }
  function filterEmpty(line) { return line.trim().length > 0; }
  function mapDefault(line) { return line.trim(); }
  function mapQuickmarks(line) {
        var token = line.trim().split(" ");
        return {
            mark : token[0],
            url : token.slice(1).join(" ")
        };
  }
  function mapSearchengines(line) {
      var engine = null;
      try {
          var token = line.trim().split(" ");
          var keyword = token[0];
          var url = token.slice(1).join(" ");
          var purl = net.parseUri(url);
          var host = purl ? purl.host : null;
          engine = {
              keyword : keyword, 
              url : url, 
              host : host
          };
      }
      catch (e) { }
      return engine;

  }
  Object.defineProperties(data, {
      parse : {
          value : function(name) {
              var mapFunction;
              switch (name) {
                case "bookmarks": 
                case "cookiesWhitelist" : 
                case "sessionCookiesWhitelist" : 
                case "scriptWhitelist" : 
                case "pluginsWhitelist" : 
                    mapFunction = mapDefault;
                    break;
                case "quickmarks" : 
                    mapFunction = mapQuickmarks;
                    break;
                case "searchEngines" : 
                    mapFunction = mapSearchengines;
                    break;
                default : 
                    throw new Error("Parsing " + name + " is not supported!");
              }
              var data = null;

              try {
                  data = io.read(this[name]); 
              }
              catch (e) {
                  return [];
              }
              return data.split("\n").filter(filterEmpty).map(mapFunction).filter(filterNull);
          }
      },
      "bookmarks" :  { value : configDir + "/" + profile + "/bookmarks", enumerable : true }, 
      "history"   :  { value : configDir + "/" + profile + "/history", enumerable : true },
      "cookies"   :  { value : configDir + "/" + profile + "/cookies", enumerable : true }, 
      "quickmarks" : { value : configDir + "/" + profile + "/quickmarks", enumerable : true },
      "cookiesWhitelist"  :  { value : configDir + "/" + profile + "/cookies.allow", enumerable : true }, 
      "sessionCookiesWhitelist"   :  { value : configDir + "/" + profile + "/cookies_session.allow", enumerable : true }, 
      "pluginsWhitelist"  :  { value : configDir + "/" + profile + "/plugins.allow", enumerable : true }, 
      "scriptWhitelist"   :  { value : configDir + "/" + profile + "/scripts.allow", enumerable : true }, 
      "session"   :  { value : configDir + "/" + profile + "/session", enumerable : true }, 
      "customKeys"  :  { value : configDir + "/" + profile + "/custom_keys", enumerable : true }, 
      "keys"  :  { value : configDir + "/keys", enumerable : true }, 
      "settings"  :  { value : configDir + "/settings", enumerable : true }, 
      "searchEngines"   :  { value : configDir + "/searchengines", enumerable : true }
  });
})();
Object.freeze(data);
