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

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 from 'react';
import PropTypes from 'prop-types';
import { Box } from 'grommet/es6/components/Box';
import { Heading } from 'grommet/es6/components/Heading';
import { Button } from 'grommet/es6/components/Button';
import { ResponsiveContext } from 'grommet/es6/contexts/ResponsiveContext';
import { Menu } from 'grommet-icons/es6/icons/Menu';

var Sidebar = function (_React$Component) {
  _inherits(Sidebar, _React$Component);

  function Sidebar() {
    var _temp, _this, _ret;

    _classCallCheck(this, Sidebar);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
      collapsed: undefined
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

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

    var _props = this.props,
        title = _props.title,
        width = _props.width,
        children = _props.children,
        collapsible = _props.collapsible,
        headingProps = _props.headingProps,
        rest = _objectWithoutProperties(_props, ['title', 'width', 'children', 'collapsible', 'headingProps']);

    var collapsed = this.state.collapsed;

    var BtnToggle = function BtnToggle(_ref) {
      var view = _ref.view;
      return collapsible ? React.createElement(Button, { icon: React.createElement(Menu, null), onClick: function onClick() {
          return _this2.setState({ collapsed: view !== 'collapsed' });
        } }) : null;
    };
    return React.createElement(
      ResponsiveContext.Consumer,
      null,
      function (size) {
        return collapsible && size === 'small' && collapsed === undefined || collapsed === true ? React.createElement(
          Box,
          { align: 'start' },
          React.createElement(BtnToggle, { view: 'collapsed' })
        ) : React.createElement(
          Box,
          _extends({
            width: width
          }, rest),
          React.createElement(
            Box,
            { pad: { bottom: 'small' }, gap: 'small', direction: 'row', align: 'center' },
            React.createElement(BtnToggle, { view: 'expanded' }),
            title && React.createElement(
              Box,
              _extends({
                flex: false,
                tag: 'header'
              }, headingProps),
              typeof title === 'string' ? React.createElement(
                Heading,
                { margin: 'none', level: 3 },
                title
              ) : title
            )
          ),
          children
        );
      }
    );
  };

  return Sidebar;
}(React.Component);

var SidebarDoc = void 0;
if (process.env.NODE_ENV !== 'production') {
  SidebarDoc = require('./doc').default(Sidebar); // eslint-disable-line global-require
}

var SidebaWrapper = SidebarDoc || Sidebar;

SidebaWrapper.defaultProps = {
  title: undefined,
  width: 'medium',
  collapsible: true
};

SidebaWrapper.propTypes = {
  title: PropTypes.node,
  width: PropTypes.string,
  collapsible: PropTypes.bool
};

export { SidebaWrapper as Sidebar };