import _defineProperty from "@babel/runtime/helpers/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import * as React from 'react';
import { createBEM } from '@zarm-design/bem';
import { ConfigContext } from '../config-provider';
import GridContext from './GridContext';

var Grid = function Grid(props) {
  var className = props.className,
      style = props.style,
      columns = props.columns,
      gutter = props.gutter,
      bordered = props.bordered,
      square = props.square,
      children = props.children;

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

  var bem = createBEM('grid', {
    prefixCls: prefixCls
  });
  var gutters = Array.isArray(gutter) ? gutter : [gutter, gutter];
  var gridContext = React.useMemo(function () {
    return {
      columns: columns,
      gutter: gutters,
      bordered: bordered,
      square: square
    };
  }, [columns, gutters, bordered, square]);
  var gridStyle = {};
  var horizontalGutter = gutters[0] > 0 ? gutters[0] / 2 : undefined;
  var verticalGutter = gutters[1] > 0 ? gutters[1] / 2 : undefined;

  if (!square && horizontalGutter) {
    gridStyle.paddingTop = horizontalGutter;
    gridStyle.paddingBottom = horizontalGutter;
  }

  if (!square && verticalGutter) {
    gridStyle.paddingLeft = verticalGutter;
    gridStyle.paddingRight = verticalGutter;
  }

  var classes = bem([{
    bordered: bordered,
    square: square
  }, className]);
  return /*#__PURE__*/React.createElement(GridContext.Provider, {
    value: gridContext
  }, /*#__PURE__*/React.createElement("div", {
    className: classes,
    style: _objectSpread(_objectSpread({}, gridStyle), style)
  }, children));
};

Grid.displayName = 'Grid';
Grid.defaultProps = {
  columns: 3,
  gutter: 0,
  bordered: true
};
export default Grid;