'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _Content = require('./Content');

var _Content2 = _interopRequireDefault(_Content);

var _Trigger = require('./Trigger');

var _Trigger2 = _interopRequireDefault(_Trigger);

var _Item = require('./Item');

var _Item2 = _interopRequireDefault(_Item);

var _Separation = require('./Separation');

var _Separation2 = _interopRequireDefault(_Separation);

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

var _styleModuleImportMap = {
  './dropdown.scss': {
    'dropdown': 'flive_dropdown_dropdown',
    'nav-bar': 'flive_dropdown_nav-bar',
    'nav-bar-level-2': 'flive_dropdown_nav-bar-level-2',
    'sm': 'flive_dropdown_sm',
    'lg': 'flive_dropdown_lg',
    'md': 'flive_dropdown_md'
  }
};

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

var propTypes = {
  /** @ignore */
  className: _propTypes2.default.string,
  /** @ignore */
  children: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.node]),
  variant: _propTypes2.default.string,
  reverse: _propTypes2.default.bool,
  active: _propTypes2.default.bool,
  /** x-small, small, medium, large */
  size: _propTypes2.default.oneOf(['xs', 'sm', 'md', 'lg', 'nav-bar', 'nav-bar-level-2'])
};

var defaultProps = {
  className: undefined,
  children: null,
  variant: 'primary',
  reverse: false,
  active: false,
  itemClassName: '',
  triggerClassName: '',
  contentClassName: '',
  size: 'md'
};

var Dropdown = function (_Component) {
  _inherits(Dropdown, _Component);

  function Dropdown() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Dropdown);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Dropdown.__proto__ || Object.getPrototypeOf(Dropdown)).call.apply(_ref, [this].concat(args))), _this), _this.state = { active: false }, _this.setdropDownRef = function (node) {
      _this.dropDownRef = node;
    }, _this.handleClickOutside = function (event) {
      if (event) {
        if (_this.dropDownRef && !_this.dropDownRef.contains(event.target)) {
          _this.setState({ isActive: false });
        }
      }
    }, _this.openPanel = function () {
      _this.setState({ isActive: true });
    }, _this.closePanel = function () {
      _this.setState({ isActive: false });
    }, _this.togglePanel = function () {
      if (!_this.state.isActive) {
        _this.openPanel();
      } else {
        _this.closePanel();
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Dropdown, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      document.addEventListener('click', this.handleClickOutside);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      document.removeEventListener('click', this.handleClickOutside);
    }
  }, {
    key: 'buildChildren',
    value: function buildChildren(children) {
      var _this2 = this;

      return _react2.default.Children.map(children, function (child) {
        var _props = _this2.props,
            variant = _props.variant,
            size = _props.size,
            reverse = _props.reverse;
        var isActive = _this2.state.isActive;

        var triggerActive = isActive || _this2.props.active;
        if (_react2.default.isValidElement(child)) {
          var componentName = child.type.componentName;

          switch (componentName) {
            case 'Trigger':
              return _react2.default.cloneElement(child, {
                state: triggerActive ? 'dropdown-opened' : 'dropdown-closed',
                onClick: _this2.togglePanel,
                variant: variant === 'launcher' ? 'icon' : variant,
                size: size
              });
            case 'Content':
              return _react2.default.cloneElement(child, {
                active: isActive,
                closePanel: _this2.closePanel,
                variant: variant,
                reverse: reverse
              });
            default:
              return child;
          }
        }
        return child;
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      var _props2 = this.props,
          size = _props2.size,
          children = _props2.children,
          className = _props2.className,
          props = _objectWithoutProperties(_props2, ['size', 'children', 'className']);

      var isActive = this.state.isActive;

      var childrenProps = _extends({
        togglePanel: this.togglePanel,
        openPanel: this.openPanel,
        closePanel: this.closePanel,
        isActive: isActive
      }, props);
      return _react2.default.createElement(
        'div',
        _extends({
          ref: function ref(node) {
            _this3.dropDownRef = node;
          }
        }, (0, _omit2.default)(props, ['variant', 'reverse', 'active', 'itemClassName', 'triggerClassName', 'contentClassName']), {
          className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('dropdown', size), _styleModuleImportMap)
        }),
        typeof children === 'function' ? children(childrenProps) : this.buildChildren(children)
      );
    }
  }]);

  return Dropdown;
}(_react.Component);

Dropdown.componentName = 'Dropdown';


Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;

Dropdown.Trigger = _Trigger2.default;
Dropdown.Content = _Content2.default;
Dropdown.Item = _Item2.default;
Dropdown.Separation = _Separation2.default;

exports.default = Dropdown;
module.exports = exports['default'];