import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { createStructuredSelector } from 'reselect';

// import { withReducer } from '../../app/core/with-reducer';
// import { withEpics } from '../../app/core/with-epics';
// import { NavigationStackProp } from 'react-navigation-stack';
import { SBox } from '../../common/s-components/layout/s-box';
// import reducer, { ACTION } from '../reducer';
import { ACTION as COMMON_ACTION } from '../../common/reducer';
import { makeAuthTokenSelector } from '../selectors';
import { theme } from '../../app/theme';
import { withErrorBoundary } from '../../common/hoc/with-error-boundary';
import { TAuthRootState, TUndefString } from '../types';
import { Router } from '../../common/utils/navigation-service';
// import { useIntercom } from '../../welcome-survey/hooks/use-intercom';

type TConnected = {
  token: TUndefString;
};

const mapStateToProps = createStructuredSelector<TAuthRootState, TConnected>({
  token: makeAuthTokenSelector(),
  //†prop
});
const mapDispatchToProps = {
  checkOnboarding: COMMON_ACTION.checkOnboarding,
  //†action
};

type TProps = TConnected & {
  // https://reactnavigation.org/docs/4.x/typescript/
  // navigation: NavigationStackProp<undefined>;
  checkOnboarding: () => void;
};

const Connected: React.FC<TProps> = ({ token, checkOnboarding }) => {
  // useIntercom();

  useEffect(() => {
    if (token) {
      // @ts-ignore
      // eslint-disable-next-line
      // console.rx({
      //   CHECK_ONBOARDING: true,
      // });
      checkOnboarding();
    } else {
      // @ts-ignore
      // eslint-disable-next-line
      // console.rx({
      //   NAVIGATE_TO_AUTH: true,
      // });
      Router.navigateTo('Auth');
    }
  }, []);

  return <SBox flex={1} width={1} bg={theme.colors.ebonyClay} />;
};

export default compose(
  // withReducer('auth', reducer),
  // withEpics('auth', epics),
  withErrorBoundary('auth:auth-loading'),
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
)(Connected);
