import { type CallErrorType, type CallParameters, type CallReturnType, call, } from '../actions/call.js' import type { Config } from '../createConfig.js' import type { ScopeKeyParameter } from '../types/properties.js' import type { QueryOptions, QueryParameter } from '../types/query.js' import type { Compute, ExactPartial } from '../types/utils.js' import { filterQueryOptions } from './utils.js' export type CallOptions = Compute< ExactPartial> & ScopeKeyParameter > & QueryParameter< CallQueryFnData, CallErrorType, selectData, CallQueryKey > export function callQueryOptions( config: config, options: CallOptions = {}, ): CallQueryOptions { return { ...options.query, queryFn: async (context) => { const [, { scopeKey: _, ...parameters }] = context.queryKey const data = await call(config, { ...parameters, } as CallParameters) return data ?? null }, queryKey: callQueryKey(options), } } export type CallQueryFnData = CallReturnType export type CallData = CallQueryFnData export function callQueryKey( options: Compute< ExactPartial> & ScopeKeyParameter > = {}, ) { return ['call', filterQueryOptions(options)] as const } export type CallQueryKey = ReturnType< typeof callQueryKey > export type CallQueryOptions< config extends Config, selectData = CallData, > = QueryOptions< CallQueryFnData, CallErrorType, selectData, CallQueryKey >