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

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _jump = require('jump.js');

var _jump2 = _interopRequireDefault(_jump);

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

var _omit2 = _interopRequireDefault(_omit);

var _Button = require('../../Button');

var _Button2 = _interopRequireDefault(_Button);

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

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 = {
  onClick: _propTypes2.default.func,
  duration: _propTypes2.default.number,
  offset: _propTypes2.default.number,
  to: _propTypes2.default.string,
  withHashTag: _propTypes2.default.bool,
  scrollJump: _propTypes2.default.bool,
  variant: _propTypes2.default.string
};

var defaultProps = {
  onClick: undefined,
  duration: 500,
  offset: 0,
  to: '',
  withHashTag: false,
  scrollJump: null,
  variant: 'button-collapsible-card'
};

var CollapsibleItem = function (_PureComponent) {
  _inherits(CollapsibleItem, _PureComponent);

  function CollapsibleItem() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, CollapsibleItem);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = CollapsibleItem.__proto__ || Object.getPrototypeOf(CollapsibleItem)).call.apply(_ref, [this].concat(args))), _this), _this.componentDidMount = function () {
      var _this$props = _this.props,
          scrollJump = _this$props.scrollJump,
          offset = _this$props.offset,
          to = _this$props.to;

      if (scrollJump && '#' + to === window.location.hash) {
        (0, _jump2.default)('#' + to, { duration: 0, offset: offset });
      }
    }, _this.handleClick = function (e) {
      var _this$props2 = _this.props,
          onClick = _this$props2.onClick,
          scrollJump = _this$props2.scrollJump,
          duration = _this$props2.duration,
          to = _this$props2.to,
          offset = _this$props2.offset,
          withHashTag = _this$props2.withHashTag;

      if (typeof onClick === 'function') onClick(e);
      if (scrollJump) {
        if (withHashTag) {
          var _window = window,
              scrollX = _window.scrollX,
              scrollY = _window.scrollY;

          window.location.hash = to;
          window.scroll(scrollX, scrollY);
          setTimeout(function () {
            (0, _jump2.default)('#' + to, { duration: duration, offset: offset });
          }, 50);
        } else {
          (0, _jump2.default)('#' + to, { duration: duration, offset: offset });
        }
      }
      e.preventDefault();
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(CollapsibleItem, [{
    key: 'render',
    value: function render() {
      var variant = this.props.variant;

      return _react2.default.createElement(_Button2.default, _extends({
        onClick: this.handleClick,
        variant: variant
      }, (0, _omit2.default)(this.props, Object.keys(propTypes))));
    }
  }]);

  return CollapsibleItem;
}(_react.PureComponent);

CollapsibleItem.propTypes = propTypes;
CollapsibleItem.defaultProps = defaultProps;

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