import React, { useContext } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import { Button } from '../button';

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

  var _createNamespace = createNamespace('submit-bar', prefixCls),
      bem = _createNamespace[0];

  var renderText = function renderText() {
    var price = props.price,
        label = props.label,
        currency = props.currency,
        textAlign = props.textAlign,
        suffixLabel = props.suffixLabel,
        decimalLength = props.decimalLength;

    if (typeof +price === 'number') {
      var pricePair = (+price / 100).toFixed(+decimalLength).split('.');
      var decimal = decimalLength ? "." + pricePair[1] : '';
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('text')),
        style: {
          textAlign: textAlign
        }
      }, /*#__PURE__*/React.createElement("span", null, label || locale.vanSubmitBar.label), /*#__PURE__*/React.createElement("span", {
        className: cls(bem('price'))
      }, currency, /*#__PURE__*/React.createElement("span", {
        className: cls(bem('price-integer'))
      }, pricePair[0]), decimal), suffixLabel && /*#__PURE__*/React.createElement("span", {
        className: cls(bem('suffix-label'))
      }, suffixLabel));
    }

    return null;
  };

  var renderTip = function renderTip() {
    var tip = props.tip,
        tipIcon = props.tipIcon;

    if (tip) {
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('tip'))
      }, tipIcon && /*#__PURE__*/React.cloneElement(tipIcon, {
        className: cls(bem('tip-icon'))
      }), tip && /*#__PURE__*/React.createElement("span", {
        className: cls(bem('tip-text'))
      }, tip));
    }

    return null;
  };

  var onClickButton = function onClickButton() {
    return props == null ? void 0 : props.onSubmit();
  };

  var renderButton = function renderButton() {
    if (props.button) {
      return props.button;
    }

    return /*#__PURE__*/React.createElement(Button, {
      round: true,
      type: props.buttonType,
      text: props.buttonText,
      className: cls(bem('button', props.buttonType)),
      color: props.buttonColor,
      loading: props.loading,
      disabled: props.disabled,
      onClick: onClickButton
    });
  };

  return /*#__PURE__*/React.createElement("div", {
    className: cls(props.className, bem(), {
      'van-safe-area-bottom': props.safeAreaInsetBottom
    }),
    style: props.style
  }, props.top, renderTip(), /*#__PURE__*/React.createElement("div", {
    className: cls(bem('bar'))
  }, props.children, renderText(), renderButton()));
}; // defaultProps defined if need


SubmitBar.defaultProps = {
  buttonType: 'danger',
  decimalLength: 2,
  currency: '¥',
  safeAreaInsetBottom: true
};
export default SubmitBar;