import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _extends from "@babel/runtime/helpers/extends";
import _Form from "antd/es/form";
import React, { useEffect } from "react";
import { PageStateFlag } from "../../../lowcode/_utils/treeDataRelationFn/type";
import { CLowCodeService } from "../../utils/CLowCodeService";
import { PageStateService } from "../../utils/CLowCodeService/pageStateService";
import { toNumber } from "lodash";
import BigNumberService from "../../utils/bignumber";
import { EncryptionType } from "../../utils/type";
import { encryptionMd5, getDecimalsData, getFormItemComponentKey, isEffectiveValue } from "../../utils/comfn/util";
import { getFormItemValue, isWalletInfo } from "./comFn";
import { WalletService } from "../../utils/_wallet";
import { groupSubService } from "../../utils/comfn/groupSubService";
var FormPro = function FormPro(props, ref) {
  var children = props.children;
  var pageStateService = new PageStateService(props.webGid);
  var cLowCodeService = new CLowCodeService();
  var walletService = new WalletService();
  cLowCodeService.loadPageProps(props);
  var _Form$useForm = _Form.useForm(),
    form = _Form$useForm[0];
  useEffect(function () {
    if (props.__designMode !== "design" && children) {
      if (isWalletInfo(children)) {
        walletService.watchWalletConnect(function (singer) {
          initFormItemValue();
        });
      } else {
        initFormItemValue();
      }
    }
  }, []);
  useEffect(function () {
    if (props.__designMode !== "design" && children) {
      var subKeys = [];
      children.forEach(function (item) {
        if (item.props.name) {
          // 提供给改值时，修正表单的显示
          var key = getFormItemComponentKey(props.webGid, item.props.name);
          subKeys.push(key);
          groupSubService.subScribeOwn(key, function (res) {
            if (res.name === item.props.name) {
              var _form$setFieldsValue;
              form.setFieldsValue((_form$setFieldsValue = {}, _form$setFieldsValue[item.props.name] = res.data, _form$setFieldsValue));
            }
          });
        }
      });
      return function () {
        if (subKeys.length) {
          subKeys.forEach(function (key) {
            groupSubService.unsubScribeOwn(key);
          });
        }
      };
    }
  }, []);
  var initFormItemValue = function initFormItemValue() {
    var formItemValue = getFormItemValue(children, cLowCodeService);
    var keysData = Object.keys(formItemValue);
    if (keysData.length) {
      form.setFieldsValue(formItemValue);
      keysData.forEach(function (key) {
        var newValue = formItemValue[key];
        pageStateService.setPageStateData(key, newValue, PageStateFlag.State);
      });
    }
  };
  function formChange(e, allData) {
    // 将数据存储到全局
    Object.keys(allData).forEach(function (key, index) {
      var newValue = handleDecimals(allData[key], index);
      pageStateService.setPageStateData(key, newValue, PageStateFlag.State);
    });
    // 这里需要使用发布，更新默认依赖项
  }
  function handleDecimals(value, index) {
    var result = value;
    if (isEffectiveValue(value) && props.children && props.children[index]) {
      var _props$children$index, _props$children$index2;
      var decimalsData = (_props$children$index = props.children[index].props) === null || _props$children$index === void 0 ? void 0 : _props$children$index.decimals;
      // 精度处理
      if (!isNaN(toNumber(value)) && decimalsData) {
        var childProps = getDecimalsData(cLowCodeService, decimalsData);
        if (childProps > 0) {
          result = new BigNumberService(value).multipliedBy(new BigNumberService(10).pow(childProps)).toFormat();
        }
      }

      // 加密处理
      if ((_props$children$index2 = props.children[index].props) !== null && _props$children$index2 !== void 0 && _props$children$index2.encryption) {
        var _props$children$index3;
        var encryption = (_props$children$index3 = props.children[index].props) === null || _props$children$index3 === void 0 ? void 0 : _props$children$index3.encryption;
        if (encryption === EncryptionType.md5) {
          result = encryptionMd5(value);
        }
      }
    }
    return result;
  }
  return /*#__PURE__*/React.createElement(_Form, _extends({
    form: form,
    onValuesChange: formChange
  }, props), children);
};
var _default = /*#__PURE__*/function (_React$Component) {
  function _default() {
    return _React$Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _React$Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(FormPro, _extends({
      className: "loginBox"
    }, this.props));
  };
  return _default;
}(React.Component);
export { _default as default };