"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.isPlainObject = isPlainObject;
var _moment = _interopRequireDefault(require("moment"));
var _get = _interopRequireDefault(require("rc-util/es/utils/get"));
var _isNil = _interopRequireDefault(require("../isNil"));
const dateFormatterMap = {
  time: 'HH:mm:ss',
  timeRange: 'HH:mm:ss',
  date: 'YYYY-MM-DD',
  dateWeek: 'YYYY-wo',
  dateMonth: 'YYYY-MM',
  dateQuarter: 'YYYY-QQ',
  dateYear: 'YYYY',
  dateRange: 'YYYY-MM-DD',
  dateTime: 'YYYY-MM-DD HH:mm:ss',
  dateTimeRange: 'YYYY-MM-DD HH:mm:ss'
};
function isObject(o) {
  return Object.prototype.toString.call(o) === '[object Object]';
}
function isPlainObject(o) {
  if (isObject(o) === false) return false;

  // If has modified constructor
  const ctor = o.constructor;
  if (ctor === undefined) return true;

  // If has modified prototype
  const prot = ctor.prototype;
  if (isObject(prot) === false) return false;

  // If constructor does not have an Object-specific method
  // eslint-disable-next-line
  if (prot.hasOwnProperty('isPrototypeOf') === false) {
    return false;
  }

  // Most likely a plain Object
  return true;
}
const convertMoment = (value, dateFormatter, valueType) => {
  if (_moment.default.isMoment(value)) {
    if (dateFormatter === 'number') {
      return value.valueOf();
    }
    return value.format(dateFormatterMap[valueType] || 'YYYY-MM-DD HH:mm:ss');
  }
  return value;
};

/**
 * 根据不同的格式转化 moment
 *
 * @param value
 * @param dateFormatter
 * @param valueType
 */
const conversionMoment = (value, dateFormatter, valueType) => {
  if (!dateFormatter) {
    return value;
  }
  if (!Array.isArray(value)) {
    return convertMoment(value, dateFormatter, valueType);
  }
  return value.map(item => convertMoment(item, dateFormatter, valueType));
};

/**
 * 这里主要是来转化一下数据 将 moment 转化为 string 将 all 默认删除
 *
 * @param value
 * @param dateFormatter
 * @param proColumnsMap
 */
const conversionSubmitValue = (value, dateFormatter, valueTypeMap, omitNil, parentKey) => {
  const tmpValue = {};
  Object.keys(value).forEach(key => {
    const namePath = parentKey ? [parentKey, key] : [key];
    const valueType = (0, _get.default)(valueTypeMap, namePath) || 'text';
    const itemValue = value[key];
    if ((0, _isNil.default)(itemValue) && omitNil) {
      return;
    }
    if (isPlainObject(itemValue)) {
      tmpValue[key] = conversionSubmitValue(itemValue, dateFormatter, valueTypeMap, omitNil, key);
      return;
    }
    // 都没命中，原样返回
    tmpValue[key] = conversionMoment(itemValue, dateFormatter, valueType);
  });
  return tmpValue;
};
var _default = conversionSubmitValue;
exports.default = _default;