-- Constants

local FADE_IN_TWEEN = TweenInfo.new(1, Enum.EasingStyle.Linear)
local FADE_OUT_TWEEN = TweenInfo.new(1, Enum.EasingStyle.Linear)

local MusicPlayer = {}

function MusicPlayer:TransitionSoundVolumes(sounds: {Sound}, targetVolume: number, tweenInfo: TweenInfo)
	for _, sound in sounds do
		local tween = tweenService:Create(sound, tweenInfo, {Volume = targetVolume})
		tween:Play()
		
		if targetVolume > 0 and not sound.IsPlaying then
			sound:Play()
		elseif targetVolume == 0 and sound.IsPlaying then
			tween.Completed:Connect(function(playbackState)
				if playbackState == Enum.PlaybackState.Completed and targetVolume == 0 then
					sound:Stop()
				end
			end)
		end
	end
end

function MusicPlayer:FadeSoundsIn(sounds: {Sound})
	self:TransitionSoundVolumes(sounds, 0.5, FADE_IN_TWEEN)
end

function MusicPlayer:FadeSoundsOut(sounds: {Sound})
	self:TransitionSoundVolumes(sounds, 0, FADE_OUT_TWEEN)
end

return MusicPlayer
