/**
 * Created by geweimin on 17/5/2.
 */
import { templateReg } from './StringUtils';

export const getMappings = (modalInputData, props) => {
  let mappingFields = null;
  if (!modalInputData.actionLinkMappings) {
    if (modalInputData.mappingsList) {
      mappingFields = modalInputData.mappingsList[0].mappings;
    } else {
      mappingFields = modalInputData.mappings;
    }
  } else {
    /* eslint-disable */
    let i = 0;
    const matchRes = modalInputData.actionLink.match(templateReg);
    const templateField = matchRes && matchRes[0].substring(2, matchRes[0].length - 1);
    modalInputData.actionLinkMappings.forEach((item, index) => {
      if (item.dictionaryItemName === { ...props.form.getFieldsValue(), ...props.query }[templateField]) {
        i = index;
      }
    });
    mappingFields = modalInputData.mappingsList[i].mappings;
  }
  return mappingFields;
};
