// AUTO-GENERATED by mizan — do not edit

import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'
import { registerContext, type ContextState } from '@mizan/base'

import { fetchUserContext, type UserContextData, type UserContextParams, callUpdateProfile, callEcho, callFindUser, callRenameUser, callWhoami } from '../index'

export function useUserContext(params: UserContextParams) {
    const state = ref<ContextState<UserContextData>>({ data: null, status: 'idle', error: null })
    let handle: ReturnType<typeof registerContext> | null = null

    onMounted(() => {
        handle = registerContext('user', params, () => fetchUserContext(params))
        handle.subscribe(() => { state.value = handle!.getState() })
        handle.refetch()
    })

    onServerPrefetch(async () => {
        handle = registerContext('user', params, () => fetchUserContext(params))
        await handle.refetch()
        state.value = handle.getState()
    })

    onUnmounted(() => { handle?.unregister() })

    return {
        state,
        userOrders: computed(() => state.value.data?.user_orders ?? null) as ComputedRef<userOrdersOutput | null>,
        userProfile: computed(() => state.value.data?.user_profile ?? null) as ComputedRef<userProfileOutput | null>,
        loading: computed(() => state.value.status === 'loading'),
        error: computed(() => state.value.error),
    }
}

export function useUpdateProfile() {
    const isPending = ref(false)
    const error = ref<Error | null>(null)
    async function mutate(args: Parameters<typeof callUpdateProfile>[0]) {
        isPending.value = true; error.value = null
        try { return await callUpdateProfile(args) }
        catch (e) { error.value = e as Error; throw e }
        finally { isPending.value = false }
    }
    return { mutate, isPending, error }
}

export function useEcho() {
    const isPending = ref(false)
    const error = ref<Error | null>(null)
    async function mutate(args: Parameters<typeof callEcho>[0]) {
        isPending.value = true; error.value = null
        try { return await callEcho(args) }
        catch (e) { error.value = e as Error; throw e }
        finally { isPending.value = false }
    }
    return { mutate, isPending, error }
}

export function useFindUser() {
    const isPending = ref(false)
    const error = ref<Error | null>(null)
    async function mutate(args: Parameters<typeof callFindUser>[0]) {
        isPending.value = true; error.value = null
        try { return await callFindUser(args) }
        catch (e) { error.value = e as Error; throw e }
        finally { isPending.value = false }
    }
    return { mutate, isPending, error }
}

export function useRenameUser() {
    const isPending = ref(false)
    const error = ref<Error | null>(null)
    async function mutate(args: Parameters<typeof callRenameUser>[0]) {
        isPending.value = true; error.value = null
        try { return await callRenameUser(args) }
        catch (e) { error.value = e as Error; throw e }
        finally { isPending.value = false }
    }
    return { mutate, isPending, error }
}

export function useWhoami() {
    const isPending = ref(false)
    const error = ref<Error | null>(null)
    async function mutate() {
        isPending.value = true; error.value = null
        try { return await callWhoami() }
        catch (e) { error.value = e as Error; throw e }
        finally { isPending.value = false }
    }
    return { mutate, isPending, error }
}

export type { ContextState } from '@mizan/base'
export { configure, initSession, MizanError } from '@mizan/base'
