'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 _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _reactRedux = require('react-redux');

var _header = require('../actions/header');

var _index = require('../constants/index');

var _NavMenu = require('../components/navigation/NavMenu');

var _NavMenu2 = _interopRequireDefault(_NavMenu);

var _HeaderProgress = require('../components/navigation/HeaderProgress');

var _HeaderProgress2 = _interopRequireDefault(_HeaderProgress);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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 DEFAULT_HEIGHT = 80;

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

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

    var _this = _possibleConstructorReturn(this, (NaviBar.__proto__ || Object.getPrototypeOf(NaviBar)).call(this, props));

    _this.state = {
      height: DEFAULT_HEIGHT,
      isScrolledOver: false
    };
    _this._getHeaderHeight = _this._getHeaderHeight.bind(_this);
    _this._handleScroll = _this._handleScroll.bind(_this);
    return _this;
  }

  _createClass(NaviBar, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._getHeaderHeight();
      window.addEventListener('resize', this._getHeaderHeight);

      // detect sroll position
      window.addEventListener('scroll', this._handleScroll);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('resize', this._getHeaderHeight);
      window.removeEventListener('scroll', this._handleScroll);
    }
  }, {
    key: '_handleScroll',
    value: function _handleScroll() {
      var scrollPos = window.scrollY;
      if (scrollPos > this.state.height && !this.state.isScrolledOver) {
        this.setState({ isScrolledOver: true });
      } else if (scrollPos <= this.state.height && this.state.isScrolledOver) {
        this.setState({ isScrolledOver: false });
      }
    }
  }, {
    key: '_getHeaderHeight',
    value: function _getHeaderHeight() {
      var rect = _reactDom2.default.findDOMNode(this.refs.headerbox).getBoundingClientRect();
      this.setState({
        height: _lodash2.default.get(rect, 'height', DEFAULT_HEIGHT)
      });
    }
  }, {
    key: 'shouldComponentUpdate',
    value: function shouldComponentUpdate(nextProps, nextState) {
      // eslint-disable-line
      // if(nextState.height !== this.state.height ||
      //    nextProps.header.readPercent !== this.props.header.readPercent ||
      //    nextState.isScrolledOver !== this.state.isScrolledOver) {
      //   return true
      // }
      // if (nextProps.path === this.props.path) {
      //   return false
      // }
      return true;
    }
  }, {
    key: '_renderDesktop',
    value: function _renderDesktop() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_NavMenu2.default, _extends({}, this.props, {
          isScrolledOver: this.state.isScrolledOver,
          pageTitle: this.props.header.pageTitle,
          pageTopic: this.props.header.pageTopic,
          articleId: this.props.header.articleId
        }))
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var height = this.state.height;
      var header = this.props.header;

      var percent = header.readPercent || 0;

      var progressBar = header.pageType === _index.ARTICLE || header.pageType === _index.PHOTOGRAPHY_ARTICLE ? _react2.default.createElement(_HeaderProgress2.default, { percent: percent }) : null;

      return _react2.default.createElement(
        'div',
        { style: { height: height + 'px' } },
        _react2.default.createElement(
          'div',
          { ref: 'headerbox', className: 'fixTop' },
          this._renderDesktop(),
          progressBar
        )
      );
    }
  }]);

  return NaviBar;
}(_react.Component);

NaviBar.contextTypes = {
  device: _react.PropTypes.string
};

function mapStateToProps(state) {
  return {
    header: state.header || {}
  };
}

exports.default = (0, _reactRedux.connect)(mapStateToProps, { setReadProgress: _header.setReadProgress })(NaviBar);