"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Device = _interopRequireDefault(require("./../Device"));

var _Bridge = _interopRequireDefault(require("./../Bridge"));

var _excluded = ["className", "showUrlTitle", "caption", "captionAttribute", "lButtons", "rButtons", "backButtonAttribute", "children"];

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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Titlebar = /*#__PURE__*/function (_Component) {
  _inheritsLoose(Titlebar, _Component);

  function Titlebar(props) {
    var _this;

    _this = _Component.call(this, props) || this;

    _defineProperty(_assertThisInitialized(_this), "onClickBack", function (e) {
      var _this$props$backButto = _this.props.backButtonAttribute,
          backButtonAttribute = _this$props$backButto === void 0 ? {} : _this$props$backButto; // 如果有返回按钮的点击事件,则优先执行props的方法

      if (backButtonAttribute.onClick) {
        backButtonAttribute.onClick(e);
        return;
      } // 否则走默认的返回


      _Bridge["default"].back();
    });

    _defineProperty(_assertThisInitialized(_this), "getButtonsDOM", function (arr) {
      var _this$props$backButto2 = _this.props.backButtonAttribute,
          backButtonAttribute = _this$props$backButto2 === void 0 ? {} : _this$props$backButto2;
      return arr.map(function (item, index) {
        if (item === '$back') {
          item = {
            className: backButtonAttribute.className || null,
            style: backButtonAttribute.style || null,
            icon: backButtonAttribute.icon || null,
            iconAttribute: backButtonAttribute.iconAttribute !== undefined ? backButtonAttribute.iconAttribute : {
              className: 'shape-arrow-left'
            },
            caption: backButtonAttribute.caption || null,
            onClick: _this.onClickBack
          };
        }

        return /*#__PURE__*/_react["default"].createElement("a", {
          key: index,
          disabled: item.disabled,
          onClick: function onClick(e) {
            if (item.onClick) item.onClick(e, item, index);
          },
          className: "titlebar-button button" + (item.className ? ' ' + item.className : ' bar'),
          style: item.style
        }, item.iconAttribute && /*#__PURE__*/_react["default"].createElement("span", _extends({}, item.iconAttribute, {
          className: "icon" + (item.iconAttribute.className ? ' ' + item.iconAttribute.className : '')
        })), item.icon && item.icon, item.caption && /*#__PURE__*/_react["default"].createElement("span", null, item.caption));
      });
    });

    _this.state = {};
    return _this;
  } // 默认的返回按键


  var _proto = Titlebar.prototype;

  _proto.render = function render() {
    var _this2 = this;

    var _this$props = this.props,
        className = _this$props.className,
        showUrlTitle = _this$props.showUrlTitle,
        caption = _this$props.caption,
        _this$props$captionAt = _this$props.captionAttribute,
        captionAttribute = _this$props$captionAt === void 0 ? {} : _this$props$captionAt,
        lButtons = _this$props.lButtons,
        rButtons = _this$props.rButtons,
        _this$props$backButto3 = _this$props.backButtonAttribute,
        backButtonAttribute = _this$props$backButto3 === void 0 ? {} : _this$props$backButto3,
        children = _this$props.children,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded); // 构建左右按钮


    var lButtonsDOM = null;

    if (Array.isArray(lButtons)) {
      lButtonsDOM = this.getButtonsDOM(lButtons);
    }

    var rButtonsDOM = null;

    if (Array.isArray(rButtons)) {
      rButtonsDOM = this.getButtonsDOM(rButtons);
    } // 设置显示标题


    var title = _Device["default"].getUrlParameter('titlebar', location.search);

    if (showUrlTitle && title) {
      caption = /*#__PURE__*/_react["default"].createElement("h1", _extends({}, captionAttribute, {
        className: "titlebar-caption nowrap text-center" + (captionAttribute.className ? ' ' + captionAttribute.className : '')
      }), decodeURIComponent(decodeURIComponent(title)));
    } else if (typeof caption === 'string') {
      caption = /*#__PURE__*/_react["default"].createElement("h1", _extends({}, captionAttribute, {
        className: "titlebar-caption nowrap text-center" + (captionAttribute.className ? ' ' + captionAttribute.className : '')
      }), caption);
    }

    return /*#__PURE__*/_react["default"].createElement("div", _extends({
      ref: function ref(el) {
        _this2.$el = el;
      }
    }, others, {
      className: "titlebar" + (className ? ' ' + className : '')
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "titlebar-left"
    }, lButtonsDOM), caption, children, /*#__PURE__*/_react["default"].createElement("div", {
      className: "titlebar-right"
    }, rButtonsDOM));
  };

  return Titlebar;
}(_react.Component);

exports["default"] = Titlebar;

_defineProperty(Titlebar, "defaultProps", {
  showUrlTitle: true,
  lButtons: ['$back'],
  className: 'border-b'
});

Titlebar.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes["default"].string,
  showUrlTitle: _propTypes["default"].bool,
  // 标题是否显示url中的titlebar
  caption: _propTypes["default"].node,
  captionAttribute: _propTypes["default"].object,
  // 只有caption为string类型或者显示地址栏标题时才有用
  lButtons: _propTypes["default"].array,
  // [{caption: string, className: string, style: object, icon: node, iconAttribute: {className: ''}}]
  rButtons: _propTypes["default"].array,
  backButtonAttribute: _propTypes["default"].object,
  // {caption: string, className: string, style: object, icon: node, iconAttribute: {className: ''}}
  children: _propTypes["default"].node
} : {};
module.exports = exports.default;