"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 _classnames = _interopRequireDefault(require("classnames"));

var _input = _interopRequireDefault(require("./form-fields/input"));

var _department = _interopRequireDefault(require("./form-fields/department"));

var _user = _interopRequireDefault(require("./form-fields/user"));

var _duties = _interopRequireDefault(require("./form-fields/duties"));

var _textarea = _interopRequireDefault(require("./form-fields/textarea"));

var _radios = _interopRequireDefault(require("./form-fields/radios"));

var _dropdown = _interopRequireDefault(require("./form-fields/dropdown"));

var _group = _interopRequireDefault(require("./form-fields/group"));

var _inputVerificationCode = _interopRequireDefault(require("./form-fields/input-verification-code"));

var _password = _interopRequireDefault(require("./form-fields/password"));

var _date = _interopRequireDefault(require("./form-fields/date"));

var _cycle = _interopRequireDefault(require("./form-fields/cycle"));

var _layout = require("./layout");

require("./index.css");

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; }

var preCls = "titaui-form";
var Form = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var children = props.children,
      onFieldsChange = props.onFieldsChange,
      getFormContainer = props.getFormContainer,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? "vertical" : _props$direction,
      _props$className = props.className,
      className = _props$className === void 0 ? "" : _props$className;
  var fieldsValue = (0, _react.useRef)({});
  var fieldRefs = (0, _react.useRef)({});

  var onFieldChangeHandler = function onFieldChangeHandler(name, value) {
    fieldsValue.current[name] = value;
    onFieldsChange === null || onFieldsChange === void 0 ? void 0 : onFieldsChange(fieldsValue.current);
  };

  var initFieldsValue = function initFieldsValue(name, value) {
    fieldsValue.current[name] = value;
  };

  var onStoreRef = function onStoreRef(name, ref) {
    fieldRefs.current[name] = ref;
  };

  var getContainer = function getContainer() {
    if (getFormContainer) return getFormContainer();
    return window.document.body;
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      verifing: function verifing(opts) {
        var _ref = opts || {},
            onlyFirstError = _ref.onlyFirstError;

        var errors = [];
        var fieldsKeys = Object.keys(fieldRefs.current);

        for (var i = 0; i < fieldsKeys.length; i += 1) {
          var fieldRef = fieldRefs.current[fieldsKeys[i]];
          var fieldErrors = fieldRef.verifing();

          if (fieldErrors && fieldErrors.length) {
            errors = errors.concat(fieldErrors);

            if (onlyFirstError) {
              break;
            }
          }
        }

        return errors;
      }
    };
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls, className)
  }, _react["default"].Children.map(children, function (child) {
    if (!child) return null;
    var _child$props = child.props,
        name = _child$props.name,
        value = _child$props.value;
    initFieldsValue(name, value);
    return /*#__PURE__*/_react["default"].createElement(_layout.SingleLayout, null, /*#__PURE__*/_react["default"].cloneElement(child, _objectSpread({
      ref: function ref(_ref2) {
        return onStoreRef(name, _ref2);
      },
      onChange: function onChange(value) {
        return onFieldChangeHandler(name, value);
      },
      getContainer: getContainer,
      direction: direction
    }, child.props)));
  }));
});
var FormFields = {
  Input: _input["default"],
  Department: _department["default"],
  User: _user["default"],
  Duties: _duties["default"],
  Textarea: _textarea["default"],
  Radios: _radios["default"],
  DropDown: _dropdown["default"],
  GroupFields: _group["default"],
  VerificationInput: _inputVerificationCode["default"],
  Password: _password["default"],
  Date: _date["default"],
  Cycle: _cycle["default"]
}; // @ts-ignore

Form.Fields = FormFields;
Form.defaultProps = {
  className: "",
  direction: "vertical"
};
var _default = Form;
exports["default"] = _default;