import { Fragment, jsx } from "react/jsx-runtime";
import { useEffect, useState, useRef } from "react";
import { useAppDispatch } from "../../data/react.js";
import { login } from "../../utils/auth/index.js";
import { LOGIN_SUCCESS } from "../../data/auth/actions.js";
const Login = ({
  clientId,
  scope,
  responseType,
  children,
  isConsumerUser
}) => {
  const [userAuthorized, setUserAuthorized] = useState(false);
  const dispatch = useAppDispatch();
  const ref = useRef(false);
  useEffect(() => {
    if (ref.current) return;
    ref.current = true;
    login({ clientId, scope, responseType, isConsumerUser }).then(({ authorized }) => {
      if (authorized) setUserAuthorized(true);
      dispatch({ type: LOGIN_SUCCESS });
    }).catch(() => {
    }).finally(() => {
      ref.current = false;
    });
  }, [dispatch, clientId, scope, responseType, isConsumerUser]);
  return userAuthorized ? /* @__PURE__ */ jsx(Fragment, { children }) : null;
};
export {
  Login
};
