var _excluded = ["children", "className", "style"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

import React, { useContext, useMemo } from 'react';
import cls from 'clsx';
import { addUnit } from '../utils';
import { BORDER } from '../utils/constant';
import Badge from '../badge';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var GridItem = function GridItem(_ref) {
  var _cls;

  var children = _ref.children,
      className = _ref.className,
      style = _ref.style,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('grid-item', prefixCls),
      bem = _createNamespace[0];

  var index = props.index,
      parent = props.parent;

  if (!parent) {
    if (process.env.NODE_ENV !== 'production') {
      // eslint-disable-next-line no-console
      console.error('[React Vant] <GridItem> must be a child component of <Grid>.');
    }
  }

  var rootStyle = useMemo(function () {
    var square = parent.square,
        gutter = parent.gutter,
        columnNum = parent.columnNum;
    var percent = 100 / +columnNum + "%";

    var internalStyle = _extends({}, style, {
      flexBasis: percent
    });

    if (square) {
      internalStyle.paddingTop = percent;
    } else if (gutter) {
      var gutterValue = addUnit(gutter);
      internalStyle.paddingRight = gutterValue;

      if (index >= columnNum) {
        internalStyle.marginTop = gutterValue;
      }
    }

    return internalStyle;
  }, [parent.style, parent.gutter, parent.columnNum]);
  var contentStyle = useMemo(function () {
    var square = parent.square,
        gutter = parent.gutter;

    if (square && gutter) {
      var gutterValue = addUnit(gutter);
      return _extends({}, props.contentStyle, {
        right: gutterValue,
        bottom: gutterValue,
        height: 'auto'
      });
    }

    return props.contentStyle;
  }, [parent.gutter, parent.columnNum, props.contentStyle]);

  var renderIcon = function renderIcon() {
    if (props.icon) {
      return /*#__PURE__*/React.createElement(Badge, _extends({}, props.badge), /*#__PURE__*/React.cloneElement(props.icon, {
        className: cls(bem('icon')),
        color: props.iconColor,
        fontSize: parent.iconSize
      }));
    }

    return null;
  };

  var renderText = function renderText() {
    if ( /*#__PURE__*/React.isValidElement(props.text)) {
      return props.text;
    }

    if (props.text) {
      return /*#__PURE__*/React.createElement("span", {
        className: cls(bem('text'))
      }, props.text);
    }

    return null;
  };

  var renderContent = function renderContent() {
    if (children) {
      return children;
    }

    return /*#__PURE__*/React.createElement(React.Fragment, null, renderIcon(), renderText());
  };

  var center = parent.center,
      border = parent.border,
      square = parent.square,
      gutter = parent.gutter,
      reverse = parent.reverse,
      direction = parent.direction;
  var classes = cls(props.contentClassName, bem('content', [direction, {
    center: center,
    square: square,
    reverse: reverse,
    clickable: !!props.onClick,
    surround: border && gutter
  }]), (_cls = {}, _cls[BORDER] = border, _cls));
  return /*#__PURE__*/React.createElement("div", {
    className: cls(className, bem({
      square: square
    })),
    style: rootStyle
  }, /*#__PURE__*/React.createElement("div", {
    role: props.onClick ? 'button' : undefined,
    className: classes,
    style: contentStyle,
    onClick: props.onClick
  }, renderContent()));
};

GridItem.defaultProps = {
  index: 0,
  parent: {}
};
export default GridItem;