"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.useFormShallowLayout = exports.useFormLayout = exports.useFormDeepLayout = exports.default = exports.FormLayoutShallowContext = exports.FormLayoutDeepContext = exports.FormLayout = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useResponsiveFormLayout = require("./useResponsiveFormLayout");
var _builtins__ = require("../__builtins__");
var _classnames = _interopRequireDefault(require("classnames"));
var _excluded = ["shallow", "children", "prefix", "className", "style"];
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 _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; }
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 FormLayoutDeepContext = /*#__PURE__*/(0, _react.createContext)({});
exports.FormLayoutDeepContext = FormLayoutDeepContext;
var FormLayoutShallowContext = /*#__PURE__*/(0, _react.createContext)({});
exports.FormLayoutShallowContext = FormLayoutShallowContext;
var useFormDeepLayout = function useFormDeepLayout() {
  return (0, _react.useContext)(FormLayoutDeepContext);
};
exports.useFormDeepLayout = useFormDeepLayout;
var useFormShallowLayout = function useFormShallowLayout() {
  return (0, _react.useContext)(FormLayoutShallowContext);
};
exports.useFormShallowLayout = useFormShallowLayout;
var useFormLayout = function useFormLayout() {
  return _objectSpread(_objectSpread({}, useFormDeepLayout()), useFormShallowLayout());
};
exports.useFormLayout = useFormLayout;
var FormLayout = function FormLayout(_ref) {
  var _cls;
  var shallow = _ref.shallow,
    children = _ref.children,
    prefix = _ref.prefix,
    className = _ref.className,
    style = _ref.style,
    otherProps = _objectWithoutProperties(_ref, _excluded);
  var _useResponsiveFormLay = (0, _useResponsiveFormLayout.useResponsiveFormLayout)(otherProps),
    ref = _useResponsiveFormLay.ref,
    props = _useResponsiveFormLay.props;
  var deepLayout = useFormDeepLayout();
  var formPrefixCls = (0, _builtins__.usePrefixCls)('form', {
    prefix: prefix
  });
  var layoutPrefixCls = (0, _builtins__.usePrefixCls)('formily-layout', {
    prefix: prefix
  });
  var layoutClassName = (0, _classnames.default)(layoutPrefixCls, (_cls = {}, _defineProperty(_cls, "".concat(formPrefixCls, "-").concat(props.layout), true), _defineProperty(_cls, "".concat(formPrefixCls, "-rtl"), props.direction === 'rtl'), _defineProperty(_cls, "".concat(formPrefixCls, "-").concat(props.size), props.size), _cls), className);
  var renderChildren = function renderChildren() {
    var newDeepLayout = _objectSpread({}, deepLayout);
    if (!shallow) {
      Object.assign(newDeepLayout, props);
    } else {
      if (props.size) {
        newDeepLayout.size = props.size;
      }
      if (props.colon) {
        newDeepLayout.colon = props.colon;
      }
    }
    return /*#__PURE__*/_react.default.createElement(FormLayoutDeepContext.Provider, {
      value: newDeepLayout
    }, /*#__PURE__*/_react.default.createElement(FormLayoutShallowContext.Provider, {
      value: shallow ? props : undefined
    }, children));
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref,
    className: layoutClassName,
    style: style
  }, renderChildren());
};
exports.FormLayout = FormLayout;
FormLayout.defaultProps = {
  shallow: true
};
FormLayout.useFormDeepLayout = useFormDeepLayout;
FormLayout.useFormShallowLayout = useFormShallowLayout;
FormLayout.useFormLayout = useFormLayout;
var _default = FormLayout;
exports.default = _default;