'use strict';

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

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 _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _animationTimer = require('animation-timer');

var _functionalEasing = require('functional-easing');

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 Container = _styledComponents2.default.div.withConfig({
  displayName: 'Expand__Container'
})(['overflow: hidden; width: 100%; height: 100%;']);
var Content = _styledComponents2.default.div.withConfig({
  displayName: 'Expand__Content'
})(['overflow: hidden; width: 100%; height: 100%;']);

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

  function Expand() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Expand);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Expand.__proto__ || Object.getPrototypeOf(Expand)).call.apply(_ref, [this].concat(args))), _this), _this.animate = function () {
      var _this$props = _this.props,
          direction = _this$props.direction,
          duration = _this$props.duration;

      var content = _this._content;
      var naturalHeight = content.scrollHeight;
      var naturalWidth = content.scrollWidth;
      var easer = new _functionalEasing.Easer().using('cubic');

      if (direction === 'vertical' || direction === 'both') {
        content.style.height = '0px';
      }
      if (direction === 'horizontal' || direction === 'both') {
        content.style.width = '0px';
      }

      new _animationTimer.AnimationTimer().duration(duration).on('tick', easer(function (percent) {
        if (direction === 'vertical' || direction === 'both') {
          content.style.height = naturalHeight * percent + 'px';
        }
        if (direction === 'horizontal' || direction === 'both') {
          content.style.width = naturalWidth * percent + 'px';
        }
      })).on('stop', function () {
        content.style.width = '';
        content.style.height = '';
      }).play();
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Expand, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (this.props.animate) {
        this.animate();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      return _react2.default.createElement(
        Container,
        null,
        _react2.default.createElement(
          Content,
          { innerRef: function innerRef(el) {
              _this2._content = el;
            } },
          this.props.children
        )
      );
    }
  }]);

  return Expand;
}(_react2.default.Component);

Expand.propTypes = {
  animate: _propTypes2.default.bool,
  direction: _propTypes2.default.oneOf(['vertical', 'horizontal', 'both']),
  children: _propTypes2.default.node.isRequired,
  duration: _propTypes2.default.number
};
Expand.defaultProps = {
  animate: true,
  direction: 'vertical',
  duration: 200
};
exports.default = Expand;