'use strict';

exports.__esModule = true;

var _remove2 = require('lodash/remove');

var _remove3 = _interopRequireDefault(_remove2);

var _clone2 = require('lodash/clone');

var _clone3 = _interopRequireDefault(_clone2);

var _isEqual2 = require('lodash/isEqual');

var _isEqual3 = _interopRequireDefault(_isEqual2);

var _isUndefined2 = require('lodash/isUndefined');

var _isUndefined3 = _interopRequireDefault(_isUndefined2);

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 _react = require('react');

var React = _interopRequireWildcard(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _setStatic = require('recompose/setStatic');

var _setStatic2 = _interopRequireDefault(_setStatic);

var _Transition = require('rsuite-utils/lib/Animation/Transition');

var _Transition2 = _interopRequireDefault(_Transition);

var _SidenavBody = require('./SidenavBody');

var _SidenavBody2 = _interopRequireDefault(_SidenavBody);

var _SidenavHeader = require('./SidenavHeader');

var _SidenavHeader2 = _interopRequireDefault(_SidenavHeader);

var _SidenavToggle = require('./SidenavToggle');

var _SidenavToggle2 = _interopRequireDefault(_SidenavToggle);

var _utils = require('./utils');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

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) : _defaults(subClass, superClass); }

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

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleSelect = function (eventKey, event) {
      var onSelect = _this.props.onSelect;


      _this.setState({ activeKey: eventKey });
      onSelect && onSelect(eventKey, event);
    };

    _this.handleOpenChange = function (eventKey, event) {
      var onOpenChange = _this.props.onOpenChange;

      var find = function find(key) {
        return (0, _isEqual3.default)(key, eventKey);
      };
      var openKeys = (0, _clone3.default)(_this.state.openKeys) || [];

      if (openKeys.some(find)) {
        (0, _remove3.default)(openKeys, find);
      } else {
        openKeys.push(eventKey);
      }

      _this.setState({ openKeys: openKeys });

      onOpenChange && onOpenChange(openKeys, event);
    };

    _this.state = {
      activeKey: props.defaultActiveKey,
      openKeys: props.defaultOpenKeys || []
    };
    return _this;
  }

  Sidenav.prototype.getChildContext = function getChildContext() {
    var _props = this.props,
        expanded = _props.expanded,
        openKeys = _props.openKeys,
        activeKey = _props.activeKey;


    return {
      expanded: expanded,
      sidenav: true,
      activeKey: (0, _isUndefined3.default)(activeKey) ? this.state.activeKey : activeKey,
      openKeys: (0, _isUndefined3.default)(openKeys) ? this.state.openKeys : openKeys,
      onOpenChange: this.handleOpenChange,
      onSelect: this.handleSelect
    };
  };

  Sidenav.prototype.render = function render() {
    var _props2 = this.props,
        className = _props2.className,
        classPrefix = _props2.classPrefix,
        appearance = _props2.appearance,
        expanded = _props2.expanded,
        openKeys = _props2.openKeys,
        defaultOpenKeys = _props2.defaultOpenKeys,
        Component = _props2.componentClass,
        props = _objectWithoutProperties(_props2, ['className', 'classPrefix', 'appearance', 'expanded', 'openKeys', 'defaultOpenKeys', 'componentClass']);

    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(classPrefix, addPrefix(appearance), className);
    var unhandled = (0, _utils.getUnhandledProps)(Sidenav, props);

    return React.createElement(
      _Transition2.default,
      {
        'in': expanded,
        timeout: 300,
        exitedClassName: addPrefix('collapse-out'),
        exitingClassName: addPrefix(['collapse-out', 'collapsing']),
        enteredClassName: addPrefix('collapse-in'),
        enteringClassName: addPrefix(['collapse-in', 'collapsing'])
      },
      React.createElement(Component, _extends({}, unhandled, { className: classes, role: 'navigation' }))
    );
  };

  return Sidenav;
}(React.Component);

Sidenav.defaultProps = {
  appearance: 'default',
  expanded: true
};
Sidenav.childContextTypes = {
  activeKey: _propTypes2.default.any,
  openKeys: _propTypes2.default.array,
  expanded: _propTypes2.default.bool,
  sidenav: _propTypes2.default.bool,
  onSelect: _propTypes2.default.func,
  onOpenChange: _propTypes2.default.func
};
Sidenav.handledProps = ['activeKey', 'appearance', 'className', 'classPrefix', 'componentClass', 'defaultActiveKey', 'defaultOpenKeys', 'expanded', 'onOpenChange', 'onSelect', 'openKeys'];
Sidenav.propTypes = {
  classPrefix: _propTypes2.default.string,
  className: _propTypes2.default.string,
  expanded: _propTypes2.default.bool.isRequired,
  appearance: _propTypes2.default.oneOf(['default', 'inverse', 'subtle']).isRequired,
  defaultOpenKeys: _propTypes2.default.arrayOf(_propTypes2.default.any),
  openKeys: _propTypes2.default.arrayOf(_propTypes2.default.any),
  onOpenChange: _propTypes2.default.func,
  activeKey: _propTypes2.default.any,
  defaultActiveKey: _propTypes2.default.any,
  onSelect: _propTypes2.default.func,
  componentClass: function componentClass() {
    return (typeof (React.ElementType == null ? {} : React.ElementType) === 'function' ? _propTypes2.default.instanceOf(React.ElementType == null ? {} : React.ElementType).isRequired : _propTypes2.default.any.isRequired).apply(this, arguments);
  }
};


var EnhancedSidenav = (0, _utils.defaultProps)({
  classPrefix: 'sidenav',
  componentClass: 'div'
})(Sidenav);

(0, _setStatic2.default)('Header', _SidenavHeader2.default)(EnhancedSidenav);
(0, _setStatic2.default)('Body', _SidenavBody2.default)(EnhancedSidenav);
(0, _setStatic2.default)('Toggle', _SidenavToggle2.default)(EnhancedSidenav);

exports.default = EnhancedSidenav;