'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderColumn = exports.handleOrderItems = exports.handleFilterItems = exports.arr2obj = 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; };
// import { FormItemMap } from '../detail/displayComp';


var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _JSExecutor = require('../../utils/JSExecutor');

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

var numeral = require('numeral');

var isNeedTransForm = function isNeedTransForm(type) {
  return type === 'SELECT' || type === 'CHECKBOXGROUP' || type === 'CHECKBOX' || type === 'RADIO' || type === 'RADIOGROUP';
};

var arr2obj = exports.arr2obj = function arr2obj(array, key) {
  return array.reduce(function (result, item) {
    result[item[key]] = item;
    return result;
  }, {});
};

var handleFilterItems = exports.handleFilterItems = function handleFilterItems(filterItems, fieldsObject) {
  return filterItems.map(function (filter) {
    // const fieldData = data.fields.find(field => field.name === filter.name);
    var fieldData = fieldsObject[filter.name];
    if (fieldData) {
      return _extends({}, filter, {
        type: fieldData.displayComponent.componentType,
        extra: fieldData.displayComponent.dictionaryItems,
        dataType: fieldData.type
      });
    }
    return filter;
  });
};

var handleOrderItems = exports.handleOrderItems = function handleOrderItems(orderItems) {
  var orderFields = {};
  if (orderItems && orderItems.length > 0) {
    orderItems.forEach(function (item) {
      // const temp = {
      //   orderField: item.name,
      //   orderType: item.isAsc ? 'Asc' : 'Desc'
      // };
      orderFields[item.name] = item;
    });
  }
  return orderFields;
};

var renderColumn = exports.renderColumn = function renderColumn(text, fieldContent, record) {
  var ret = text;
  if (!fieldContent) {
    ret = '';
  } else if (isNeedTransForm(fieldContent.displayComponent.componentType)) {
    (function () {
      var valueList = text && text.split(',') || [];
      var dictItem = fieldContent.displayComponent.dictionaryItems.filter(function (dict) {
        return valueList.indexOf(dict.code) >= 0;
      });
      ret = dictItem && dictItem.reduce(function (res, f) {
        return res + ' ' + f.value;
      }, '') || text;
    })();
  } else if (fieldContent.type === 'Date') {
    if (text) {
      ret = (0, _moment2.default)(new Date(text)).format('YYYY-MM-DD');
    } else {
      ret = '';
    }
  } else if (fieldContent.displayComponent.componentType === 'INPUTNUMBER') {
    var magnitude = fieldContent.displayComponent.magnitude || 1;
    if (text) {
      var isAmountFormat = null;
      fieldContent && fieldContent.formValidates && fieldContent.formValidates.forEach(function (item, index1) {
        if (item.validateType === 'ISAMOUNT') {
          isAmountFormat = item.format;
        }
      });
      var format = isAmountFormat || '0,0.0000';
      ret = numeral(text * magnitude).format(format);
    } else {
      ret = text;
    }
    var addOnBefore = fieldContent.displayComponent.addOnBefore;
    if (addOnBefore) {
      if (addOnBefore.indexOf('function') >= 0 && record) {
        ret = (0, _JSExecutor.createFunc)(addOnBefore)(record) + ' ' + ret;
      } else {
        ret = addOnBefore + ' ' + ret;
      }
    }
  } else {
    ret = text;
  }
  return ret;
};