import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import "./chunk-EGTKQWHU.js";
import {
  addUserFields
} from "./chunk-V2542KTT.js";
import {
  ApiKeyLogin,
  LoginWithI18n,
  RefreshTokenLogin,
  useBiLogger,
  useErrorReporter
} from "./chunk-VZ3FZHJK.js";
import "./chunk-OWRWPLVG.js";
import "./chunk-CIOBCAOI.js";
import "./chunk-RPTAJN5Y.js";
import {
  ConfirmInput,
  Text,
  useExit
} from "./chunk-2DKXIL7C.js";
import {
  require_react
} from "./chunk-7ZOEVDW3.js";
import "./chunk-AFPDHQYI.js";
import "./chunk-HYKBBZLS.js";
import "./chunk-C5RTKQVL.js";
import "./chunk-LFX6WS3B.js";
import "./chunk-XTVDTH5G.js";
import "./chunk-TMZBOD2B.js";
import "./chunk-DR2IE2NL.js";
import "./chunk-JWVFFICN.js";
import "./chunk-YRSNPU5L.js";
import {
  require_lib
} from "./chunk-PIOT47FJ.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// src/components/LoginCommand/index.ts
init_esm_shims();

// src/components/LoginCommand/LoginCommand.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);

// src/components/LoginCommand/AlreadyLoggedIn.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var AlreadyLoggedIn = ({ userInfo, onSubmit }) => {
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(Text, null, "Wix CLI is currently logged in to account", " ", /* @__PURE__ */ import_react.default.createElement(Text, { skin: "info" }, userInfo.email)), /* @__PURE__ */ import_react.default.createElement(
    ConfirmInput,
    {
      label: "Do you want to log in to a different account?",
      initialValue: true,
      onSubmit
    }
  ));
};

// src/components/LoginCommand/LoginCommand.tsx
var LoginState = (0, import_variant.variant)({
  PendingUserConfirmation: {},
  InProgress: {},
  Success: (0, import_variant.fields)()
});
var LoginCommand = ({
  apiKeyToken,
  refreshToken,
  userInfo: initialUserInfo
}) => {
  const exit = useExit();
  const errorReporter = useErrorReporter();
  const biLogger = useBiLogger();
  const [loginState, setLoginState] = (0, import_react2.useState)(
    initialUserInfo == null ? LoginState.InProgress() : LoginState.PendingUserConfirmation()
  );
  const handleLogin = (0, import_react2.useCallback)(
    ({ userInfo }) => {
      errorReporter.setUser({ id: userInfo.userId });
      addUserFields(biLogger, userInfo.userId);
      setLoginState(LoginState.Success({ userInfo }));
    },
    [setLoginState, errorReporter, biLogger]
  );
  return /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, initialUserInfo && /* @__PURE__ */ import_react2.default.createElement(
    AlreadyLoggedIn,
    {
      userInfo: initialUserInfo,
      onSubmit: (relogin) => relogin && setLoginState(LoginState.InProgress())
    }
  ), (0, import_variant.match)(loginState, {
    PendingUserConfirmation: () => null,
    InProgress: () => apiKeyToken ? /* @__PURE__ */ import_react2.default.createElement(
      ApiKeyLogin,
      {
        token: apiKeyToken,
        onLogin: handleLogin,
        onError: exit
      }
    ) : refreshToken ? /* @__PURE__ */ import_react2.default.createElement(
      RefreshTokenLogin,
      {
        refreshToken,
        onLogin: handleLogin,
        onError: exit
      }
    ) : /* @__PURE__ */ import_react2.default.createElement(LoginWithI18n, { onLogin: handleLogin, onError: exit }),
    Success: ({ userInfo }) => /* @__PURE__ */ import_react2.default.createElement(Text, null, "Wix CLI is logged in to account", " ", /* @__PURE__ */ import_react2.default.createElement(Text, { skin: "info" }, userInfo.email))
  }));
};
export {
  LoginCommand
};
//# sourceMappingURL=LoginCommand-SANEWDRO.js.map