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 _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

// @ts-nocheck
import { fetch } from "@fishx/rest";
import message from "antd/es/message";
export function toArray(candidate) {
  if (candidate === undefined || candidate === false) return [];
  return Array.isArray(candidate) ? candidate : [candidate];
}
export function getFieldId(namePath, formName) {
  if (!namePath.length) return undefined;
  var mergedId = namePath.join('_');
  return formName ? "".concat(formName, "_").concat(mergedId) : mergedId;
} // 请求远程数据源

export function remoteSourceHandler(_x, _x2, _x3, _x4) {
  return _remoteSourceHandler.apply(this, arguments);
}

function _remoteSourceHandler() {
  _remoteSourceHandler = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(options, relates, values, locale) {
    var url, _options$method, method, _options$params, params, fetchUrl, value, _yield$fetch, data;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(options && options.url)) {
              _context.next = 21;
              break;
            }

            url = options.url, _options$method = options.method, method = _options$method === void 0 ? 'GET' : _options$method, _options$params = options.params, params = _options$params === void 0 ? {} : _options$params;
            fetchUrl = url;

            if (!(url.indexOf('${value}') > -1)) {
              _context.next = 8;
              break;
            }

            value = values[relates[0]];

            if (!(value === null || value === undefined)) {
              _context.next = 7;
              break;
            }

            return _context.abrupt("return", false);

          case 7:
            fetchUrl = fetchUrl.replace('${value}', value);

          case 8:
            _context.prev = 8;
            _context.next = 11;
            return fetch(fetchUrl, {
              method: method,
              data: _objectSpread({}, params)
            });

          case 11:
            _yield$fetch = _context.sent;
            data = _yield$fetch.data;

            if (!data) {
              _context.next = 15;
              break;
            }

            return _context.abrupt("return", data);

          case 15:
            _context.next = 21;
            break;

          case 17:
            _context.prev = 17;
            _context.t0 = _context["catch"](8);
            message.error(locale.requestFail);
            return _context.abrupt("return", []);

          case 21:
            return _context.abrupt("return", options || []);

          case 22:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[8, 17]]);
  }));
  return _remoteSourceHandler.apply(this, arguments);
}

export function handleFieldSource(name, options, wrapForm) {
  wrapForm.setFieldSource(_defineProperty({}, name, options)); // const currentValue = wrapForm.getFieldValue(name);
  // const exit = options.findIndex(v => v.value === currentValue) > -1;
  // // 如果下拉数据源内不包含当前值，则清空当前值
  // if (!exit) {
  //   wrapForm.setFieldsValue({[name]: null});
  // }
}
export function cascadeHandler(wrapForm, allValues, allDependency) {
  var currentItem = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';

  function cascade(relates, inputs, output, defaultOutput, key, item) {
    var res = relates.some(function (v, i) {
      if (inputs[i] === '*') {
        return !allValues[v];
      } else {
        if (allValues[v] && _typeof(allValues[v]) === 'object') {
          var value = Array.isArray(allValues[v]) ? allValues[v] : Object.values(allValues[v]);

          var _res = value.some(function (val) {
            return inputs[i].indexOf(val) !== -1;
          });

          return !_res;
        } else {
          return inputs[i].indexOf(allValues[v]) === -1;
        }
      }
    });

    if (key === 'value') {
      wrapForm.setFieldValue(_defineProperty({}, item, res ? defaultOutput : output));
    }

    if (key === 'source') {
      var _wrapForm$__INTERNAL_ = wrapForm.__INTERNAL__.locale,
          locale = _wrapForm$__INTERNAL_ === void 0 ? {} : _wrapForm$__INTERNAL_;

      if (res) {
        remoteSourceHandler(defaultOutput, relates, allValues, locale).then(function (res) {
          if (res) {
            handleFieldSource(item, res, wrapForm);
          }
        });
      } else {
        remoteSourceHandler(output, relates, allValues, locale).then(function (res) {
          if (res) {
            handleFieldSource(item, res, wrapForm);
          }
        });
      }
    }

    if (key === 'disabled') {
      wrapForm.setFieldDisabled(_defineProperty({}, item, res ? defaultOutput : output));
    }

    if (key === 'visible') {
      wrapForm.setFieldVisible(_defineProperty({}, item, res ? defaultOutput : output));
    }

    if (key === 'required') {
      wrapForm.setFieldRequired(_defineProperty({}, item, res ? defaultOutput : output));
    }

    return !res;
  }

  Object.keys(allValues).forEach(function (item) {
    var dependency = allDependency[item];

    if (dependency) {
      Object.keys(dependency).forEach(function (key) {
        var dep = dependency[key];
        var relates = dep.relates,
            inputs = dep.inputs,
            output = dep.output,
            defaultOutput = dep.defaultOutput,
            _dep$cases = dep.cases,
            cases = _dep$cases === void 0 ? [] : _dep$cases;

        if (currentItem && relates.indexOf(currentItem) !== -1 || !currentItem) {
          if (cases.length) {
            cases.some(function (v) {
              return cascade(relates, v.inputs, v.output, defaultOutput, key, item);
            });
          } else {
            cascade(relates, inputs, output, defaultOutput, key, item);
          }
        }
      });
    }
  });
}