import { inject, onMounted, provide, ref, type InjectionKey, type Ref } from 'vue' import { message } from 'ant-design-vue' import { getEarnMoney } from '@/api/earnMoney' import type { EarnMoney } from '@/models/earnMoney' import { useUserStore } from '@/store/userStore' import { useConfigStore } from '@/store/configStore' import { getReferralShareText, getReferralShareTitle } from '@/utils/config' import { loading } from '@/utils' export interface EarnMoneyPageContext { data: Ref userStore: ReturnType nameProject: string isTransaction: Ref loading: typeof loading setIsTransaction: (value: boolean) => void copyCode: () => void shareCode: () => void loadEarnMoney: () => Promise } export const EARN_MONEY_PAGE_CONTEXT_KEY: InjectionKey = Symbol('earnMoneyPageContext') export function useEarnMoneyPage() { const data = ref() const userStore = useUserStore() const nameProject = useConfigStore().config.name_fa const isTransaction = ref(false) function setIsTransaction(value: boolean) { isTransaction.value = value } function copyCode() { const val = userStore.userInfo?.referral_id navigator.clipboard.writeText(val) message.info('کد رونوشت شد') } function shareCode() { const val = getReferralShareText({ userName: userStore.userInfo?.name, projectName: nameProject, referralId: userStore.userInfo?.referral_id, }) navigator.share({ title: getReferralShareTitle(nameProject), text: val, }) } async function loadEarnMoney() { try { loading.value = true data.value = await getEarnMoney() } catch (e) { console.log(e) } finally { loading.value = false } } onMounted(() => { loadEarnMoney() }) const context: EarnMoneyPageContext = { data, userStore, nameProject, isTransaction, loading, setIsTransaction, copyCode, shareCode, loadEarnMoney, } provide(EARN_MONEY_PAGE_CONTEXT_KEY, context) return context } export function useEarnMoneyPageContext() { const context = inject(EARN_MONEY_PAGE_CONTEXT_KEY) if (!context) { throw new Error('useEarnMoneyPageContext must be used within a page that calls useEarnMoneyPage()') } return context }