const ERROR_MESSAGES: Record<string, string> = {
	VISIBILITY_LIMIT_REACHED: "You've reached our plan's AI Visibility limit",
	PAGE_VIEW_CAPTURE_UNSUPPORTED_PLATFORM: "Page-view capture isn't available for this Mintall account.",
};

const FALLBACK_MESSAGE = 'Something went wrong. Please try again.';

export function getApiErrorCode(error: unknown): string | null {
	if (error && typeof error === 'object' && typeof (error as { error?: unknown }).error === 'string') {
		return (error as { error: string }).error;
	}
	return null;
}

export function isVisibilityLimitReached(error: unknown): boolean {
	return getApiErrorCode(error) === 'VISIBILITY_LIMIT_REACHED';
}

export function getApiErrorMessage(error: unknown): string {
	const code = getApiErrorCode(error);
	const message = code ? ERROR_MESSAGES[code] : undefined;
	return message ?? FALLBACK_MESSAGE;
}
