"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

var _button = require("../button");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var SubmitBar = function SubmitBar(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      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.default.createElement("div", {
        className: (0, _clsx.default)(bem('text')),
        style: {
          textAlign: textAlign
        }
      }, /*#__PURE__*/_react.default.createElement("span", null, label || locale.vanSubmitBar.label), /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('price'))
      }, currency, /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('price-integer'))
      }, pricePair[0]), decimal), suffixLabel && /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(bem('suffix-label'))
      }, suffixLabel));
    }

    return null;
  };

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

    if (tip) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('tip'))
      }, tipIcon && /*#__PURE__*/_react.default.cloneElement(tipIcon, {
        className: (0, _clsx.default)(bem('tip-icon'))
      }), tip && /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _clsx.default)(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.default.createElement(_button.Button, {
      round: true,
      type: props.buttonType,
      text: props.buttonText,
      className: (0, _clsx.default)(bem('button', props.buttonType)),
      color: props.buttonColor,
      loading: props.loading,
      disabled: props.disabled,
      onClick: onClickButton
    });
  };

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


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