import { constructContractIssuer, Context, extractUser, getAuthenticated, getContractId } from '@coinweb/contract-kit';

let rawContext: Context | null = null;

export const setRawContext = (ctx: Context) => {
  rawContext = ctx;
};

export const getRawContext = () => {
  if (!rawContext) {
    throw new Error('Context not set');
  }

  return rawContext;
};

export const context = {
  get issuer() {
    return constructContractIssuer(getContractId(getRawContext().tx));
  },
  get authenticated() {
    return getAuthenticated(getRawContext().tx);
  },
  get user() {
    return extractUser(getAuthenticated(getRawContext().tx));
  },
};
