const stream_of = require('rillet').of;
const tag_format = require('./tags.js');

function edition_or_artefact(artefact, edition_field, artefact_field) {
  if (artefact.edition && artefact.edition[edition_field])
    return artefact.edition[edition_field];
  return artefact[artefact_field];
} // edition_field

function minimal_artefact_format(artefact, url_helper) {
  return {
    'id': url_helper.artefact_url(artefact),
    'web_url': url_helper.artefact_web_url(artefact),
    'slug': artefact.slug,
    'title': edition_or_artefact(artefact, 'title', 'name'),
    'format': underscorify(edition_or_artefact(artefact, 'format', 'kind'))
  };
} // minimal_artefact_format

function basic_artefact_format(artefact, url_helper) {
  return merge(
    minimal_artefact_format(artefact, url_helper),
    {
      'updated_at': updated_date(artefact),
      'created_at': created_date(artefact),
      'tag_ids': artefact.tag_ids
    }
  );
} // basic_artefact_format

function format(artefact, url_helper) {
  const pretty = basic_artefact_format(artefact, url_helper);

  pretty.tags = tag_format(artefact.tags, url_helper);

  return pretty;
} // format

module.exports = format;

///////////////////////////////////////////////
function underscorify(str) {
  if (!str)
    return str;
  return str.replace(/ /g, '_');
} // underscorify

function merge(object1, object2) {
  for (const [k,v] of Object.entries(object2))
    object1[k] = v;
  return object1;
} // merge

function updated_date(artefact) {
  try {
    const ud = stream_of(artefact.updated_at, artefact.edition ? artefact.edition.updated_at : undefined).
  	filter(d => d).
 	max();
    return ud ? ud.iso8601 : '';
  } catch(err) {
    return '';
  } // catch
} // updated_date


function created_date(artefact) {
  return artefact.created_at ? artefact.created_at.iso8601 : '';
} // created_date

@@ -1661,18 +1661,28 @@
 d ? 
-ud.iso8601
+content_api_date(ud)
  : '
@@ -1814,16 +1814,33 @@
 ed_at ? 
+content_api_date(
 artefact
@@ -1854,37 +1854,467 @@
 d_at
-.iso8601 : '';%0A%7D // created_date
+) : '';%0A%7D // created_date%0A%0A// if javascripts toISOString is ok, we can use that instead%0Afunction content_api_date(date) %7B%0A  return date.getUTCFullYear() +%0A        '-' + pad(date.getUTCMonth() + 1) +%0A        '-' + pad(date.getUTCDate()) +%0A        'T' + pad(date.getUTCHours()) +%0A        ':' + pad(date.getUTCMinutes()) +%0A        ':' + pad(date.getUTCSeconds()) +%0A        '+00:00';%0A%7D // content_api_date%0A%0Afunction pad(n) %7B%0A  return (n %3C 10) ? %600$%7Bn%7D%60 : n;%0A%7D // pad
 %0A

format date to agree with existing content_api