'use strict';


const chain = require('ramda/src/chain');
const toReactComponent = require('jsonml-to-react-component');
const exist = require('exist.js');
const NotFound = require('{{ themePath }}/template/NotFound');
const data = require('./data.js');
const NProgress = require('nprogress');
require('nprogress/nprogress.css');

const plugins = data.plugins;
const converters = chain((plugin) => plugin.converters || [], plugins);
const utils = {
  get: exist.get,
  toReactComponent(jsonml) {
    return toReactComponent(jsonml, converters);
  },
};
plugins.map((plugin) => plugin.utils || {})
  .forEach((u) => Object.assign(utils, u));

function calcPropsPath(dataPath, params) {
  return Object.keys(params).reduce(
    (path, param) => path.replace(`:${param}`, params[param]),
    dataPath
  );
}

function hasParams(dataPath) {
  return dataPath.split('/').some((snippet) => snippet.startsWith(':'));
}

function defaultCollect(nextProps, callback) {
  callback(null, nextProps);
}
module.exports = function templateWrapper(template, dataPath = '') {
  const Template = require(`{{ themePath }}/template${template.replace(/^\.\/template/, '')}`);

  return (nextState, callback) => {
    const propsPath = calcPropsPath(dataPath, nextState.params);
    const pageData = exist.get(data.markdown, propsPath.split('/').slice(1));
    const collect = Template.collect || defaultCollect;
    collect(Object.assign({}, nextState, {
      data: data.markdown,
      pageData,
      utils,
    }), (err, nextProps) => {
      NProgress.done();
      const Comp = !hasParams(dataPath) || pageData ?
      Template.default || Template : NotFound;
      Comp.defaultProps = Object.assign({}, Comp.defaultProps, nextProps, {
        data: data.markdown,
        pageData,
        utils,
      });
      callback(err, Comp);
    });
  };
};

@@ -8,16 +8,48 @@
 rict';%0A%0A
+const React = require('react');%0A
 %0Aconst c
@@ -1624,16 +1624,24 @@
 eData ?%0A
+        
       Te
@@ -1689,27 +1689,75 @@
     
-Comp.defaultProps =
+callback(err, (props) =%3E %7B%0A        return React.createElement(Comp,
  Obj
@@ -1775,21 +1775,9 @@
 %7B%7D, 
-Comp.defaultP
+p
 rops
@@ -1791,108 +1791,19 @@
 rops
-, %7B%0A        data: data.markdown,%0A        pageData,%0A        utils,%0A      %7D);%0A      callback(err, Comp
+));%0A      %7D
 );%0A 

lazy load