"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = require("styled-components");
var _getMonitor = _interopRequireDefault(require("../utils/getMonitor"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
class DevTools extends _react.Component {
  constructor(props) {
    super(props);
    this.getMonitor(props, props.monitorState);
  }
  getMonitor(props, skipUpdate) {
    const monitorElement = (0, _getMonitor.default)(props);
    this.monitorProps = monitorElement.props;
    this.Monitor = monitorElement.type;

    // eslint-disable-next-line @typescript-eslint/unbound-method
    const update = this.Monitor.update;
    if (update) {
      let newMonitorState;
      const monitorState = props.monitorState;
      if (skipUpdate || monitorState && monitorState.__overwritten__ === props.monitor) {
        newMonitorState = monitorState;
      } else {
        newMonitorState = update(this.monitorProps, undefined, {});
        if (newMonitorState !== monitorState) {
          this.preventRender = true;
        }
      }
      this.dispatch({
        type: '@@INIT_MONITOR',
        newMonitorState,
        update,
        monitorProps: this.monitorProps
      });
    }
  }
  UNSAFE_componentWillUpdate(nextProps) {
    if (nextProps.monitor !== this.props.monitor) this.getMonitor(nextProps);
  }
  shouldComponentUpdate(nextProps) {
    return nextProps.monitor !== this.props.monitor || nextProps.liftedState !== this.props.liftedState || nextProps.monitorState !== this.props.monitorState || nextProps.features !== this.props.features || nextProps.theme.scheme !== this.props.theme.scheme;
  }
  dispatch = action => {
    this.props.dispatch(action);
  };
  render() {
    if (this.preventRender) {
      this.preventRender = false;
      return null;
    }
    const liftedState = {
      ...this.props.liftedState,
      monitorState: this.props.monitorState
    };
    const MonitorAsAny = this.Monitor;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: `monitor monitor-${this.props.monitor}`
    }, /*#__PURE__*/_react.default.createElement(MonitorAsAny, (0, _extends2.default)({}, liftedState, this.monitorProps, {
      features: this.props.features,
      dispatch: this.dispatch,
      theme: this.props.theme,
      sortStateTreeAlphabetically: this.props.stateTreeSettings.sortAlphabetically,
      disableStateTreeCollection: this.props.stateTreeSettings.disableCollection
    })));
  }
}
var _default = exports.default = (0, _styledComponents.withTheme)(DevTools);