import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import "./chunk-54RFHRDU.js";
import {
  addUserFields
} from "./chunk-VC5Q2AI5.js";
import {
  ApiKeyLogin,
  LoginWithI18n,
  RefreshTokenLogin,
  useBiLogger,
  useErrorReporter
} from "./chunk-HWT3LUJJ.js";
import "./chunk-TOBTRYZF.js";
import "./chunk-7WVNO7VU.js";
import "./chunk-RPTAJN5Y.js";
import {
  ConfirmInput,
  Text,
  useExit
} from "./chunk-YL4347CI.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import "./chunk-AFPDHQYI.js";
import "./chunk-HYKBBZLS.js";
import "./chunk-C5RTKQVL.js";
import "./chunk-SVUXJRFD.js";
import "./chunk-YT5BPQDJ.js";
import "./chunk-LU3R3EBR.js";
import "./chunk-XZUGLEBK.js";
import "./chunk-VASURI4S.js";
import "./chunk-SDLCFZRL.js";
import {
  require_lib
} from "./chunk-MTT3QXBC.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-KTAURQDU.js.map