import { ref, computed } from 'vue' import type { RegisterIdentityRequest, RegisterIdentityResponse, Identity, IdentityListResponse } from '../types/identitySchema' const API_BASE = '/api' export function useIdentityApi() { const loading = ref(false) const error = ref(null) const identities = ref([]) const total = ref(0) // Register new identity const registerIdentity = async (data: RegisterIdentityRequest): Promise => { loading.value = true error.value = null try { const response = await fetch(`${API_BASE}/identities`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-KArtSell-User': 'current-user', // Will be replaced with actual auth token 'X-KArtSell-Role': 'Admin', }, body: JSON.stringify(data), }) if (!response.ok) { const errorData = await response.json().catch(() => ({ message: 'Unknown error' })) throw new Error(errorData.message || `HTTP ${response.status}`) } const result = await response.json() return result } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to register identity' console.error('Register identity error:', err) return null } finally { loading.value = false } } // Get identity details const getIdentity = async (identityId: string): Promise => { loading.value = true error.value = null try { const response = await fetch(`${API_BASE}/identities/${identityId}`, { headers: { 'X-KArtSell-User': 'current-user', 'X-KArtSell-Role': 'Admin', }, }) if (!response.ok) throw new Error(`HTTP ${response.status}`) const data = await response.json() return data } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to fetch identity' return null } finally { loading.value = false } } // List identities (mock for now, replace with actual API call) const listIdentities = async (page = 1, pageSize = 20): Promise => { loading.value = true error.value = null try { // TODO: Replace with actual API call when endpoint is available // For now, mock data identities.value = [] total.value = 0 } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to fetch identities' } finally { loading.value = false } } // Delete identity const deleteIdentity = async (identityId: string): Promise => { loading.value = true error.value = null try { const response = await fetch(`${API_BASE}/identities/${identityId}`, { method: 'DELETE', headers: { 'X-KArtSell-User': 'current-user', 'X-KArtSell-Role': 'Admin', }, }) if (!response.ok) throw new Error(`HTTP ${response.status}`) return true } catch (err) { error.value = err instanceof Error ? err.message : 'Failed to delete identity' return false } finally { loading.value = false } } return { // State loading, error, identities, total, // Computed hasError: computed(() => error.value !== null), isLoading: computed(() => loading.value), // Methods registerIdentity, getIdentity, listIdentities, deleteIdentity, } }