import { ref } from 'vue' import { useCwa } from '#cwa/composables/cwa' import type { PageDataMetadataResource } from '#cwa/storage/stores/api-documentation/state' import type { CwaResource } from '#cwa/resources/resource-utils' import { fetchAllMembers } from './fetchAllMembers' export const useParentPageDataLoader = () => { const $cwa = useCwa() const currentTypeRequestId = ref(0) const currentInstanceRequestId = ref(0) const dataTypes = ref() const dataInstances = ref() function fqcnToEntrypointKey(fqcn: string): string | undefined { const className = fqcn.split('\\').pop() if (!className) return undefined return className.charAt(0).toLowerCase() + className.slice(1) } async function loadDataTypes() { const thisRequestId = currentTypeRequestId.value + 1 currentTypeRequestId.value = thisRequestId const docs = await $cwa.getApiDocumentation() if (thisRequestId !== currentTypeRequestId.value) return const allMetadata = docs?.pageDataMetadata?.member if (!allMetadata) { dataTypes.value = [] return } dataTypes.value = allMetadata.filter(d => !d.resourceClass.endsWith('\\AbstractPageData')) } async function loadDataInstances(entrypointKey: string) { const thisRequestId = currentInstanceRequestId.value + 1 currentInstanceRequestId.value = thisRequestId const docs = await $cwa.getApiDocumentation() const endpoint = docs?.entrypoint?.[entrypointKey] if (!endpoint) { if (thisRequestId === currentInstanceRequestId.value) { dataInstances.value = [] } return } const instances = await fetchAllMembers($cwa, endpoint) if (thisRequestId === currentInstanceRequestId.value) { dataInstances.value = instances } } return { loadDataTypes, loadDataInstances, dataTypes, dataInstances, fqcnToEntrypointKey } }