var _excluded = ["title", "description", "dataSource", "showTitle", "showDescription", "groupInfoPosition", "variables", "renderMode", "children", "currentValue", "className"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
import classNames from 'classnames';
import useDataSource from "../hooks/useDataSource";
import Form from "../../form";
import FormSettingProvider from "./provider/formSetting/FormSettingProvider";
import "./index.less";

/**
 * @title: 数据源表单组件
 * @description: 基于数据源动态生成表单，支持编辑、查看、禁用三种模式
 */
var BaseForm = /*#__PURE__*/forwardRef(function (props, ref) {
  var title = props.title,
    description = props.description,
    dataSource = props.dataSource,
    showTitle = props.showTitle,
    showDescription = props.showDescription,
    groupInfoPosition = props.groupInfoPosition,
    variables = props.variables,
    renderMode = props.renderMode,
    children = props.children,
    currentValue = props.currentValue,
    className = props.className,
    others = _objectWithoutProperties(props, _excluded);
  var formRef = useRef(null);
  var _useDataSource = useDataSource(),
    get = _useDataSource.get;
  useEffect(function () {
    var _formRef$current;
    (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.resetFields();
  }, [dataSource]);
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  useImperativeHandle(ref, function () {
    return formRef.current;
  });
  useEffect(function () {
    if (['edit', 'view'].includes(renderMode || '') && dataSource) {
      get === null || get === void 0 ? void 0 : get.runAsync().then(function (res) {
        var _formRef$current2;
        (_formRef$current2 = formRef.current) === null || _formRef$current2 === void 0 ? void 0 : _formRef$current2.setFieldsValue(res);
      });
    }
  }, [renderMode, dataSource]);
  return /*#__PURE__*/React.createElement(FormSettingProvider, {
    renderMode: renderMode,
    groupInfoPosition: groupInfoPosition,
    currentValue: currentValue,
    size: others.size
  }, /*#__PURE__*/React.createElement(Form, _extends({}, others, {
    className: classNames('pisell-lowcode-data-source-form', className),
    ref: formRef,
    preserve: true
  }), (title || description) && /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-data-source-form-header"
  }, showTitle && /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-data-source-form-title"
  }, title), showDescription && /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-data-source-form-description"
  }, description)), children));
});
export default BaseForm;