import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useContext } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import Button from '../button';

const SubmitBar = props => {
  const {
    prefixCls,
    createNamespace,
    locale
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('submit-bar', prefixCls);

  const renderText = () => {
    const {
      price,
      label,
      currency,
      textAlign,
      suffixLabel,
      decimalLength
    } = props;

    if (typeof +price === 'number') {
      const pricePair = (+price / 100).toFixed(+decimalLength).split('.');
      const decimal = decimalLength ? `.${pricePair[1]}` : '';
      return _jsxs("div", Object.assign({
        className: cls(bem('text')),
        style: {
          textAlign
        }
      }, {
        children: [_jsx("span", {
          children: label || locale.vanSubmitBar.label
        }), _jsxs("span", Object.assign({
          className: cls(bem('price'))
        }, {
          children: [currency, _jsx("span", Object.assign({
            className: cls(bem('price-integer'))
          }, {
            children: pricePair[0]
          })), decimal]
        })), suffixLabel && _jsx("span", Object.assign({
          className: cls(bem('suffix-label'))
        }, {
          children: suffixLabel
        }))]
      }));
    }

    return null;
  };

  const renderTip = () => {
    const {
      tip,
      tipIcon
    } = props;

    if (tip) {
      return _jsxs("div", Object.assign({
        className: cls(bem('tip'))
      }, {
        children: [tipIcon && React.cloneElement(tipIcon, {
          className: cls(bem('tip-icon'))
        }), tip && _jsx("span", Object.assign({
          className: cls(bem('tip-text'))
        }, {
          children: tip
        }))]
      }));
    }

    return null;
  };

  const onClickButton = () => props === null || props === void 0 ? void 0 : props.onSubmit();

  const renderButton = () => {
    if (props.button) {
      return props.button;
    }

    return _jsx(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 _jsxs("div", Object.assign({
    className: cls(props.className, bem(), {
      'rv-safe-area-bottom': props.safeAreaInsetBottom
    }),
    style: props.style
  }, {
    children: [props.top, renderTip(), _jsxs("div", Object.assign({
      className: cls(bem('bar'))
    }, {
      children: [props.children, renderText(), renderButton()]
    }))]
  }));
}; // defaultProps defined if need


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