"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _utils = require("@teamix/utils");
var _core = require("@formily/core");
var _reactive = require("@formily/reactive");
var _formily = require("@teamix/formily");
var _components = require("@alicloudfe/components");
var _ProField = _interopRequireDefault(require("../Components/ProField"));
var _addCascadeEffect = _interopRequireDefault(require("./addCascadeEffect"));
var _Editable = require("../Components/Editable");
var _FormGroup = _interopRequireDefault(require("../Components/FormGroup"));
var _LightFilter = _interopRequireDefault(require("../Components/LightFilter"));
var _index = _interopRequireDefault(require("../Components/Text/index"));
var _SchemaForm = _interopRequireDefault(require("../SchemaForm"));
var _useAutoSubmit = _interopRequireDefault(require("./useAutoSubmit"));
var _useAutoLayout = _interopRequireDefault(require("./useAutoLayout"));
var _useInitialRequest = _interopRequireDefault(require("./useInitialRequest"));
var _validate = _interopRequireDefault(require("../locales/validate"));
require("./index.scss");
var _excluded = ["form", "initialValues", "initialRequest", "scope", "context", "components", "onChange", "onChangeType", "onSubmit", "onSubmitFailed", "onInitialComplete", "className", "validateFirst", "schema", "children"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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; }
(0, _core.registerValidateLocale)(_validate.default);
var formilyComponents = {
  FormLayout: _formily.FormLayout,
  FormItem: _formily.FormItem,
  ArrayCollapse: _formily.ArrayCollapse,
  ArrayCards: _formily.ArrayCards,
  ArrayItems: _formily.ArrayItems,
  ArrayTable: _formily.ArrayTable,
  Space: _formily.Space,
  FormGrid: _formily.FormGrid,
  FormTab: _formily.FormTab,
  FormCollapse: _formily.FormCollapse,
  FormStep: _formily.FormStep,
  Editable: _formily.Editable,
  FormDialogFooter: _formily.FormDialog.Footer,
  FormDrawerFooter: _formily.FormDrawer.Footer,
  FormButtonGroup: _formily.FormButtonGroup,
  Submit: _formily.Submit,
  Reset: _formily.Reset,
  EditableDialog: _Editable.EditableDialog,
  EditableDrawer: _Editable.EditableDrawer,
  EditablePopover: _Editable.EditablePopover,
  FormGroup: _FormGroup.default,
  Upload: _formily.Upload,
  SelectTable: _formily.SelectTable,
  Affix: _components.Affix,
  LightFilter: _LightFilter.default,
  Text: _index.default
};
var ProForm = /*#__PURE__*/(0, _react.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 = (0, _react.useRef)();
  // 获取全局配置
  var _ref2 = (0, _utils.getGlobalConfig)('ProForm') || {},
    globalComponents = _ref2.components,
    globalScope = _ref2.scope;
  var mergedScope = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, context), globalScope), scope), {}, {
    context: context
  });
  var form = (0, _react.useMemo)(function () {
    var nextForm = outerForm || (0, _core.createForm)({
      validateFirst: validateFirst
    });
    (0, _addCascadeEffect.default)(nextForm, schema, mergedScope);
    return nextForm;
  }, [outerForm]);
  var prefixCls = (0, _utils.usePrefixCls)('teamix-pro-form');
  // 由于Actions需要，解构context供使用。原context键名的上下文继续保留。
  var mergedComponents = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, globalComponents), components), formilyComponents), _ProField.default);
  var onAutoSubmit = (0, _useAutoSubmit.default)(onSubmit, context);
  var autoTeamixLayout = (0, _useAutoLayout.default)(_objectSpread(_objectSpread({}, otherProps), {}, {
    schema: schema
  }), formRef);
  (0, _react.useMemo)(function () {
    // 配置表单默认值
    if (initialValues) {
      form.setInitialValues(initialValues);
    } else if (initialRequest) {
      (0, _useInitialRequest.default)(initialRequest, context).then(function (values) {
        form.setInitialValues(values);
      }).finally(function () {
        onInitialComplete === null || onInitialComplete === void 0 ? void 0 : onInitialComplete(form);
      });
    }
  }, []);
  // 添加 onChange 事件
  (0, _react.useMemo)(function () {
    if (onChangeType === 'form') {
      // 使用 onFormValuesChange 无法监听具体变化字段
      form.removeEffects('onChangeByFormValuesChange');
      form.addEffects('onChangeByFormValuesChange', function () {
        (0, _core.onFormValuesChange)(function (form) {
          onChange === null || onChange === void 0 ? void 0 : onChange((0, _reactive.toJS)(form.values));
        });
      });
    } else {
      // 使用 onFieldValueChange 无法监听数组类型值的增删
      form.removeEffects('onChangeByFieldValueChange');
      form.addEffects('onChangeByFieldValueChange', function () {
        (0, _core.onFieldValueChange)('*', function (field, form) {
          var _field$props;
          onChange === null || onChange === void 0 ? void 0 : onChange((0, _reactive.toJS)(form.values), (0, _reactive.toJS)(field.value), (_field$props = field.props) === null || _field$props === void 0 ? void 0 : _field$props.name);
        });
      });
    }
  }, [onChange, form]);
  (0, _react.useEffect)(function () {
    // 配置国际化
    (0, _core.setValidateLanguage)((0, _utils.getLanguage)() || 'zh-cn');
  }, []);
  return /*#__PURE__*/_react.default.createElement(_formily.Form, _objectSpread(_objectSpread(_objectSpread({
    className: (0, _utils.cls)(prefixCls(), className)
  }, otherProps), autoTeamixLayout), {}, {
    form: form,
    onAutoSubmit: onAutoSubmit,
    onAutoSubmitFailed: onSubmitFailed
  }), schema ? /*#__PURE__*/_react.default.createElement(_SchemaForm.default, {
    schema: schema,
    scope: mergedScope,
    components: mergedComponents
  }) : null, /*#__PURE__*/_react.default.createElement("div", {
    ref: formRef
  }), children);
});
ProForm.defaultProps = {
  colon: false,
  validateFirst: true,
  breakpoints: true,
  labelAlign: 'left'
};
var _default = ProForm;
exports.default = _default;