'use strict';

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

var _handlebars = require('handlebars');

var _handlebars2 = _interopRequireDefault(_handlebars);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

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

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Page class
 * manage HTML generation for page template
 */
var Page =

/**
 * Create new page object
 * @param  {Object} params req.params from express route
 * @param  {Object} i18n translation
 * @param  {Function} callback 
 * @param  {Boolean} onlyHTML default = false, if true HTML content will contains abe attributes
 * @return {String} HTML page as string
 */
function Page(templateId, template, json) {
  var onlyHTML = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

  _classCallCheck(this, Page);

  // HOOKS beforePageJson
  json = _.abeExtend.hooks.instance.trigger('beforePageJson', json);

  _.abeEngine.instance.content = json;

  if (typeof _handlebars2.default.templates[templateId] !== 'undefined' && _handlebars2.default.templates[templateId] !== null && _.config.files.templates.precompile) {
    template = _handlebars2.default.templates[templateId];
    this.html = template(json, { data: { intl: _.config.intlData } });
  } else {

    this._onlyHTML = onlyHTML;
    this.template = template;
    this.HbsTemplatePath = _path2.default.join(_.config.root, _.config.templates.url, 'hbs/' + templateId + '.hbs');

    // Remove text with attribute "visible=false"
    this.template = _.cmsTemplates.prepare.removeHiddenAbeTag(this.template);

    if (!this._onlyHTML) {
      // Surrounds each Abe tag (which are text/rich/textarea and not in html attribute) with <abe> tag
      // ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title>
      this.template = _.cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template);

      this.template = _.cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.template);

      this.template = _.cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.template);

      this.template = _.cmsTemplates.prepare.addAbeSourceComment(this.template, json);
    } else {
      this.template = _.cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template);
    }

    // je rajoute les index pour chaque bloc lié à un each
    this.template = _.cmsTemplates.prepare.indexEachBlocks(this.template, this._onlyHTML);

    // We remove the {{abe type=data ...}} from the text 
    this.template = _.cmsData.source.removeDataList(this.template);

    // It's time to replace the [index] by {{@index}} (concerning each blocks)
    this.template = _.cmsTemplates.prepare.replaceAbeEachIndex(this.template);

    if (_.config.files.templates.precompile) {
      // Let's persist the precompiled template for future use (kind of cache)
      _fsExtra2.default.writeFileSync(this.HbsTemplatePath, _handlebars2.default.precompile(this.template), 'utf8');
      _.Manager.instance.addHbsTemplate(templateId);
    }

    // I compile the text
    var compiledTemplate = _handlebars2.default.compile(_.cmsTemplates.insertDebugtoolUtilities(this.template, this._onlyHTML));

    // I create the html page ! yeah !!!
    this.html = compiledTemplate(json, { data: { intl: _.config.intlData } });
  }

  if (this._onlyHTML) {
    this.html = _.abeExtend.hooks.instance.trigger('afterPageSaveCompile', this.html, json);
  } else {
    this.html = _.abeExtend.hooks.instance.trigger('afterPageEditorCompile', this.html, json);
  }
};

exports.default = Page;