'use strict';

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

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 12/8/16.
                                                                                                                                                                                                                                                                   */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _constant = require('../constant');

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * @param onLocalChange {function}
 *
 * example:
 * function(e, helper){
 *   e.target.value==1 ?
 *   helper.setDictionaryFilter('registeredCapitalCurrency', function(a){return a.code==='RMB' || a.code === 'POUND';})
 *   :'';
 * }
 *
 * */
var createComp = exports.createComp = function createComp(compRender, fieldCtrl, onLocalChange, changeInitValue) {
  var getFieldValue = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : function () {
    return '';
  };

  var onChange = function onChange(e) {
    if (fieldCtrl) {
      if (onLocalChange) {
        var setFieldHelper = function setFieldHelper() {
          var fieldMap = {};
          var getFieldMap = function getFieldMap() {
            return fieldMap;
          };
          var setModalInputEmpty = function setModalInputEmpty() {
            changeInitValue('');
          };
          var setFieldVisible = function setFieldVisible(fieldName, value) {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].isVisible = value;
          };

          var setFieldsVisible = function setFieldsVisible(fieldNames, value) {
            fieldNames.forEach(function (item, index) {
              if (!fieldMap[item]) {
                fieldMap[item] = {};
              }
              fieldMap[item].isVisible = value;
            });
          };

          // TODO: 校验规则已经修改, 但这里并没有更新
          var setFieldRule = function setFieldRule(fieldName, rules) {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            var orgRules = fieldMap[fieldName].rules || {};
            fieldMap[fieldName].rules = Object.assign(orgRules, rules);
          };
          var setFieldValue = function setFieldValue(fieldName) {
            var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';

            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].value = value;
          };
          var setDictionaryFilter = function setDictionaryFilter(fieldName, fn) {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].dictionaryFilter = fn;
          };

          var setActionLink = function setActionLink(fieldName, actionLink) {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].actionLink = actionLink;
          };

          return {
            getFieldMap: getFieldMap,
            setFieldVisible: setFieldVisible,
            setFieldRule: setFieldRule,
            setFieldValue: setFieldValue,
            setDictionaryFilter: setDictionaryFilter,
            setActionLink: setActionLink,
            setModalInputEmpty: setModalInputEmpty,
            getFieldValue: getFieldValue,
            setFieldsVisible: setFieldsVisible
          };
        };
        var helper = setFieldHelper();
        onLocalChange(e, {
          setFieldVisible: helper.setFieldVisible,
          setFieldRule: helper.setFieldRule,
          setFieldValue: helper.setFieldValue,
          setDictionaryFilter: helper.setDictionaryFilter,
          setActionLink: helper.setActionLink,
          setModalInputEmpty: helper.setModalInputEmpty,
          getFieldValue: helper.getFieldValue,
          setFieldsVisible: helper.setFieldsVisible
        });
        fieldCtrl(helper.getFieldMap());
      }
    }
  };

  return _extends({}, compRender, _defineProperty({}, _constant.EDIT, function (record) {
    return _react2.default.cloneElement(compRender[_constant.EDIT](record), { onChange: onChange });
  }));
};