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

var _class, _temp;

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Device from './../Device';
import Bridge from './../Bridge';

var Titlebar = (_temp = _class = function (_Component) {
  _inherits(Titlebar, _Component);

  function Titlebar(props) {
    _classCallCheck(this, Titlebar);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

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

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

    _this.getButtonsDOM = function (arr) {
      var _this$props$backButto2 = _this.props.backButtonAttribute,
          backButtonAttribute = _this$props$backButto2 === undefined ? {} : _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 React.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 && React.createElement('span', _extends({}, item.iconAttribute, { className: 'icon' + (item.iconAttribute.className ? ' ' + item.iconAttribute.className : '') })),
          item.icon && item.icon,
          item.caption && React.createElement(
            'span',
            null,
            item.caption
          )
        );
      });
    };

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

  // 解析返回按钮dom


  Titlebar.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        className = _props.className,
        showUrlTitle = _props.showUrlTitle,
        caption = _props.caption,
        _props$captionAttribu = _props.captionAttribute,
        captionAttribute = _props$captionAttribu === undefined ? {} : _props$captionAttribu,
        lButtons = _props.lButtons,
        rButtons = _props.rButtons,
        _props$backButtonAttr = _props.backButtonAttribute,
        backButtonAttribute = _props$backButtonAttr === undefined ? {} : _props$backButtonAttr,
        children = _props.children,
        others = _objectWithoutProperties(_props, ['className', 'showUrlTitle', 'caption', 'captionAttribute', 'lButtons', 'rButtons', 'backButtonAttribute', 'children']);
    // 构建左右按钮


    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.getUrlParameter('titlebar', location.search);
    if (showUrlTitle && title) {
      caption = React.createElement(
        'h1',
        _extends({}, captionAttribute, { className: 'titlebar-caption nowrap text-center' + (captionAttribute.className ? ' ' + captionAttribute.className : '') }),
        decodeURIComponent(decodeURIComponent(title))
      );
    } else if (typeof caption === 'string') {
      caption = React.createElement(
        'h1',
        _extends({}, captionAttribute, { className: 'titlebar-caption nowrap text-center' + (captionAttribute.className ? ' ' + captionAttribute.className : '') }),
        caption
      );
    }
    return React.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, others, { className: 'titlebar' + (className ? ' ' + className : '') }),
      React.createElement(
        'div',
        { className: 'titlebar-left' },
        lButtonsDOM
      ),
      caption,
      children,
      React.createElement(
        'div',
        { className: 'titlebar-right' },
        rButtonsDOM
      )
    );
  };

  return Titlebar;
}(Component), _class.defaultProps = {
  showUrlTitle: true,
  lButtons: ['$back'],
  className: 'border-b'
}, _temp);
export { Titlebar as default };
Titlebar.propTypes = process.env.NODE_ENV !== "production" ? {
  className: PropTypes.string,

  showUrlTitle: PropTypes.bool, // 标题是否显示url中的titlebar
  caption: PropTypes.node,
  captionAttribute: PropTypes.object, // 只有caption为string类型或者显示地址栏标题时才有用

  lButtons: PropTypes.array, // [{caption: string, className: string, style: object, icon: node, iconAttribute: {className: ''}}]
  rButtons: PropTypes.array,

  backButtonAttribute: PropTypes.object, // {caption: string, className: string, style: object, icon: node, iconAttribute: {className: ''}}

  children: PropTypes.node
} : {};