'use strict';

exports.__esModule = true;

var _pick2 = require('lodash/pick');

var _pick3 = _interopRequireDefault(_pick2);

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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Collapse = require('rsuite-utils/lib/Animation/Collapse');

var _Collapse2 = _interopRequireDefault(_Collapse);

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

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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 Panel = function (_React$Component) {
  _inherits(Panel, _React$Component);

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

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

    _this.handleSelect = function (event) {
      event.persist();
      event.selected = true;
      var _this$props = _this.props,
          onSelect = _this$props.onSelect,
          eventKey = _this$props.eventKey;

      if (onSelect) {
        onSelect(eventKey, event);
      } else {
        event.preventDefault();
      }

      if (event.selected) {
        _this.handleToggle();
      }
    };

    _this.handleToggle = function () {
      _this.setState({ expanded: !_this.state.expanded });
    };

    _this.addPrefix = function (name) {
      return (0, _utils.prefix)(_this.props.classPrefix)(name);
    };

    _this.state = {
      expanded: props.defaultExpanded
    };
    return _this;
  }

  Panel.prototype.isExpanded = function isExpanded() {
    return (0, _isUndefined3.default)(this.props.expanded) ? this.state.expanded : this.props.expanded;
  };

  Panel.prototype.renderCollapsibleTitle = function renderCollapsibleTitle(header, headerRole) {
    return React.createElement(
      'h4',
      { className: this.addPrefix('title'), role: 'presentation' },
      this.renderAnchor(header, headerRole)
    );
  };

  Panel.prototype.renderCollapsibleBody = function renderCollapsibleBody(panelRole) {
    var id = this.props.id;

    var collapseProps = _extends({}, (0, _pick3.default)(this.props, _Collapse2.default.handledProps), {
      in: this.isExpanded()
    });

    var props = {
      id: id,
      className: this.addPrefix('collapse'),
      'aria-hidden': !this.isExpanded()
    };
    if (panelRole) {
      props.role = panelRole;
    }

    return React.createElement(
      _Collapse2.default,
      collapseProps,
      React.createElement(
        'div',
        props,
        this.renderBody()
      )
    );
  };

  Panel.prototype.renderBody = function renderBody() {
    var _classNames;

    var _props = this.props,
        children = _props.children,
        bodyFill = _props.bodyFill;

    var classes = (0, _classnames2.default)(this.addPrefix('body'), (_classNames = {}, _classNames[this.addPrefix('body-fill')] = bodyFill, _classNames));

    return React.createElement(
      'div',
      { className: classes },
      children
    );
  };

  Panel.prototype.renderHeading = function renderHeading(headerRole) {
    var _props2 = this.props,
        header = _props2.header,
        collapsible = _props2.collapsible;


    if (!header) {
      return null;
    }

    if (!React.isValidElement(header) || Array.isArray(header)) {
      header = collapsible ? this.renderCollapsibleTitle(header, headerRole) : header;
    } else {
      var _className = (0, _classnames2.default)(this.addPrefix('title'), header.props.className);
      header = React.cloneElement(header, { className: _className });
    }
    return React.createElement(
      'div',
      {
        role: 'rowheader',
        className: this.addPrefix('heading'),
        onClick: this.handleSelect,
        tabIndex: -1
      },
      header
    );
  };

  Panel.prototype.renderAnchor = function renderAnchor(header, headerRole) {
    var _props3 = this.props,
        id = _props3.id,
        collapsible = _props3.collapsible;


    return React.createElement(
      'a',
      {
        href: '#' + (id || ''),
        'aria-controls': collapsible ? id : null,
        className: this.isExpanded() ? null : 'collapsed',
        'aria-expanded': this.isExpanded(),
        'aria-selected': this.isExpanded(),
        role: headerRole
      },
      header
    );
  };

  Panel.prototype.render = function render() {
    var _classNames2;

    var _props4 = this.props,
        headerRole = _props4.headerRole,
        panelRole = _props4.panelRole,
        className = _props4.className,
        collapsible = _props4.collapsible,
        bordered = _props4.bordered,
        classPrefix = _props4.classPrefix,
        id = _props4.id,
        props = _objectWithoutProperties(_props4, ['headerRole', 'panelRole', 'className', 'collapsible', 'bordered', 'classPrefix', 'id']);

    var clesses = (0, _classnames2.default)(classPrefix, this.addPrefix('default'), className, (_classNames2 = {}, _classNames2[this.addPrefix('in')] = this.isExpanded(), _classNames2[this.addPrefix('collapsible')] = collapsible, _classNames2[this.addPrefix('bordered')] = bordered, _classNames2));

    var unhandled = (0, _utils.getUnhandledProps)(Panel, props);

    return React.createElement(
      'div',
      _extends({}, unhandled, { className: clesses, onSelect: null, id: collapsible ? null : id }),
      this.renderHeading(headerRole),
      collapsible ? this.renderCollapsibleBody(panelRole) : this.renderBody()
    );
  };

  return Panel;
}(React.Component);

Panel.handledProps = ['bodyFill', 'bordered', 'children', 'className', 'classPrefix', 'collapsible', 'defaultExpanded', 'eventKey', 'expanded', 'header', 'headerRole', 'id', 'onEnter', 'onEntered', 'onEntering', 'onExit', 'onExited', 'onExiting', 'onSelect', 'panelRole'];
Panel.propTypes = {
  collapsible: _propTypes2.default.bool,
  bordered: _propTypes2.default.bool,
  bodyFill: _propTypes2.default.bool,
  header: _propTypes2.default.any,
  id: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  defaultExpanded: _propTypes2.default.bool,
  expanded: _propTypes2.default.bool,
  eventKey: _propTypes2.default.any,
  headerRole: _propTypes2.default.string,
  panelRole: _propTypes2.default.string,
  classPrefix: _propTypes2.default.string,
  children: _propTypes2.default.node,
  onSelect: _propTypes2.default.func,
  onEnter: _propTypes2.default.func,
  onEntering: _propTypes2.default.func,
  onEntered: _propTypes2.default.func,
  onExit: _propTypes2.default.func,
  onExiting: _propTypes2.default.func,
  onExited: _propTypes2.default.func,
  className: _propTypes2.default.string
};
exports.default = (0, _utils.defaultProps)({
  classPrefix: 'panel'
})(Panel);