import "core-js/modules/es6.string.fixed";
import "core-js/modules/es6.string.anchor";

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

import React from 'react';
import PropTypes from 'prop-types';

var Topbar =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(Topbar, _React$Component);

  function Topbar() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      isMobileOpen: false,
      activeIndex: null,
      focus: 0
    };

    _this.getChildContext = function () {
      return {
        activeIndex: _this.state.activeIndex,
        onActivate: function onActivate(index) {
          return _this.setSelected(index);
        },
        onFocus: function onFocus(index) {
          return _this.setState(function () {
            return {
              focus: index
            };
          });
        },
        focus: _this.state.focus
      };
    };

    _this.setSelected = function (index) {
      if (index === _this.state.activeIndex) return;

      _this.setState(function () {
        return {
          activeIndex: index
        };
      });
    };

    return _this;
  }

  var _proto = Topbar.prototype;

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

    var _this$props = this.props,
        anchor = _this$props.anchor,
        brandAnchorElement = _this$props.brandAnchorElement,
        color = _this$props.color,
        fixed = _this$props.fixed,
        icon = _this$props.icon,
        image = _this$props.image,
        title = _this$props.title;
    var cuiTopBarClass = 'cui-top-bar';
    var cuiBrandClass = 'cui-brand';
    var brandNodeChildren = [React.createElement("div", {
      className: cuiBrandClass + "__logo",
      key: cuiBrandClass + "__logo"
    }, image ? image : React.createElement("i", {
      className: "icon " + icon
    })), React.createElement("div", {
      className: cuiBrandClass + "__title",
      key: cuiBrandClass + "__title"
    }, title)];

    var getBrandAnchor = function getBrandAnchor() {
      return brandAnchorElement ? React.cloneElement(brandAnchorElement, {
        className: "" + cuiBrandClass + ("" + (brandAnchorElement.props.className && " " + brandAnchorElement.props.className || ''))
      }, brandNodeChildren) : React.createElement("a", {
        className: cuiBrandClass,
        href: anchor
      }, brandNodeChildren);
    };

    var brandNode = React.createElement("div", {
      className: cuiTopBarClass + "__brand"
    }, getBrandAnchor());
    var injectChildren = React.Children.map(this.props.children, function (child) {
      if (!child) return;

      if (child.type.displayName === 'TopbarMobile') {
        return React.cloneElement(child, {
          brandNode: brandNode
        });
      } else {
        return child;
      }
    });
    return React.createElement("div", {
      className: "" + cuiTopBarClass + ("" + (fixed && " " + cuiTopBarClass + "--fixed" || '')) + (" " + cuiTopBarClass + "--" + color),
      role: "navigation",
      ref: function ref(_ref) {
        _this2.parentContainer = _ref;
      }
    }, React.createElement("div", {
      className: cuiTopBarClass + "__container"
    }, brandNode, injectChildren));
  };

  return Topbar;
}(React.Component);

Topbar.displayName = 'Topbar';
Topbar.propTypes = {
  /** @prop App Url/Link | null */
  anchor: PropTypes.string,

  /** @prop Custom Node to wrap | null */
  brandAnchorElement: PropTypes.element,

  /** @prop Children nodes to render inside of Topbar | null */
  children: PropTypes.node,

  /** @prop Optional CSS class string | '' */
  className: PropTypes.string,

  /** @prop Topbar header color | '' */
  color: PropTypes.string,

  /** @prop Determines if Topbar is fixed to top | false */
  fixed: PropTypes.bool,

  /** @prop Icon class name | 'icon-cisco-logo' */
  icon: PropTypes.string,

  /** @prop Image source URL | null */
  image: PropTypes.node,

  /** @prop Topbar title text | '' */
  title: PropTypes.string
};
Topbar.defaultProps = {
  anchor: null,
  brandAnchorElement: null,
  children: null,
  className: '',
  color: 'dark',
  fixed: false,
  icon: 'icon-cisco-logo',
  image: null,
  title: ''
};
Topbar.childContextTypes = {
  focus: PropTypes.number,
  activeIndex: PropTypes.number,
  onActivate: PropTypes.func,
  onFocus: PropTypes.func
};
Topbar.displayName = 'Topbar';
export default Topbar;