import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import matchPath from 'rudy-match-path';
import { LocationProvider, Router } from '@reach/router';
import { CommonProvider, ModalFormContainer, ToastsContainer } from '@kineticdata/bundle-common';
import { is } from 'immutable';
import { connectedHistory, context, store } from './redux/store';
import { syncAppState } from './redux/modules/app';
import { App } from './App';

var _default = /*#__PURE__*/function (_Component) {
  _inherits(_default, _Component);

  var _super = _createSuper(_default);

  function _default(props) {
    var _this;

    _classCallCheck(this, _default);

    _this = _super.call(this, props);
    _this.state = {
      ready: false
    }; // Listen to the local store to see if the embedded app is ready to be
    // re-rendered. Currently this just means that the required props have been
    // synced into the local store.

    _this.unsubscribe = store.subscribe(function () {
      var ready = store.getState().app.ready;

      if (ready !== _this.state.ready) {
        _this.setState({
          ready: ready
        });
      }
    });
    return _this;
  }

  _createClass(_default, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      Object.entries(this.props.appState).forEach(syncAppState);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      Object.entries(this.props.appState).filter(function (_ref) {
        var _ref2 = _slicedToArray(_ref, 2),
            key = _ref2[0],
            value = _ref2[1];

        return !is(value, prevProps.appState[key]);
      }).forEach(syncAppState);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.unsubscribe();
    }
  }, {
    key: "render",
    value: function render() {
      return this.state.ready && /*#__PURE__*/React.createElement(Provider, {
        store: store,
        context: context
      }, /*#__PURE__*/React.createElement(CommonProvider, null, /*#__PURE__*/React.createElement(LocationProvider, {
        hashRouting: true,
        history: connectedHistory
      }, /*#__PURE__*/React.createElement(ToastsContainer, {
        duration: 5000
      }), /*#__PURE__*/React.createElement(ModalFormContainer, null), /*#__PURE__*/React.createElement(Router, null, /*#__PURE__*/React.createElement(App, {
        render: this.props.render,
        path: "".concat(this.props.appState.location, "/*")
      })))));
    }
  }]);

  return _default;
}(Component);

_default.shouldSuppressSidebar = function (_ref3) {
  var appLocation = _ref3.appLocation,
      location = _ref3.location;
  return matchPath(location.pathname, {
    path: appLocation
  }) && !matchPath(location.pathname, {
    path: "".concat(appLocation, "/settings")
  });
};

_default.shouldHideHeader = function (_ref4) {
  var appLocation = _ref4.appLocation,
      location = _ref4.location;
  return matchPath(location.pathname, {
    path: "".concat(appLocation, "/display")
  });
};

_default.hasPublicRoutes = false;
_default.bodyClassName = 'package--tech-bar';
export { _default as default };