"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ChatWrapper = require("./ChatWrapper");
var _reactRedux = require("react-redux");
var _chatSettingsSlice = require("../../roomStore/chatSettingsSlice");
var _auth = require("../../networking/api-requests/auth.api");
var _StyledComponents = require("../styled/StyledComponents");
var _RNStyled = require("./RNStyled");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const LoginWrapper = ({
  ...props
}) => {
  const [showModal, setShowModal] = (0, _react.useState)(false);
  const {
    user
  } = (0, _reactRedux.useSelector)(state => state.chatSettingStore);
  const loginUserFunction = (0, _react.useCallback)(async () => {
    try {
      // Per product-code-policy: no compiled-in dev credentials. If the
      // consumer didn't pass `user.email` / `user.password`, the call
      // below fails on empty strings, hits catch, and returns null.
      const authData = await (0, _auth.loginEmail)(props?.user?.email || '', props?.user?.password || '');
      return {
        ...authData.data.user,
        token: authData.data.token,
        refreshToken: authData.data.refreshToken
      };
    } catch (error) {
      console.error('Login failed:', error);
      return null;
    }
  }, []);
  const dispatch = (0, _reactRedux.useDispatch)();
  (0, _react.useEffect)(() => {
    // When config.initBeforeLoad is enabled, the XmppProvider owns user
    // resolution end-to-end. Skip the legacy login paths here so we don't
    // race against the provider's resolveInitBeforeLoadUser pipeline.
    if (props?.config?.initBeforeLoad) {
      return;
    }
    if (props?.config?.userLogin?.enabled && props?.config?.userLogin?.user) {
      dispatch((0, _chatSettingsSlice.setUser)(props.config.userLogin.user));
      return;
    }

    //if no login config - default user login

    if (!props.config?.googleLogin && !props.config?.defaultLogin && !props.config?.jwtLogin && !props.config?.userLogin && !props.config?.customLogin && user.xmppUsername === '') {
      const defaultLogin = async () => {
        try {
          const loginData = await loginUserFunction();
          if (loginData) {
            dispatch((0, _chatSettingsSlice.setUser)(loginData));
          }
        } catch (error) {
          console.log('error with default login', error);
          setShowModal(true);
        }
      };
      defaultLogin();
    }
    //if google - show login.tsx and process user there (there will be dispatch, set user)
    //if only ethora - show login with only ethora
    return () => {
      //clear
    };
  }, []);
  return (
    /*#__PURE__*/
    // <>
    //   {showModal ? (
    //     <View
    //       style={{
    //         ...props.MainComponentStyles,
    //         display: 'flex',
    //         justifyContent: 'center',
    //         alignItems: 'center',
    //         flexDirection: 'column',
    //         padding: '20px',
    //         gap: '8px',
    //       }}
    //     >
    //       <p>Error on loading chat. Please, try again later</p>
    //       <OrDelimiter>Or</OrDelimiter>
    //       <Button onClick={() => setShowModal(false)} style={{ width: '100%' }}>
    //         Enter with default account
    //       </Button>
    //     </View>
    //   ) : user && user.xmppPassword !== '' ? (
    //     <ChatWrapper {...props} />
    //   ) : (
    //     <LoginForm {...props} />
    //   )}
    // </>
    (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: showModal ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_RNStyled.Container, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_RNStyled.Message, {
          children: "Error on loading chat. Please, try again later"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.OrDelimiter, {
          children: "Or"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_RNStyled.CustomButton, {
          onPress: () => setShowModal(false),
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_RNStyled.ButtonText, {
            children: "Enter with default account"
          })
        })]
      }) :
      /*#__PURE__*/
      // CRITICAL: forward props so `ChatWrapper` sees `config` (and
      // therefore `initBeforeLoad`, `xmppSettings`, `refreshTokens`,
      // etc.). Without this, ChatWrapper's effect can't tell that
      // XmppProvider owns bootstrap and races against it.
      (0, _jsxRuntime.jsx)(_ChatWrapper.ChatWrapper, {
        ...props
      })
    })
  );
};
var _default = exports.default = LoginWrapper;
//# sourceMappingURL=LoginWrapper.js.map