import "core-js/modules/es.array.reduce.js";
import React from 'react';
import get from 'rc-util/es/utils/get';
import namePathSet from 'rc-util/es/utils/set';
import isNil from '../isNil';
const transformKeySubmitValue = (values, dataFormatMapRaw) => {
  // ignore nil transfrom
  const dataFormatMap = Object.keys(dataFormatMapRaw).reduce((ret, key) => {
    const value = dataFormatMapRaw[key];
    if (!isNil(value)) {
      // eslint-disable-next-line no-param-reassign
      ret[key] = value; // can't be undefined
    }

    return ret;
  }, {});
  if (Object.keys(dataFormatMap).length < 1) {
    return values;
  }
  let finalValues = {};
  const gen = (tempValues, parentsKey) => {
    let result = {};
    Object.keys(tempValues).forEach(entryKey => {
      const key = parentsKey ? [parentsKey, entryKey] : [entryKey];
      const itemValue = tempValues[entryKey];
      if (typeof itemValue === 'object' && !Array.isArray(itemValue) && ! /*#__PURE__*/React.isValidElement(itemValue) &&
      // ignore walk throungh React Element
      !(itemValue instanceof Blob) // ignore walk throungh Blob
      ) {
        const genValues = gen(itemValue, entryKey);
        if (Object.keys(genValues).length < 1) {
          return;
        }
        result = namePathSet(result, [entryKey], genValues);
        return;
      }
      const transformFunction = get(dataFormatMap, key);
      const tempKey = typeof transformFunction === 'function' ? transformFunction === null || transformFunction === void 0 ? void 0 : transformFunction(itemValue, entryKey, tempValues) : entryKey;
      // { [key:string]:any } 数组也能通过编译
      if (Array.isArray(tempKey)) {
        result = namePathSet(result, tempKey, itemValue);
        return;
      }
      if (typeof tempKey === 'object') {
        finalValues = {
          ...finalValues,
          ...tempKey
        };
      } else if (tempKey) {
        result = namePathSet(result, [tempKey], itemValue);
      }
    });
    return result;
  };
  finalValues = {
    ...gen(values),
    ...finalValues
  };
  return finalValues;
};
export default transformKeySubmitValue;