import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
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';

const GridItem = _a => {
  var {
    children,
    className,
    style
  } = _a,
      props = __rest(_a, ["children", "className", "style"]);

  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('grid-item', prefixCls);
  const {
    index,
    parent
  } = props;

  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>.');
    }
  }

  const rootStyle = useMemo(() => {
    const {
      square,
      gutter,
      columnNum
    } = parent;
    const percent = `${100 / +columnNum}%`;
    const internalStyle = Object.assign(Object.assign({}, style), {
      flexBasis: percent
    });

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

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

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

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

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

  const renderIcon = () => {
    if (props.icon) {
      return _jsx(Badge, Object.assign({}, props.badge, {
        children: React.cloneElement(props.icon, {
          className: cls(bem('icon')),
          color: props.iconColor,
          fontSize: parent.iconSize
        })
      }));
    }

    return null;
  };

  const renderText = () => {
    if (React.isValidElement(props.text)) {
      return props.text;
    }

    if (props.text) {
      return _jsx("span", Object.assign({
        className: cls(bem('text'))
      }, {
        children: props.text
      }));
    }

    return null;
  };

  const renderContent = () => {
    if (children) {
      return children;
    }

    return _jsxs(_Fragment, {
      children: [renderIcon(), renderText()]
    });
  };

  const {
    center,
    border,
    square,
    gutter,
    reverse,
    direction
  } = parent;
  const classes = cls(props.contentClassName, bem('content', [direction, {
    center,
    square,
    reverse,
    clickable: !!props.onClick,
    surround: border && gutter
  }]), {
    [BORDER]: border
  });
  return _jsx("div", Object.assign({
    className: cls(className, bem({
      square
    })),
    style: rootStyle
  }, {
    children: _jsx("div", Object.assign({
      role: props.onClick ? 'button' : undefined,
      className: classes,
      style: contentStyle,
      onClick: props.onClick
    }, {
      children: renderContent()
    }))
  }));
};

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