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; }

import { useForm } from '@formily/react';
import { toJS } from '@formily/reactive';
import { isArr, isPlainObj, isUsable, getValueByValue } from '@teamix/utils';
import fieldTypeMap from '../fieldTypeMap';
/**
 * 获取表单值的可显示值
 * @returns 如果某字段有dataSource，则返回value对应label
 * @example const example = {
    a: [
        [{ b: 'a.0.0.b', c: 'a.0.0.c' }, { d: 'a.0.1.d' }],
        { e: [{ f: { g: [{ h: 'a.1.e.0.f.g.0.h' }] } }, 'a.1.e.1'] },
      ],
  };
  console.log(getDisplayValues(example));
 */

var getDisplayValues = function getDisplayValues(form, values) {
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var displayValues; // 拼接路径

  var connectPath = function connectPath(path) {
    return prefix ? "".concat(prefix, ".").concat(path) : "".concat(path);
  }; // 根据值路径获取地址


  var getAddress = function getAddress(path) {
    return form.indexes[path];
  }; // 当前field


  var field = form.query(getAddress(prefix)).take(); // 是否真实字段值（自定义组件、布局组件、自增数组组件为非真实字段）

  var isFieldValue = Object.keys(fieldTypeMap).includes(field === null || field === void 0 ? void 0 : field.component[0]); // 判断值的类型，递归获取每个值在该结构中的完整路径

  if (!isUsable(values)) {
    displayValues = {};
  } else if (isPlainObj(values) && !isFieldValue) {
    // 如果值是布局对象的值
    displayValues = {};
    Object.entries(values).forEach(function (_ref) {
      var _ref2 = _slicedToArray(_ref, 2),
          key = _ref2[0],
          value = _ref2[1];

      displayValues[key] = getDisplayValues(form, value, connectPath(key));
    });
  } else if (isArr(values) && !isFieldValue) {
    // 如果值是自增数组的值
    // ArrayItems为字符串数组时，由于form.indexes中对应address为最后一个组件，无法正常获取displayName
    // 例如：arrayItemsString.0: "arrayItemsString.0.ArrayItemsRemove"
    displayValues = [];
    values.map(function (value, index) {
      displayValues[index] = getDisplayValues(form, value, connectPath(index));
    });
  } else {
    var _values$value;

    // 通过值的完整路径获取字段address（从form实例的indexes中），然后获取该字段的value在该字段dataSource中对应的label
    var realValue = (_values$value = values === null || values === void 0 ? void 0 : values.value) !== null && _values$value !== void 0 ? _values$value : values; // 针对单个字段的值是对象，例如Select的useDetailValue

    var displayValue = getValueByValue(field === null || field === void 0 ? void 0 : field.dataSource, realValue);
    displayValues = {
      title: field === null || field === void 0 ? void 0 : field.title,
      component: field === null || field === void 0 ? void 0 : field.component[0],
      value: values,
      displayValue: displayValue
    };
  }

  return displayValues;
};

var useFormDisplayValues = function useFormDisplayValues() {
  var form = useForm();
  return getDisplayValues(form, toJS(form.values));
};

var getFormDisplayValues = function getFormDisplayValues(form) {
  return getDisplayValues(form, toJS(form.values));
};

export { useFormDisplayValues, getFormDisplayValues };