import { f as useEffect } from './dom-context-hooks.module-50442785.js';
import { d as dist, J, B, H, Q as Qe, R as Rn, I as In } from './index.module-b23fdad8.js';

const GET_REFERRAL_CODES = dist.gql `
  query getCodes(
    $engagementMedium: UserEngagementMedium!
    $limit: Int!
    $offset: Int!
    $filter: ReferralCodeFilterInput
  ) {
    viewer {
      ... on User {
        referralCodeList(limit: $limit, offset: $offset, filter: $filter) {
          data {
            code
            dateUsed
            dateCopied
            singleUse
            shareLinkCodes(limit: $limit) {
              data {
                linkCode
                shareLink: shortUrl(
                  shareMedium: DIRECT
                  engagementMedium: EMBED
                )
                email: shortUrl(
                  shareMedium: EMAIL
                  engagementMedium: $engagementMedium
                )
                whatsApp: shortUrl(
                  shareMedium: WHATSAPP
                  engagementMedium: $engagementMedium
                )
                fbMessenger: shortUrl(
                  shareMedium: FBMESSENGER
                  engagementMedium: $engagementMedium
                )
              }
            }
          }
          count
          totalCount
        }
      }
    }
  }
`;
const REFERRAL_CODES_NAMESPACE = "sq:referral-codes";
const REFERRAL_CODES_PAGINATION_CONTEXT = "sq:referral-codes-pagination";
const SET_CODE_COPIED = dist.gql `
  mutation markReferralCodeCopied($referralCode: String!) {
    markReferralCodeCopied(referralCode: $referralCode) {
      referralCode {
        dateCopied
      }
    }
  }
`;
function useReferralCodes(props) {
  const user = J();
  const engagementMedium = B();
  const programId = props.programId || H();
  const { refresh } = Qe();
  const { envelope: referralData, states, callbacks, } = Rn(GET_REFERRAL_CODES, (data) => { var _a; return (_a = data === null || data === void 0 ? void 0 : data.viewer) === null || _a === void 0 ? void 0 : _a.referralCodeList; }, {
    limit: 1,
    offset: 0,
  }, {
    engagementMedium,
    programId,
    filter: {
      fuelTank_eq: true,
      programId_eq: programId,
      dateUsed_exists: false,
    },
  }, !(user === null || user === void 0 ? void 0 : user.jwt));
  const [paginationContext, setPaginationContext] = In({
    namespace: REFERRAL_CODES_PAGINATION_CONTEXT,
    initialValue: { states, callbacks },
  });
  const [referralCodesContext, setReferralCodesContext] = In({
    namespace: REFERRAL_CODES_NAMESPACE,
    initialValue: {
      refresh,
      loading: true,
      referralCode: "",
      shareLink: "",
      isCopied: false,
      isUsed: false,
      singleUse: false,
      email: {
        messageLink: "",
      },
      fbmessenger: {
        messageLink: "",
        shareLink: "",
      },
      whatsapp: { messageLink: "" },
    },
  });
  useEffect(() => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
    if ((_a = referralData === null || referralData === void 0 ? void 0 : referralData.data) === null || _a === void 0 ? void 0 : _a.length) {
      const data = referralData.data[0];
      setReferralCodesContext({
        refresh,
        referralCode: data.code,
        isCopied: !!data.dateCopied,
        isUsed: !!data.dateUsed,
        singleUse: !!data.singleUse,
        shareLink: (_d = (_c = (_b = data.shareLinkCodes) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.direct,
        email: {
          messageLink: (_g = (_f = (_e = data.shareLinkCodes) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.email,
        },
        fbmessenger: {
          messageLink: (_k = (_j = (_h = data.shareLinkCodes) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.fbMessenger,
          shareLink: (_o = (_m = (_l = data.shareLinkCodes) === null || _l === void 0 ? void 0 : _l.data) === null || _m === void 0 ? void 0 : _m[0]) === null || _o === void 0 ? void 0 : _o.fbMessenger,
        },
        whatsapp: { messageLink: (_r = (_q = (_p = data.shareLinkCodes) === null || _p === void 0 ? void 0 : _p.data) === null || _q === void 0 ? void 0 : _q[0]) === null || _r === void 0 ? void 0 : _r.whatsApp },
      });
      setPaginationContext({ states, callbacks });
    }
  }, [referralData]);
  return {
    states: {
      ...states,
      noCodes: (referralData === null || referralData === void 0 ? void 0 : referralData.totalCount) === 0,
    },
    data: referralCodesContext,
    callbacks: {},
  };
}

export { REFERRAL_CODES_PAGINATION_CONTEXT as R, SET_CODE_COPIED as S, REFERRAL_CODES_NAMESPACE as a, useReferralCodes as u };
