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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState } from 'react';
import { isArr, isUndefined, locales } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
import { Tag, Tooltip } from 'antd';
import { useRequest } from 'ahooks';
import useEngineContext from "../../../../hooks/useEngineContext";
import { getFormData } from "../serve";
import "./index.less";
var RenderValueById = function RenderValueById(props) {
  var _field$setting, _ref, _ref$toString, _field$setting6;
  var field = props.field,
    propsValue = props.value;
  var _useEngineContext = useEngineContext(),
    appHelper = _useEngineContext.appHelper;

  // 关联表单展示数量
  var _useState = useState(10),
    _useState2 = _slicedToArray(_useState, 2),
    displayCount = _useState2[0],
    setDisplayCount = _useState2[1];
  var _getFormData = useRequest(getFormData, {
    manual: true,
    cacheKey: "formOption-".concat((_field$setting = field.setting) === null || _field$setting === void 0 ? void 0 : _field$setting.linkedTable, "-").concat((_ref = propsValue || []) === null || _ref === void 0 || (_ref$toString = _ref.toString) === null || _ref$toString === void 0 ? void 0 : _ref$toString.call(_ref)),
    staleTime: 500,
    cacheTime: 5000
  });
  var value = useMemo(function () {
    var v = propsValue;
    if (field.filed_type === 'form_to_form') {
      if (isArr(propsValue)) {
        v = propsValue;
      } else if (propsValue !== null && propsValue !== void 0 && propsValue.form_ids) {
        v = propsValue.form_ids;
      } else {
        v = propsValue;
      }
    }
    return v;
  }, [propsValue]);
  useEffect(function () {
    var _field$setting2;
    if (field.filed_type === 'form_to_form' && (_field$setting2 = field.setting) !== null && _field$setting2 !== void 0 && _field$setting2.linkedTable) {
      var _field$setting3;
      _getFormData.run({
        form_record_ids: value,
        form_id: (_field$setting3 = field.setting) === null || _field$setting3 === void 0 ? void 0 : _field$setting3.linkedTable,
        skip: 1,
        num: 999
      });
    }
  }, [field.filed_type, value]);
  var options = useMemo(function () {
    var _field$setting4, _field$setting5;
    if (field.filed_type === 'form_to_form' && field !== null && field !== void 0 && (_field$setting4 = field.setting) !== null && _field$setting4 !== void 0 && _field$setting4.linkedTable) {
      var _getFormData$data;
      return ((_getFormData$data = _getFormData.data) === null || _getFormData$data === void 0 || (_getFormData$data = _getFormData$data.data) === null || _getFormData$data === void 0 || (_getFormData$data = _getFormData$data.list) === null || _getFormData$data === void 0 ? void 0 : _getFormData$data.map(function (item) {
        return {
          title: item.main_field,
          id: item.form_record_id
        };
      })) || [];
    }
    return field === null || field === void 0 || (_field$setting5 = field.setting) === null || _field$setting5 === void 0 ? void 0 : _field$setting5.customTypeList;
  }, [field.filed_type, field === null || field === void 0 || (_field$setting6 = field.setting) === null || _field$setting6 === void 0 ? void 0 : _field$setting6.customTypeList, _getFormData.data]);
  if (isArr(value)) {
    var _field$setting7;
    // 是否是关联表单列表展示样式
    var formListDisplay = (field === null || field === void 0 || (_field$setting7 = field.setting) === null || _field$setting7 === void 0 ? void 0 : _field$setting7.displayStyle) === 'list' && (field === null || field === void 0 ? void 0 : field.filed_type) === 'form_to_form';
    var _list = options === null || options === void 0 ? void 0 : options.filter(function (i) {
      return value === null || value === void 0 ? void 0 : value.includes((i === null || i === void 0 ? void 0 : i.id) || i.value);
    });
    var _str = !formListDisplay && (_list === null || _list === void 0 ? void 0 : _list.map(function (i) {
      var _appHelper$utils;
      var title = appHelper === null || appHelper === void 0 || (_appHelper$utils = appHelper.utils) === null || _appHelper$utils === void 0 ? void 0 : _appHelper$utils.translationShop((i === null || i === void 0 ? void 0 : i.title) || (i === null || i === void 0 ? void 0 : i.label));
      return /*#__PURE__*/React.createElement(Tooltip, {
        title: title
      }, /*#__PURE__*/React.createElement(Tag, {
        className: "pisell-lowcode__booking-form-link-tag-title"
      }, title));
    })) || '-';

    // 如果列表式展示
    if (formListDisplay) {
      var displayList = (_list === null || _list === void 0 ? void 0 : _list.slice(0, displayCount)) || [];
      _str = (displayList === null || displayList === void 0 ? void 0 : displayList.map(function (i, index) {
        var _appHelper$utils2;
        var title = appHelper === null || appHelper === void 0 || (_appHelper$utils2 = appHelper.utils) === null || _appHelper$utils2 === void 0 ? void 0 : _appHelper$utils2.translationShop((i === null || i === void 0 ? void 0 : i.title) || (i === null || i === void 0 ? void 0 : i.label));
        return /*#__PURE__*/React.createElement("div", {
          className: "pisell-lowcode__booking-form-link-to-list-item"
        }, /*#__PURE__*/React.createElement("span", {
          className: "pisell-lowcode__booking-form-link-to-list-item-index"
        }, index + 1), /*#__PURE__*/React.createElement(Tooltip, {
          title: title
        }, /*#__PURE__*/React.createElement("span", {
          className: "pisell-lowcode__booking-form-link-to-list-item-title"
        }, title)));
      })) || '-';
    }
    return /*#__PURE__*/React.createElement("span", {
      className: formListDisplay ? 'pisell-lowcode__booking-form-link-to-list-wrap' : ''
    }, _str || '-', formListDisplay && (_list === null || _list === void 0 ? void 0 : _list.length) > displayCount && /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__booking-form-link-to-list-item-more",
      onClick: function onClick() {
        return setDisplayCount(function (prev) {
          return Math.min(prev + 10, (_list === null || _list === void 0 ? void 0 : _list.length) || 0);
        });
      }
    }, /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.text.form.link.to.another.record.more')), /*#__PURE__*/React.createElement(Iconfont, {
      type: "icona-IconStroke"
    })));
  }
  if (!isUndefined(value)) {
    var _field$setting8, _appHelper$utils3, _item$, _item$2;
    if ((field === null || field === void 0 ? void 0 : field.filed_type) === 'select' && !(field !== null && field !== void 0 && (_field$setting8 = field.setting) !== null && _field$setting8 !== void 0 && _field$setting8.version)) {
      return /*#__PURE__*/React.createElement("span", null, value || '-');
    }
    var _item = options === null || options === void 0 ? void 0 : options.filter(function (i) {
      return (i === null || i === void 0 ? void 0 : i.id) == value;
    });
    return (appHelper === null || appHelper === void 0 || (_appHelper$utils3 = appHelper.utils) === null || _appHelper$utils3 === void 0 ? void 0 : _appHelper$utils3.translationShop((_item === null || _item === void 0 || (_item$ = _item[0]) === null || _item$ === void 0 ? void 0 : _item$.title) || (_item === null || _item === void 0 || (_item$2 = _item[0]) === null || _item$2 === void 0 ? void 0 : _item$2.label))) || '-';
  }
  return null;
};
export default RenderValueById;