function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { isVoidField } from '@formily/core';
import { useFormLayout, useFormShallowLayout } from '@teamix/formily';
import { isArr, isObj, getValueByValue, isPlainObj } from '@teamix/utils';
import schemaNameMap from './schemaNameMap';
/**
 * 映射字段size，主要是default转medium
 * @param props component的props
 * @param field 当前字段实例
 * @returns 映射后的字段属性
 */

var mapSize = function mapSize(props, field) {
  var layout = _objectSpread(_objectSpread({}, useFormShallowLayout()), useFormLayout());

  var takeSize = function takeSize() {
    return layout.size === 'default' ? 'medium' : layout.size;
  };

  return _objectSpread(_objectSpread({}, props), {}, {
    size: props.size || takeSize()
  });
};
/**
 * 映射字段state
 * @param props component的props
 * @param field 当前字段实例
 * @returns 映射后的字段属性
 */


var mapStatus = function mapStatus(props, field) {
  var _field$data;

  var takeStatus = function takeStatus() {
    var _field$warnings, _field$decoratorProps, _field$decoratorProps2;

    if (!field) return;
    if (field['loading'] || field['validating']) return 'loading';
    if (field['invalid']) return 'error';
    if ((_field$warnings = field['warnings']) === null || _field$warnings === void 0 ? void 0 : _field$warnings.length) return 'warning';
    if (((_field$decoratorProps = field.decoratorProps) === null || _field$decoratorProps === void 0 ? void 0 : _field$decoratorProps.feedbackStatus) === 'pending') return 'loading'; // 新增

    return (_field$decoratorProps2 = field.decoratorProps) === null || _field$decoratorProps2 === void 0 ? void 0 : _field$decoratorProps2.feedbackStatus;
  };

  var takeState = function takeState(state) {
    if (state === 'validating' || state === 'pending') return 'loading';
    return state;
  };

  var state = takeState(props.state) || takeStatus();
  var result = {
    state: state
  }; // 当组件处于loading状态时，仅可读不可写。

  if (state === 'loading' && ((_field$data = field.data) === null || _field$data === void 0 ? void 0 : _field$data.readOnlyOnLoading)) {
    result.readOnly = true;
  }

  return _objectSpread(_objectSpread({}, props), result);
};

var patchDataSource = function patchDataSource() {
  var dataSource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  var removeEmptyChildren = function removeEmptyChildren(data) {
    var result = _objectSpread({}, data);

    if (!result.children || result.children.length === 0) {
      delete result.children;
    } else {
      result.children = result.children.map(removeEmptyChildren);
    }

    return result;
  };

  return isArr(dataSource) ? dataSource.map(removeEmptyChildren) : [];
};

var mapDataSource = function mapDataSource(props, field) {
  var _props$dataSource;

  if (isVoidField(field)) {
    return props;
  }

  return _objectSpread(_objectSpread({}, props), {}, {
    dataSource: patchDataSource((_props$dataSource = props.dataSource) !== null && _props$dataSource !== void 0 ? _props$dataSource : field === null || field === void 0 ? void 0 : field.dataSource)
  });
};

var mapMode = function mapMode(props, field) {
  return _objectSpread(_objectSpread({}, props), {}, {
    mode: (field === null || field === void 0 ? void 0 : field.readPretty) ? 'read' : 'edit'
  });
};

var getFieldName = function getFieldName() {
  var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'FieldName';
  var suffix = arguments.length > 1 ? arguments[1] : undefined;
  return "".concat(prefix, "-").concat(suffix);
};
/**
 * 将ProForm的key映射为formily的key，然后筛除掉无效的key/value
 * @param obj
 * @returns
 */


var mapSchemaName = function mapSchemaName(obj) {
  var temp = {};
  Object.entries(obj).map(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        key = _ref2[0],
        value = _ref2[1];

    var newKey = getValueByValue(schemaNameMap, key, {
      inputKey: 'proForm',
      outputKey: 'formily'
    }) || key;

    if (value !== undefined && value !== null) {
      if (isArr(value)) {
        if (value.length) {
          temp[newKey] = value;
        }
      } else if (isObj(value)) {
        if (Object.keys(value).length) {
          temp[newKey] = value;
        }
      } else {
        temp[newKey] = value;
      }
    }
  });
  return temp;
};
/**
 * 合并数组或非数组，不移除无效值
 * @param arr 数组
 * @param v 数组或其他
 * @returns 合并数组或合并其他值
 */


var mergeArrayValue = function mergeArrayValue(arr, v) {
  if (isArr(v)) {
    return [].concat(_toConsumableArray(arr), _toConsumableArray(v));
  }

  return [].concat(_toConsumableArray(arr), [v]);
};
/**
 * 初始化请求配置为表达式
 * @param config 请求配置
 * @param suffix scope名称后缀
 * @returns 表达式和scope上下文
 */


var initializeRequestExpression = function initializeRequestExpression(config, mode, suffix) {
  if (isPlainObj(config)) {
    var requestConfigName = "$requestConfig_".concat(suffix);
    return {
      expression: "{{".concat(mode, "($request,context,").concat(requestConfigName, ")}}"),
      scope: _defineProperty({}, requestConfigName, config)
    };
  }

  return {};
};

export { mapSize, mapStatus, mapDataSource, mapMode, mergeArrayValue, getFieldName, mapSchemaName, initializeRequestExpression };