'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /**
                                                                                                                                                                                                                                                                   * Created by baoyinghai on 7/3/17.
                                                                                                                                                                                                                                                                   */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _generator = require('../generator');

var _generator2 = _interopRequireDefault(_generator);

var _fieldTransformer = require('./fieldTransformer');

var _fieldTransformer2 = _interopRequireDefault(_fieldTransformer);

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

var propsFilter = function propsFilter(props) {
  return props;
};

var getDataPath = function getDataPath(dataSource) {
  return function (f) {
    if (!dataSource) return null;
    var fieldDesc = dataSource[f];
    return fieldDesc.value.split('.');
  };
};

var createNode = function createNode(node) {
  var ret = {};
  if (!node) return null;
  if (_generator2.default[node.nodeType]) {
    (function () {
      // 区分容器还是组件
      ret.key = node.key;
      var Comp = _generator2.default[node.nodeType][node.name];
      var fieldHandler = getDataPath(node.dataSource);
      ret.renderComp = function (data) {
        var appendProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

        var dataSource = null;
        if (node.nodeType !== 'CONTAINER') {
          if (data && data.split) {
            // array
            dataSource = node.dataSource && data.map(function (d) {
              return Object.keys(node.dataSource).reduce(function (obj, key) {
                obj[key] = _fieldTransformer2.default[node.dataSource[key].vType](d, node.dataSource[key]);
                return obj;
              }, {});
            });
          } else {
            dataSource = node.dataSource && Object.keys(node.dataSource).reduce(function (obj, key) {
              obj[key] = _fieldTransformer2.default[node.dataSource[key].vType](data, node.dataSource[key]);
              return obj;
            }, {});
          }
        }

        return _react2.default.createElement(Comp, _extends({}, propsFilter(node.props), appendProps, { _getDataPath: fieldHandler, fieldDesc: node.dataSource, dataSource: dataSource }));
      };
    })();
  }
  if (node.childrenNode) {
    ret.childrenNode = node.childrenNode.map(function (n) {
      return createNode(n);
    });
  }
  return ret;
};

exports.default = {
  ngtCreate: function ngtCreate(art) {
    return createNode(art);
  }
};