'use strict';

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

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _url = require('url');

var _url2 = _interopRequireDefault(_url);

var _cli = require('../../cli');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var route = function route(req, res, next) {
  _cli.abeExtend.hooks.instance.trigger('beforeRoute', req, res, next);
  if (typeof res._header !== 'undefined' && res._header !== null) return;

  var sourceString = req.body.sourceString;
  var prefillQuantity = req.body.prefillQuantity;
  var folder = _url2.default.parse(req.header('referer')).path;
  folder = folder.replace('/abe/editor', '');
  folder = _path2.default.dirname(folder);
  var key = req.body.key;
  var jsonPage = req.body.json ? JSON.parse(JSON.stringify(req.body.json)) : {};

  jsonPage[key] = null;

  var request = '{{abe type="data" key="' + key + '" source="' + sourceString + '" prefill="true" prefill-quantity=\'' + prefillQuantity + '\' editable="true"}}';
  var obj = _cli.cmsData.attributes.getAll(request, jsonPage);

  _cli.cmsData.source.requestList(obj, folder, request, jsonPage).then(function () {

    res.set('Content-Type', 'application/json');
    res.send(JSON.stringify(jsonPage[key]));
  });
};

exports.default = route;