"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _context = require("../config-provider/context");

var _context2 = _interopRequireDefault(require("./context"));

var _useResponsive3 = _interopRequireDefault(require("./useResponsive"));

var Row = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var children = props.children,
      customPrefixCls = props.prefixCls,
      className = props.className,
      justify = props.justify,
      align = props.align,
      gutter = props.gutter,
      style = props.style,
      reset = (0, _objectWithoutProperties2.default)(props, ["children", "prefixCls", "className", "justify", "align", "gutter", "style"]);

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var _useResponsive = (0, _useResponsive3.default)(),
      _useResponsive2 = (0, _slicedToArray2.default)(_useResponsive, 1),
      activeBreakpoint = _useResponsive2[0];

  var handleResult = function handleResult(item, index, result) {
    var dividend = 2;

    if ((0, _typeof2.default)(item) === 'object') {
      var val = activeBreakpoint ? item[activeBreakpoint] : 0;

      if (typeof val === 'undefined') {
        console.warn('不存在的媒体查询，请检查gutter配置是否正确');
      }

      result[index] = val / dividend;
    } else {
      result[index] = (item || 0) / dividend;
    }

    return result;
  };

  var computeGutter = function computeGutter() {
    var result = [0, 0];

    if (Array.isArray(gutter)) {
      gutter.forEach(function (item, index) {
        handleResult(item, index, result);
      });
    } else {
      handleResult(gutter || 0, 0, result);
    }

    return result;
  };

  var getRowStyles = function getRowStyles(gutters) {
    var styles = {};
    styles.marginLeft = -gutters[0];
    styles.marginRight = -gutters[0];

    if (gutters[1]) {
      styles.marginTop = -gutters[1];
      styles.marginBottom = -gutters[1];
    }

    return styles;
  };

  var renderDom = function renderDom() {
    var _classNames;

    var prefixCls = getPrefixClassName('row', customPrefixCls);
    var gutters = computeGutter();
    var cls = (0, _classnames.default)(prefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-").concat(justify), justify), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-").concat(align), align), _classNames));

    var childs = /*#__PURE__*/_react.default.createElement(_context2.default.Provider, {
      value: {
        gutters: gutters
      }
    }, children); // const childs = React.Children.map(children, (item) => {
    //   if (item) {
    //     return React.cloneElement(item as React.ReactElement, {
    //       gutters,
    //     });
    //   }
    // });


    return /*#__PURE__*/_react.default.createElement("div", Object.assign({
      style: gutter ? (0, _objectSpread2.default)((0, _objectSpread2.default)({}, getRowStyles(gutters)), style) : (0, _objectSpread2.default)({}, style)
    }, reset, {
      className: cls,
      ref: ref
    }), childs);
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderDom());
});

Row.defaultProps = {
  justify: 'start'
};
var _default = Row;
exports.default = _default;