"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.reduce.js");
var _react = _interopRequireDefault(require("react"));
var _get = _interopRequireDefault(require("rc-util/es/utils/get"));
var _set = _interopRequireDefault(require("rc-util/es/utils/set"));
var _isNil = _interopRequireDefault(require("../isNil"));
const transformKeySubmitValue = (values, dataFormatMapRaw) => {
  // ignore nil transfrom
  const dataFormatMap = Object.keys(dataFormatMapRaw).reduce((ret, key) => {
    const value = dataFormatMapRaw[key];
    if (!(0, _isNil.default)(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.default.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 = (0, _set.default)(result, [entryKey], genValues);
        return;
      }
      const transformFunction = (0, _get.default)(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 = (0, _set.default)(result, tempKey, itemValue);
        return;
      }
      if (typeof tempKey === 'object') {
        finalValues = {
          ...finalValues,
          ...tempKey
        };
      } else if (tempKey) {
        result = (0, _set.default)(result, [tempKey], itemValue);
      }
    });
    return result;
  };
  finalValues = {
    ...gen(values),
    ...finalValues
  };
  return finalValues;
};
var _default = transformKeySubmitValue;
exports.default = _default;