// Helper to extract user-friendly error messages from API responses
export const getApiErrorMessage = (err: unknown, fallbackMessageKey: string, t: (key: string, ...args: any[]) => string): string => {
    if (err && typeof err === 'object') {
        // Check for FastAPI/DRF-style error response
        if ('response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data) {
            const errorData = err.response.data as any; // Type assertion for easier access
            if (typeof errorData.detail === 'string') {
                return errorData.detail;
            }
            if (typeof errorData.message === 'string') { // Common alternative
                return errorData.message;
            }
            // FastAPI validation errors often come as an array of objects
            if (Array.isArray(errorData.detail) && errorData.detail.length > 0) {
                const firstError = errorData.detail[0];
                if (typeof firstError.msg === 'string' && typeof firstError.type === 'string') {
                    return firstError.msg; // Simpler: just the message
                }
            }
            if (typeof errorData === 'string') { // Sometimes data itself is the error string
                return errorData;
            }
        }
        // Standard JavaScript Error object
        if (err instanceof Error && err.message) {
            return err.message;
        }
    }
    // Fallback to a translated message
    return t(fallbackMessageKey);
}; 