"use strict";

var filter = require('../filter');
var i18n = require('../i18n');

module.exports.jsonTransformPlugin = jsonTransformPlugin;
module.exports.translateDoc = translateDoc;

function jsonTransformPlugin(schema) {
  schema.set('toJSON', {transform: filterFields});
}

/**
 * Transform a document to a json doc.
 *
 * - options.fieldSpec The fields to show/hide
 */
function filterFields(doc, ret, options) {
  ret = filter(doc, ret, options);
  ret = addLinks(doc, ret, options);
  ret = translateDoc(doc, ret, options);
  ret = filterDates(doc, ret, options);
  ret = setImage(doc, ret, options);
  return ret;
}

function addLinks(doc, ret, options) {
  if (doc.constructor.collection) {
    if (options.apiBaseUrl) {
      ret.url = [
        options.apiBaseUrl,
        doc.constructor.collection.name.toLowerCase(),
        doc._id || doc.id
      ].join('/');
    }
    if (options.baseUrl && (doc._id || doc.id)) {
      ret.html_url = [
        options.baseUrl,
        doc.constructor.collection.name.toLowerCase(),
        doc._id || doc.id
      ].join('/');
    }
  }
  return ret;
}

function filterDates(doc, ret, options) {
  if (!options.at) {
    return ret;
  }

  function checkDates(field) {
    if (!field.start_date && !field.end_date) {
      return true;
    }
    var start = new Date(field.start_date);
    var end = new Date(field.end_date);
    var at = options.at;

    return start < at && (!field.end_date || end > at);
  }

  if (doc.other_names) {
    ret.other_names = doc.other_names.filter(checkDates);
  }

  if (doc.memberships) {
    ret.memberships = doc.memberships.filter(checkDates);
  }

  return ret;
}

function translateDoc(doc, ret, options) {
  if (options.returnAllTranslations) {
    return ret;
  }
  return i18n(ret, options.langs, options.defaultLanguage, options.includeTranslations);
}

function generateImageUrl(img, doc, options) {
  var url = [
    options.baseUrl,
    doc.constructor.collection.name.toLowerCase(),
    doc._id || doc.id,
    'image',
    img._id
  ].join('/');

  return url;
}

function generateImageProxyUrl(img, doc, options) {
  var url = [
    options.proxyBaseUrl,
    encodeURIComponent(img.url),
  ].join('/');

  return url;
}

function setImage(doc, ret, options) {
  var images = ret.images;

  if ( images && images.length ) {
    ret.images = [];
    doc.get('images').forEach(function imageProcess(img) {
      if (!img.url) {
        img.url = generateImageUrl(img, doc, options);
      }
      if (options.proxyBaseUrl && !img.proxy_url) {
        img.proxy_url = generateImageProxyUrl(img, doc, options);
      }
      ret.images.push(img);
    });
    ret.image = ret.images[0].url;
    if ( options.proxyBaseUrl ) {
      ret.proxy_image = ret.images[0].proxy_url;
    }
  }

  return ret;
}

@@ -1842,24 +1842,268 @@
 ations);%0A%7D%0A%0A
+%0Afunction _generateUrl(parts) %7B%0A  var url = parts.join('/');%0A%0A  // this is a bit grim but there doesn't seem to be a JS%0A  // library to do this.%0A  var reDoubleSlash = /(%5B%5E:%5D)%5C/%5C//g;%0A  url = url.replace(reDoubleSlash, %22$1/%22);%0A%0A  return url;%0A%7D%0A%0A%0A
 function gen
@@ -2135,35 +2135,37 @@
 ptions) %7B%0A  var 
-url
+parts
  = %5B%0A    options
@@ -2272,34 +2272,24 @@
  img._id%0A  %5D
-.join('/')
 ;%0A%0A  return 
@@ -2280,35 +2280,51 @@
 %0A  %5D;%0A%0A  return 
-url
+_generateUrl(parts)
 ;%0A%7D%0A%0Afunction ge
@@ -2370,19 +2370,21 @@
 %7B%0A  var 
-url
+parts
  = %5B%0A   
@@ -2442,26 +2442,16 @@
 rl),%0A  %5D
-.join('/')
 ;%0A%0A  ret
@@ -2450,27 +2450,43 @@
 ;%0A%0A  return 
-url
+_generateUrl(parts)
 ;%0A%7D%0A%0Afunctio

remove // from image URLs