import React, { useEffect, useMemo } from "react";
import { isArr, isUndefined } from "@pisell/utils";
import { Tag } from "antd";
import { useRequest } from "ahooks";
import useEngineContext from "../../../../hooks/useEngineContext";
import { getFormData } from "../serve";
var RenderValueById = function RenderValueById(props) {
  var _field$setting, _field$setting6;
  var field = props.field,
    propsValue = props.value;
  var _useEngineContext = useEngineContext(),
    appHelper = _useEngineContext.appHelper;
  var _getFormData = useRequest(getFormData, {
    manual: true,
    cacheKey: "formOption-".concat((_field$setting = field.setting) === null || _field$setting === void 0 ? void 0 : _field$setting.linkedTable),
    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 _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 = (_list === null || _list === void 0 ? void 0 : _list.map(function (i) {
      var _appHelper$utils;
      return /*#__PURE__*/React.createElement(Tag, null, 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("span", null, _str || "-");
  }
  if (!isUndefined(value)) {
    var _field$setting7, _appHelper$utils2, _item$, _item$2;
    if ((field === null || field === void 0 ? void 0 : field.filed_type) === "select" && !(field !== null && field !== void 0 && (_field$setting7 = field.setting) !== null && _field$setting7 !== void 0 && _field$setting7.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$utils2 = appHelper.utils) === null || _appHelper$utils2 === void 0 ? void 0 : _appHelper$utils2.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;