var mustache = require('mustache');
var DEFAULT = '{{slug}}';
var moment = require('moment');
             require('moment-timezone');

// this needs to be pulled directly from the moment library
// i copied them from the docs.
var MOMENT_TOKENS = ["M", "Mo", "MM", "MMM", "MMMM", "Q", "Qo", "D", "Do", "DD", "DDD", "DDDo", "DDDD", "d", "do", "dd", "ddd", "dddd", "e", "E", "w", "wo", "ww", "W", "Wo", "WW", "YY", "YYYY", "Y", "gg", "gggg", "GG", "GGGG", "A", "a", "H", "HH", "h", "hh", "k", "kk", "m", "mm", "s", "ss", "S", "SS", "SSS", "SSSS", "SSSSS", "SSSSSS", "SSSSSSS", "SSSSSSSS" , "SSSSSSSSS", "z", "zz", "Z", "ZZ", "X", "x"];
var helper = require('helper');
var normalize = helper.urlNormalizer;
var makeSlug = helper.makeSlug;
var allow = ['slug', 'name', 'size', 'more', 'menu', 'page', 'dateStamp', 'created', 'updated', 'metadata'];

// found here:
// https://gist.github.com/mathewbyrne/1280286
function removeDiacritics (str) {

  str = str || '';
  str = decodeURIComponent(str); // wtf
  str = str.replace(/^\s+|\s+$/g, '');
  str = str.toLowerCase();

  var from = "àáäâèéëêìíïîòóöôùúüûñç·_,:;";
  var to   = "aaaaeeeeiiiioooouuuunc-----";

  for (var i = 0, l = from.length; i<l; i++)
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));

  str = str
    .replace(/[^a-z0-9 -\/]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

  str = encodeURIComponent(str);

  return str;
}

module.exports = function (timeZone, format, entry) {

  // Add the permalink automatically if the metadata
  // declared a page with no permalink set. We can't
  // do this earlier, since we don't know the slug then
  var permalink = '';
  var view = {};

  format = format || DEFAULT;

  try {

    // this is so inefficient
    MOMENT_TOKENS.forEach(function(token){
      view[token] = moment.utc(entry.dateStamp || entry.updated)
                     .tz(timeZone)
                     .format(token);
    });

    for (var i in entry)
      if (allow.indexOf(i) > -1)
        view[i] = entry[i];

    view.stem = makeSlug(entry.path.slice(0, entry.path.lastIndexOf('.')));

    // this needs a better name but make sure to update any
    // existing custom formats for folks...
    view['slug-without-diacritics'] = removeDiacritics(view.slug);

    // we don't want mustache to escape anything...
    format = format.split('{{').join('{{{');
    format = format.split('}}').join('}}}');

    permalink = mustache.render(format || DEFAULT, view);

  } catch (e) {

    permalink = '';
  }

  return normalize(permalink);
};
@@ -847,22 +847,27 @@
 %0A// 
-found
+modified from
  here:
-%0A//
  htt
@@ -1001,50 +1001,31 @@
  // 
-wtf%0A  str = str.replace(/%5E%5Cs+%7C%5Cs+$/g, '');
+lol we shouldnt do this
 %0A  s
@@ -1069,77 +1069,117 @@
  %22%C3%A0%C3%A1
-%C3%A4%C3%A2%C3%A8%C3%A9%C3%AB%C3%AA%C3%AC%C3%AD%C3%AF%C3%AE%C3%B2%C3%B3%C3%B6%C3%B4%C3%B9%C3%BA%C3%BC%C3%BB%C3%B1%C3%A7%C2%B7_,:;%22;%0A  var to   = %22aaaaeeeeiiiioooouuuunc-----
+%C3%A2%C3%A4%C3%A6%C3%A3%C3%A5%C4%81%C3%A7%C4%87%C4%8D%C3%A8%C3%A9%C3%AA%C3%AB%C4%93%C4%97%C4%99%C3%AE%C3%AF%C3%AD%C4%AB%C4%AF%C3%AC%C5%82%C3%B1%C5%84%C3%B4%C3%B6%C3%B2%C3%B3%C5%93%C3%B8%C5%8D%C3%B5%C3%9F%C5%9B%C5%A1%C3%BB%C3%BC%C3%B9%C3%BA%C5%AB%C3%BF%C5%BE%C5%BA%C5%BC%22;%0A  var to   = %22aaaaaaaaccceeeeeeeiiiiiilnnoooooooosssuuuuuyzzz
 %22;%0A%0A
@@ -1298,191 +1298,8 @@
 );%0A%0A
-  str = str%0A    .replace(/%5B%5Ea-z0-9 -%5C/%5D/g, '') // remove invalid chars%0A    .replace(/%5Cs+/g, '-') // collapse whitespace and replace by -%0A    .replace(/-+/g, '-'); // collapse dashes%0A%0A
   st

extend diacritic library