import React, { useContext } from 'react';
import clsx from 'clsx';
import { Arrow, ArrowDown, ArrowLeft, ArrowUp } from '@react-vant/icons';
import { isDef } from '../utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var Cell = function Cell(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('cell', prefixCls),
      bem = _createNamespace[0];

  var renderLabel = function renderLabel() {
    var showLabel = isDef(props.label);

    if (showLabel) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('label'), props.labelClass)
      }, props.label);
    }

    return null;
  };

  var renderTitle = function renderTitle() {
    if (isDef(props.title)) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('title'), props.titleClass),
        style: props.titleStyle
      }, props.title, renderLabel());
    }

    return null;
  };

  var renderValue = function renderValue() {
    var hasTitle = isDef(props.title);
    var hasValue = props.children || isDef(props.value);

    if (hasValue) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('value', {
          alone: !hasTitle
        }), props.valueClass)
      }, props.children ? props.children : /*#__PURE__*/React.createElement("span", null, props.value));
    }

    return null;
  };

  var renderLeftIcon = function renderLeftIcon() {
    if (props.icon) {
      return /*#__PURE__*/React.cloneElement(props.icon, {
        className: clsx(bem('left-icon'))
      });
    }

    return null;
  };

  var renderRightIcon = function renderRightIcon() {
    if (props.rightIcon) {
      return props.rightIcon;
    }

    if (props.isLink) {
      var _className = clsx(bem('right-icon'));

      if (props.arrowDirection === 'left') return /*#__PURE__*/React.createElement(ArrowLeft, {
        className: _className
      });
      if (props.arrowDirection === 'up') return /*#__PURE__*/React.createElement(ArrowUp, {
        className: _className
      });
      if (props.arrowDirection === 'down') return /*#__PURE__*/React.createElement(ArrowDown, {
        className: _className
      });
      return /*#__PURE__*/React.createElement(Arrow, {
        className: _className
      });
    }

    return null;
  };

  var className = props.className,
      style = props.style,
      size = props.size,
      center = props.center,
      _props$border = props.border,
      border = _props$border === void 0 ? true : _props$border,
      isLink = props.isLink,
      required = props.required,
      onClick = props.onClick;
  var clickable = isLink || props.clickable;
  var classes = {
    center: center,
    required: required,
    clickable: clickable,
    borderless: !border
  };

  if (size) {
    classes[size] = !!size;
  }

  return /*#__PURE__*/React.createElement("div", {
    style: style,
    className: clsx(bem(classes), className),
    role: clickable ? 'button' : undefined,
    onClick: onClick
  }, renderLeftIcon(), renderTitle(), renderValue(), renderRightIcon(), props.extra);
};

export default Cell;