'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.add = add;
exports.create = create;

var _ = require('../../');

function add(json, type) {
  var date = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

  var meta = _.config.meta.name;
  var currentDate = date != null && date !== '' ? date : new Date();
  var prefixStatus = type[0];
  var abeUrl = type === 'publish' ? json[meta].link : _.cmsData.fileAttr.add(json[meta].link, prefixStatus + _.cmsData.revision.removeStatusAndDateFromFileName(currentDate.toISOString())) + '';

  if (json[meta].date == null) {
    json[meta].date = currentDate;
  }
  json[meta].latest = {
    date: currentDate,
    abeUrl: abeUrl
  };
  json[meta].status = type;
  if (json[meta][type] == null) {
    json[meta][type] = { latest: {} };
    json[meta][type].date = currentDate;
    json[meta][type].abeUrl = abeUrl;
  }
  // json[meta][type].latest = JSON.parse(JSON.stringify(obj))
  json[meta][type].latest.date = currentDate;
  json[meta][type].latest.abeUrl = abeUrl;
}

function create(json, template, url) {
  var meta = _.config.meta.name;
  if (json[meta] == null) {
    json[meta] = {};
  }

  json[meta].template = template;
  json[meta].link = url;

  return json;
}