(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(['exports', 'react', 'classnames', './px-tree-node.scss', './px-tree-icon', '../BaseComponent'], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require('react'), require('classnames'), require('./px-tree-node.scss'), require('./px-tree-icon'), require('../BaseComponent'));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.react, global.classnames, global.pxTreeNode, global.pxTreeIcon, global.BaseComponent);
    global.pxTreeNode = mod.exports;
  }
})(this, function (exports, _react, _classnames, _pxTreeNode, _pxTreeIcon, _BaseComponent2) {
  'use strict';

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

  var _react2 = _interopRequireDefault(_react);

  var _classnames2 = _interopRequireDefault(_classnames);

  var _pxTreeNode2 = _interopRequireDefault(_pxTreeNode);

  var _pxTreeIcon2 = _interopRequireDefault(_pxTreeIcon);

  var _BaseComponent3 = _interopRequireDefault(_BaseComponent2);

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

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

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

  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 TreeNode = function (_BaseComponent) {
    _inherits(TreeNode, _BaseComponent);

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

      var _this = _possibleConstructorReturn(this, (TreeNode.__proto__ || Object.getPrototypeOf(TreeNode)).call(this, props, { displayName: 'TreeNode' }));

      _this.state = {
        children: []
      };
      _this.onCategorySelect = _this.onCategorySelect.bind(_this);
      _this.onChildDisplayToggle = _this.onChildDisplayToggle.bind(_this);
      return _this;
    }

    _createClass(TreeNode, [{
      key: 'onCategorySelect',
      value: function onCategorySelect(ev) {
        this._log('onCategorySelect', ev);
        if (this.props.onCategorySelect) {
          this.props.onCategorySelect(this);
        }
        ev.preventDefault();
        ev.stopPropagation();
      }
    }, {
      key: 'onChildDisplayToggle',
      value: function onChildDisplayToggle(ev) {
        this._log('onChildDisplayToggle', ev);

        if (this.props.data.children) {
          if (this.state.children && this.state.children.length) {
            this.setState({ children: null });
          } else {
            this.setState({ children: this.props.data.children });
          }
        }
        ev.preventDefault();
        ev.stopPropagation();
      }
    }, {
      key: 'render',
      value: function render() {
        var _this2 = this;

        if (!this.state.children) {
          this.state.children = [];
        }
        var icon = null;

        var isOpen = this.state.children.length ? true : false;
        var hasChildren = this.props && this.props.data && this.props.data.children;

        var baseClasses = (0, _classnames2.default)('px-tree-node', { tree__leaf: !hasChildren }, { tree__branch: hasChildren }, { selected: this.state.selected }, { open: isOpen }, { closed: this.state.children ? false : true });

        var treeNodeClasses = (0, _classnames2.default)('tree-node', { active: this.state.selected });

        var treeLabelClasses = (0, _classnames2.default)('tree__label', { selected: this.state.selected });

        return _react2.default.createElement(
          'li',
          { className: baseClasses, ref: 'node', onClick: this.onChildDisplayToggle },
          _react2.default.createElement(
            'span',
            { className: treeLabelClasses, 'data-id': this.props.data.id },
            hasChildren && _react2.default.createElement(_pxTreeIcon2.default, { className: 'tree-node__icon', open: isOpen }),
            icon && _react2.default.createElement(
              'span',
              { className: 'tree-node__icon' },
              icon
            ),
            _react2.default.createElement(
              'span',
              { onClick: this.onCategorySelect },
              this.props.data.label
            )
          ),
          this.state.children && _react2.default.createElement(
            'ul',
            { className: 'collapse-content tree-node__content' },
            this.state.children.map(function (child, index) {
              return _react2.default.createElement(TreeNode, {
                key: index,
                data: child,
                onCategorySelect: _this2.props.onCategorySelect });
            })
          ),
          _react2.default.createElement(
            'style',
            null,
            '' + _pxTreeNode2.default
          )
        );
      }
    }]);

    return TreeNode;
  }(_BaseComponent3.default);

  exports.default = TreeNode;
});