import { useProgramId, useQuery, useUserIdentity, } from "@saasquatch/component-boilerplate";
import { gql } from "graphql-request";
import { intl } from "../../global/global";
const GET_RANK = gql `
  query ($type: String!, $filter: UserLeaderboardFilterInput) {
    viewer {
      ... on User {
        leaderboardRank(type: $type, filter: $filter) {
          rowNumber
          rank
          denseRank
        }
      }
    }
  }
`;
export function useLeaderboardRank(props) {
  var _a, _b, _c, _d;
  const programIdContext = useProgramId();
  // Default to context, overriden by props
  const programId = (_a = props.programId) !== null && _a !== void 0 ? _a : programIdContext;
  const user = useUserIdentity();
  const rankVariables = programId
    ? {
      type: props.leaderboardType,
      filter: { programId_eq: programId },
    }
    : {
      type: props.leaderboardType,
      filter: {},
    };
  if (props.interval) {
    rankVariables.filter["interval"] = props.interval;
  }
  const { data: rankData } = useQuery(GET_RANK, rankVariables, !(user === null || user === void 0 ? void 0 : user.jwt));
  const fullRankText = ((_b = rankData === null || rankData === void 0 ? void 0 : rankData.viewer) === null || _b === void 0 ? void 0 : _b.leaderboardRank) ? intl.formatMessage({ id: "rankText", defaultMessage: props.rankText }, {
    rank: (_d = (_c = rankData === null || rankData === void 0 ? void 0 : rankData.viewer) === null || _c === void 0 ? void 0 : _c.leaderboardRank) === null || _d === void 0 ? void 0 : _d[props.rankType],
  })
    : props.unrankedText || "unranked";
  return {
    data: {
      rank: fullRankText,
    },
  };
}
