var _excluded = ["extraConfig", "onComplete"];
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { isFn, isPlainObj, doCommonRequest, getValueByValue } from '@teamix/utils';
// 配置请求
var $request = function $request(field, context, _ref, type) {
  var _field$data;
  var extraConfig = _ref.extraConfig,
    onComplete = _ref.onComplete,
    rest = _objectWithoutProperties(_ref, _excluded);
  var requestConfig = rest;
  var params = requestConfig.params,
    beforeRequest = requestConfig.beforeRequest,
    formatResult = requestConfig.formatResult,
    onSuccess = requestConfig.onSuccess;
  var beforeRequestResult = isFn(beforeRequest) ? beforeRequest(field, context) : {};
  // 通过字段 data 的 refresh 主动触发的请求刷新
  var refresh = (_field$data = field.data) === null || _field$data === void 0 ? void 0 : _field$data.refresh;
  var refreshResult = isPlainObj(refresh) ? refresh : {};
  // 合并请求参数
  var mergedParams = _objectSpread(_objectSpread(_objectSpread({}, params), beforeRequestResult), refreshResult);
  // 执行通用数据请求
  return doCommonRequest(_objectSpread(_objectSpread({}, requestConfig), {}, {
    params: mergedParams,
    beforeRequest: function beforeRequest() {
      return beforeRequestResult;
    },
    formatResult: isFn(formatResult) ? function (res) {
      return formatResult(res, field, context);
    } : formatResult,
    onSuccess: isFn(onSuccess) ? function (res) {
      return onSuccess(res, field, context);
    } : onSuccess
  }), _objectSpread(_objectSpread({}, context), {}, {
    field: field,
    params: mergedParams
  }));
};
// 通用请求配置上下文
var $common = function $common(services, context, requestConfig) {
  return function (field) {
    return services(field, context, requestConfig, 'request').then(function (res) {
      var _requestConfig$onComp;
      return requestConfig === null || requestConfig === void 0 ? void 0 : (_requestConfig$onComp = requestConfig.onComplete) === null || _requestConfig$onComp === void 0 ? void 0 : _requestConfig$onComp.call(requestConfig, res, field, context);
    });
  };
};
// 配置校验
var $validator = function $validator(services, context, requestConfig) {
  return function (value, rules, _ref2) {
    var field = _ref2.field;
    return services(field, context, requestConfig, 'validator').then(function (res) {
      if (requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.onComplete) {
        return requestConfig.onComplete(res, field, context);
      } else {
        return res;
      }
    });
  };
};
// 配置DataSource
var $dataSource = function $dataSource(services, context, requestConfig) {
  return function (field) {
    field.setState({
      loading: true
    });
    return services(field, context, requestConfig, 'dataSource').then(function (res) {
      var _requestConfig$extraC, _requestConfig$onComp2;
      var isValueInDataSource = getValueByValue(res, field.value);
      var clearResult = !isValueInDataSource && (requestConfig === null || requestConfig === void 0 ? void 0 : (_requestConfig$extraC = requestConfig.extraConfig) === null || _requestConfig$extraC === void 0 ? void 0 : _requestConfig$extraC.clearInvalidValue) !== false ? {
        value: undefined
      } : {};
      field.setState(_objectSpread({
        dataSource: res,
        loading: false
      }, clearResult));
      return requestConfig === null || requestConfig === void 0 ? void 0 : (_requestConfig$onComp2 = requestConfig.onComplete) === null || _requestConfig$onComp2 === void 0 ? void 0 : _requestConfig$onComp2.call(requestConfig, res, field, context);
    }).catch(function () {
      field.setState({
        loading: false
      });
    });
  };
};
export { $request, $common, $dataSource, $validator };