var _excluded = ["form", "initialValues", "initialRequest", "scope", "context", "components", "onChange", "onChangeType", "onSubmit", "onSubmitFailed", "onInitialComplete", "className", "validateFirst", "schema", "children"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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, { memo, useEffect, useMemo, useRef } from 'react';
import { usePrefixCls, cls, getLanguage, getGlobalConfig } from '@teamix/utils';
import { createForm, onFieldValueChange, onFormValuesChange, registerValidateLocale, setValidateLanguage } from '@formily/core';
import { toJS } from '@formily/reactive';
import { Form, Upload, SelectTable, FormLayout, FormItem, ArrayCollapse, ArrayCards, ArrayItems, ArrayTable, Space, FormGrid, FormTab, FormCollapse, FormStep, Editable, FormDialog, FormDrawer, FormButtonGroup, Submit, Reset } from '@teamix/formily';
import { Affix } from '@alicloudfe/components';
import ProFieldComponents from '../Components/ProField';
import addCascadeEffect from './addCascadeEffect';
import { EditableDialog, EditableDrawer, EditablePopover } from '../Components/Editable';
import FormGroup from '../Components/FormGroup';
import LightFilter from '../Components/LightFilter';
import Text from '../Components/Text/index';
import SchemaForm from '../SchemaForm';
import useAutoSubmit from './useAutoSubmit';
import useAutoLayout from './useAutoLayout';
import useInitialRequest from './useInitialRequest';
import validateLocale from '../locales/validate';
import './index.scss';
registerValidateLocale(validateLocale);
var formilyComponents = {
  FormLayout: FormLayout,
  FormItem: FormItem,
  ArrayCollapse: ArrayCollapse,
  ArrayCards: ArrayCards,
  ArrayItems: ArrayItems,
  ArrayTable: ArrayTable,
  Space: Space,
  FormGrid: FormGrid,
  FormTab: FormTab,
  FormCollapse: FormCollapse,
  FormStep: FormStep,
  Editable: Editable,
  FormDialogFooter: FormDialog.Footer,
  FormDrawerFooter: FormDrawer.Footer,
  FormButtonGroup: FormButtonGroup,
  Submit: Submit,
  Reset: Reset,
  EditableDialog: EditableDialog,
  EditableDrawer: EditableDrawer,
  EditablePopover: EditablePopover,
  FormGroup: FormGroup,
  Upload: Upload,
  SelectTable: SelectTable,
  Affix: Affix,
  LightFilter: LightFilter,
  Text: Text
};
var ProForm = /*#__PURE__*/memo(function (_ref) {
  var outerForm = _ref.form,
    initialValues = _ref.initialValues,
    initialRequest = _ref.initialRequest,
    scope = _ref.scope,
    context = _ref.context,
    components = _ref.components,
    onChange = _ref.onChange,
    onChangeType = _ref.onChangeType,
    onSubmit = _ref.onSubmit,
    onSubmitFailed = _ref.onSubmitFailed,
    onInitialComplete = _ref.onInitialComplete,
    className = _ref.className,
    validateFirst = _ref.validateFirst,
    schema = _ref.schema,
    children = _ref.children,
    otherProps = _objectWithoutProperties(_ref, _excluded);
  var formRef = useRef();
  // 获取全局配置
  var _ref2 = getGlobalConfig('ProForm') || {},
    globalComponents = _ref2.components,
    globalScope = _ref2.scope;
  var mergedScope = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, context), globalScope), scope), {}, {
    context: context
  });
  var form = useMemo(function () {
    var nextForm = outerForm || createForm({
      validateFirst: validateFirst
    });
    addCascadeEffect(nextForm, schema, mergedScope);
    return nextForm;
  }, [outerForm]);
  var prefixCls = usePrefixCls('teamix-pro-form');
  // 由于Actions需要，解构context供使用。原context键名的上下文继续保留。
  var mergedComponents = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, globalComponents), components), formilyComponents), ProFieldComponents);
  var onAutoSubmit = useAutoSubmit(onSubmit, context);
  var autoTeamixLayout = useAutoLayout(_objectSpread(_objectSpread({}, otherProps), {}, {
    schema: schema
  }), formRef);
  useMemo(function () {
    // 配置表单默认值
    if (initialValues) {
      form.setInitialValues(initialValues);
    } else if (initialRequest) {
      useInitialRequest(initialRequest, context).then(function (values) {
        form.setInitialValues(values);
      }).finally(function () {
        onInitialComplete === null || onInitialComplete === void 0 ? void 0 : onInitialComplete(form);
      });
    }
  }, []);
  // 添加 onChange 事件
  useMemo(function () {
    if (onChangeType === 'form') {
      // 使用 onFormValuesChange 无法监听具体变化字段
      form.removeEffects('onChangeByFormValuesChange');
      form.addEffects('onChangeByFormValuesChange', function () {
        onFormValuesChange(function (form) {
          onChange === null || onChange === void 0 ? void 0 : onChange(toJS(form.values));
        });
      });
    } else {
      // 使用 onFieldValueChange 无法监听数组类型值的增删
      form.removeEffects('onChangeByFieldValueChange');
      form.addEffects('onChangeByFieldValueChange', function () {
        onFieldValueChange('*', function (field, form) {
          var _field$props;
          onChange === null || onChange === void 0 ? void 0 : onChange(toJS(form.values), toJS(field.value), (_field$props = field.props) === null || _field$props === void 0 ? void 0 : _field$props.name);
        });
      });
    }
  }, [onChange, form]);
  useEffect(function () {
    // 配置国际化
    setValidateLanguage(getLanguage() || 'zh-cn');
  }, []);
  return /*#__PURE__*/React.createElement(Form, _objectSpread(_objectSpread(_objectSpread({
    className: cls(prefixCls(), className)
  }, otherProps), autoTeamixLayout), {}, {
    form: form,
    onAutoSubmit: onAutoSubmit,
    onAutoSubmitFailed: onSubmitFailed
  }), schema ? /*#__PURE__*/React.createElement(SchemaForm, {
    schema: schema,
    scope: mergedScope,
    components: mergedComponents
  }) : null, /*#__PURE__*/React.createElement("div", {
    ref: formRef
  }), children);
});
ProForm.defaultProps = {
  colon: false,
  validateFirst: true,
  breakpoints: true,
  labelAlign: 'left'
};
export default ProForm;