import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/context';
import GridContext from './context';
import useResponsive from './useResponsive';
var Row = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["children", "prefixCls", "className", "justify", "align", "gutter", "style"]);

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var _useResponsive = useResponsive(),
      _useResponsive2 = _slicedToArray(_useResponsive, 1),
      activeBreakpoint = _useResponsive2[0];

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

    if (_typeof(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 = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(justify), justify), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(align), align), _classNames));
    var childs = /*#__PURE__*/React.createElement(GridContext.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.createElement("div", Object.assign({
      style: gutter ? _objectSpread(_objectSpread({}, getRowStyles(gutters)), style) : _objectSpread({}, style)
    }, reset, {
      className: cls,
      ref: ref
    }), childs);
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, renderDom());
});
Row.defaultProps = {
  justify: 'start'
};
export default Row;