import { CliLeaf } from '@alwaysai/alwayscli';
import { yesCliInput } from '../../cli-inputs';
import { checkUserIsLoggedInComponent } from '../../components/user';
import { ALWAYSAI_SHOW_HIDDEN } from '../../environment';
import { CliAuthenticationClient } from '../../infrastructure';

export const getAccessJwtCliLeaf = CliLeaf({
  name: 'get-access-jwt',
  description:
    'Get a JSON web token (JWT) for accessing the alwaysAI Cloud API',
  hidden: !ALWAYSAI_SHOW_HIDDEN,
  namedInputs: {
    yes: yesCliInput
  },
  async action(_, { yes }) {
    await checkUserIsLoggedInComponent({ yes });
    const { Authorization } =
      await CliAuthenticationClient().getAuthorizationHeader();
    return Authorization.split(' ')[1];
  }
});
