import {
  ensureAuthenticated,
  exchangeCustomPartnerToken,
  getPartnersToken,
  isThemeAccessSession,
  normalizeStoreFqdn,
  remove,
  setLastSeenAuthMethod,
  setLastSeenUserIdAfterAuth
} from "./chunk-OWRIOLFJ.js";
import {
  nonRandomUUID
} from "./chunk-KLJ2TVHE.js";
import {
  BugError,
  outputContent,
  outputDebug,
  outputToken
} from "./chunk-LA4HX5WI.js";
import {
  init_cjs_shims
} from "./chunk-POZ5MGPT.js";

// ../cli-kit/dist/public/node/session.js
init_cjs_shims();
async function ensureAuthenticatedPartners(scopes = [], env = process.env, options = {}) {
  outputDebug(outputContent`Ensuring that the user is authenticated with the Partners API with the following scopes:
${outputToken.json(scopes)}
`);
  let envToken = getPartnersToken();
  if (envToken) {
    let result = await exchangeCustomPartnerToken(envToken);
    return { token: result.accessToken, userId: result.userId };
  }
  let tokens = await ensureAuthenticated({ partnersApi: { scopes } }, env, options);
  if (!tokens.partners)
    throw new BugError("No partners token found after ensuring authenticated");
  return { token: tokens.partners, userId: tokens.userId };
}
async function ensureAuthenticatedAppManagement(scopes = [], env = process.env, options = {}) {
  outputDebug(outputContent`Ensuring that the user is authenticated with the App Management API with the following scopes:
${outputToken.json(scopes)}
`);
  let tokens = await ensureAuthenticated({ appManagementApi: { scopes } }, env, options);
  if (!tokens)
    throw new BugError("No App Management token found after ensuring authenticated");
  return { token: tokens.appManagement, userId: tokens.userId };
}
async function ensureAuthenticatedStorefront(scopes = [], password = void 0, forceRefresh = !1) {
  if (password) {
    let authMethod = isThemeAccessSession({ token: password, storeFqdn: "" }) ? "theme_access_token" : "custom_app_token";
    return setLastSeenAuthMethod(authMethod), setLastSeenUserIdAfterAuth(nonRandomUUID(password)), password;
  }
  outputDebug(outputContent`Ensuring that the user is authenticated with the Storefront API with the following scopes:
${outputToken.json(scopes)}
`);
  let tokens = await ensureAuthenticated({ storefrontRendererApi: { scopes } }, process.env, { forceRefresh });
  if (!tokens.storefront)
    throw new BugError("No storefront token found after ensuring authenticated");
  return tokens.storefront;
}
async function ensureAuthenticatedAdmin(store, scopes = [], forceRefresh = !1, options = {}) {
  outputDebug(outputContent`Ensuring that the user is authenticated with the Admin API with the following scopes for the store ${outputToken.raw(store)}:
${outputToken.json(scopes)}
`);
  let tokens = await ensureAuthenticated({ adminApi: { scopes, storeFqdn: store } }, process.env, {
    forceRefresh,
    ...options
  });
  if (!tokens.admin)
    throw new BugError("No admin token found after ensuring authenticated");
  return tokens.admin;
}
async function ensureAuthenticatedThemes(store, password, scopes = [], forceRefresh = !1) {
  if (outputDebug(outputContent`Ensuring that the user is authenticated with the Theme API with the following scopes:
${outputToken.json(scopes)}
`), password) {
    let session = { token: password, storeFqdn: await normalizeStoreFqdn(store) }, authMethod = isThemeAccessSession(session) ? "theme_access_token" : "custom_app_token";
    return setLastSeenAuthMethod(authMethod), setLastSeenUserIdAfterAuth(nonRandomUUID(password)), session;
  }
  return ensureAuthenticatedAdmin(store, scopes, forceRefresh);
}
async function ensureAuthenticatedBusinessPlatform(scopes = []) {
  outputDebug(outputContent`Ensuring that the user is authenticated with the Business Platform API with the following scopes:
${outputToken.json(scopes)}
`);
  let tokens = await ensureAuthenticated({ businessPlatformApi: { scopes } }, process.env);
  if (!tokens.businessPlatform)
    throw new BugError("No business-platform token found after ensuring authenticated");
  return tokens.businessPlatform;
}
function logout() {
  return remove();
}

export {
  ensureAuthenticatedPartners,
  ensureAuthenticatedAppManagement,
  ensureAuthenticatedStorefront,
  ensureAuthenticatedAdmin,
  ensureAuthenticatedThemes,
  ensureAuthenticatedBusinessPlatform,
  logout
};
//# sourceMappingURL=chunk-4KGFXEAN.js.map
