interface ResolveTaskProgressStateArgs {
	notificationMessage: string | null | undefined
	notificationProgressPct: number | null | undefined
	taskMessage: string | null | undefined
	taskProgressPct: number | null | undefined
	defaultMessage: string
}

export function resolveTaskProgressState({
	notificationMessage,
	notificationProgressPct,
	taskMessage,
	taskProgressPct,
	defaultMessage,
}: ResolveTaskProgressStateArgs): {
	progressPct: number
	message: string
} {
	const notificationPct = notificationProgressPct ?? null
	const statusPct = taskProgressPct ?? null

	if (notificationPct !== null && statusPct !== null) {
		if (statusPct > notificationPct) {
			return {
				progressPct: statusPct,
				message: taskMessage ?? notificationMessage ?? defaultMessage,
			}
		}

		return {
			progressPct: notificationPct,
			message: notificationMessage ?? taskMessage ?? defaultMessage,
		}
	}

	if (notificationPct !== null) {
		return {
			progressPct: notificationPct,
			message: notificationMessage ?? taskMessage ?? defaultMessage,
		}
	}

	if (statusPct !== null) {
		return {
			progressPct: statusPct,
			message: taskMessage ?? notificationMessage ?? defaultMessage,
		}
	}

	return {
		progressPct: 0,
		message: notificationMessage ?? taskMessage ?? defaultMessage,
	}
}
