var EscapeStore = require('./escape-store');

module.exports = function Urls() {
  var urls = new EscapeStore('URL');

  return {
    // Strip urls by replacing them by a special
    // marker for further restoring. It's done via string scanning
    // instead of regexps to speed up the process.
    escape: function(data) {
      var nextStart = 0;
      var nextEnd = 0;
      var cursor = 0;
      var tempData = [];

      for (; nextEnd < data.length;) {
        nextStart = data.indexOf('url(', nextEnd);
        if (nextStart == -1)
          break;

        nextEnd = data.indexOf(')', nextStart);

        var url = data.substring(nextStart, nextEnd + 1);
        var placeholder = urls.store(url);
        tempData.push(data.substring(cursor, nextStart));
        tempData.push(placeholder);
        cursor = nextEnd + 1;
      }

      return tempData.length > 0 ?
        tempData.join('') + data.substring(cursor, data.length) :
        data;
    },

    restore: function(data) {
      return data.replace(urls.placeholderRegExp, function(placeholder) {
        return urls.restore(placeholder).replace(/\s/g, '');
      });
    }
  };
};
