import { r as registerInstance, h as h$1 } from './index-38ad4957.js';
import { i as intl } from './global-96c14d63.js';
import { n as h } from './dom-context-hooks.module-50442785.js';
import { d as dist, H, J, w as wn, i as isDemo } from './index.module-b23fdad8.js';
import { c as cjs } from './cjs-bdfb4486.js';
import { L as LeaderboardRankView } from './sqm-leaderboard-rank-view-7a682cc3.js';

const GET_RANK = dist.gql `
  query ($type: String!, $filter: UserLeaderboardFilterInput) {
    viewer {
      ... on User {
        leaderboardRank(type: $type, filter: $filter) {
          rowNumber
          rank
          denseRank
        }
      }
    }
  }
`;
function useLeaderboardRank(props) {
  var _a, _b, _c, _d;
  const programIdContext = H();
  // Default to context, overriden by props
  const programId = (_a = props.programId) !== null && _a !== void 0 ? _a : programIdContext;
  const user = J();
  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 } = wn(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,
    },
  };
}

const sqmLeaderboardRankCss = "p,.P{font-size:var(--sl-font-size-small);color:var(--sqm-text);margin-top:0px}p.Subtitle,.P.Subtitle{color:var(--sqm-text-subdued);margin:0px}";

const LeaderboardRank = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    /**
     * @uiName Rank text
     * @uiWidget textArea
     */
    this.rankText = "You are currently in {rank, selectordinal, one {#st} two {#nd} few {#rd} other {#th} } place!";
    h(this);
  }
  disconnectedCallback() { }
  render() {
    const props = isDemo()
      ? useLeaderboardRankDemo(this)
      : useLeaderboardRank(this);
    return h$1(LeaderboardRankView, Object.assign({}, props));
  }
};
function useLeaderboardRankDemo(props) {
  const rank = intl.formatMessage({ id: "rankText", defaultMessage: props.rankText }, {
    rank: 1,
  }) || "1st";
  return cjs({
    data: {
      rank,
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
LeaderboardRank.style = sqmLeaderboardRankCss;

export { LeaderboardRank as sqm_leaderboard_rank };
