/**
 * Created by baoyinghai on 12/8/16.
 */
import React from 'react';
import { EDIT } from '../constant';

/**
 * @param onLocalChange {function}
 *
 * example:
 * function(e, helper){
 *   e.target.value==1 ?
 *   helper.setDictionaryFilter('registeredCapitalCurrency', function(a){return a.code==='RMB' || a.code === 'POUND';})
 *   :'';
 * }
 *
 * */
export const createComp = (compRender, fieldCtrl, onLocalChange, changeInitValue, getFieldValue = () => '') => {
  const onChange = e => {
    if (fieldCtrl) {
      if (onLocalChange) {
        const setFieldHelper = () => {
          const fieldMap = {};
          const getFieldMap = () => {
            return fieldMap;
          };
          const setModalInputEmpty = () => {
            changeInitValue('');
          };
          const setFieldVisible = (fieldName, value) => {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].isVisible = value;
          };

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

          // TODO: 校验规则已经修改, 但这里并没有更新
          const setFieldRule = (fieldName, rules) => {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            const orgRules = fieldMap[fieldName].rules || {};
            fieldMap[fieldName].rules = Object.assign(orgRules, rules);
          };
          const setFieldValue = (fieldName, value = '') => {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].value = value;
          };
          const setDictionaryFilter = (fieldName, fn) => {
            if (!fieldMap[fieldName]) {
              fieldMap[fieldName] = {};
            }
            fieldMap[fieldName].dictionaryFilter = fn;
          };

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

          return {
            getFieldMap,
            setFieldVisible,
            setFieldRule,
            setFieldValue,
            setDictionaryFilter,
            setActionLink,
            setModalInputEmpty,
            getFieldValue,
            setFieldsVisible
          };
        };
        const 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 {
    ...compRender,
    [EDIT]: record => {
      return React.cloneElement(compRender[EDIT](record), { onChange: onChange });
    }
  };
};
