import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import React from 'react';
import { connect } from 'react-redux';
import { Route } from 'react-router-dom';
import { routeProp } from '../../utils/propTypes';
import * as actions from './actions';
import '../../style/core/css/cagov.core.css';
import '../../style/style.scss'; // main container for applications - handles routing and user settings

export var Router =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(Router, _React$PureComponent);

  function Router() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Router);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Router)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.registerRouting = function () {
      var _this$props = _this.props,
          updateRouting = _this$props.updateRouting,
          routes = _this$props.routes,
          contactLink = _this$props.contactLink; // update store with routes and current pages hash

      updateRouting({
        routes: routes,
        contactLink: contactLink,
        hash: window.location.hash.replace('#', '')
      }); // register for future changes and clear errors on page change

      window.addEventListener('hashchange', function (event) {
        var hash = event.newURL.replace(/^[^#]*#/, '');
        updateRouting({
          hash: hash
        });
      });
    };

    return _this;
  }

  _createClass(Router, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.registerRouting();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var _this$props2 = this.props,
          routes = _this$props2.routes,
          updateRouting = _this$props2.updateRouting;

      if (routes !== nextProps.routes) {
        updateRouting({
          routes: nextProps.routes
        });
      }
    } // register routes in redux for navigation and
    // handle updating on change

  }, {
    key: "render",
    value: function render() {
      var routes = this.props.routes;
      return routes.map(function (route) {
        return React.createElement(Route, route);
      });
    }
  }]);

  return Router;
}(React.PureComponent);
Router.defaultProps = {
  contactLink: '/help'
};
export var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    updateRouting: function updateRouting(props) {
      return dispatch(actions.updateRouting(props));
    }
  };
};
var withRedux = connect(null, mapDispatchToProps);
export default withRedux(Router);