"use strict";

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

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

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

var _icons = require("@react-vant/icons");

var _constant = require("../utils/constant");

var _useHeight = _interopRequireDefault(require("../hooks/use-height"));

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

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var NavBar = function NavBar(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var navBarRef = (0, _react.useRef)(null);
  var navBarHeight = (0, _useHeight.default)(navBarRef);

  var onClickLeft = function onClickLeft(event) {
    if (props.onClickLeft) props.onClickLeft(event);
  };

  var onClickRight = function onClickRight(event) {
    if (props.onClickRight) props.onClickRight(event);
  };

  var renderLeft = function renderLeft() {
    if (typeof props.leftText !== 'string' && /*#__PURE__*/(0, _react.isValidElement)(props.leftText)) {
      return props.leftText;
    }

    return [props.leftArrow && /*#__PURE__*/_react.default.cloneElement(props.leftArrow, {
      key: 'arroe',
      className: (0, _clsx2.default)(bem('arrow'))
    }), props.leftText && /*#__PURE__*/_react.default.createElement("span", {
      key: "text",
      className: (0, _clsx2.default)(bem('text'))
    }, props.leftText)];
  };

  var renderRight = function renderRight() {
    if (typeof props.rightText !== 'string' && /*#__PURE__*/(0, _react.isValidElement)(props.rightText)) {
      return props.rightText;
    }

    return /*#__PURE__*/_react.default.createElement("span", {
      className: (0, _clsx2.default)(bem('text'))
    }, props.rightText);
  };

  var renderNavBar = function renderNavBar() {
    var _clsx;

    var title = props.title,
        fixed = props.fixed,
        border = props.border,
        zIndex = props.zIndex;

    var style = _extends({
      zIndex: zIndex !== undefined ? +zIndex : undefined
    }, props.style);

    var hasLeft = props.leftArrow || props.leftText;
    var hasRight = props.rightText;
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: navBarRef,
      style: style,
      className: (0, _clsx2.default)(bem({
        fixed: fixed,
        'safe-area-inset-top': props.safeAreaInsetTop
      }), (_clsx = {}, _clsx[_constant.BORDER_BOTTOM] = border, _clsx), props.className)
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(bem('content'))
    }, hasLeft && /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(bem('left')),
      onClick: onClickLeft
    }, renderLeft()), /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(bem('title'), 'rv-ellipsis')
    }, title), hasRight && /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx2.default)(bem('right')),
      onClick: onClickRight
    }, renderRight())));
  };

  var renderPlaceholder = function renderPlaceholder() {
    if (props.fixed && props.placeholder) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)(bem('placeholder')),
        style: {
          height: navBarHeight ? navBarHeight + "px" : undefined
        }
      });
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderPlaceholder(), renderNavBar());
};

NavBar.defaultProps = {
  border: true,
  leftArrow: /*#__PURE__*/_react.default.createElement(_icons.ArrowLeft, null)
};
var _default = NavBar;
exports.default = _default;