--[[
	Returns the closest player to specified position, along with their distance.
]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local TableUtils = require(ReplicatedStorage.Dependencies.LuaUtils.TableUtils)
local MonsterManager = require(ServerStorage.Source.Managers.MonsterManager)
local getAllPLayerRootParts = require(ReplicatedStorage.Source.Common.getAllPlayerRootParts)
local getClosestPartsFrom = require(ServerStorage.Source.Common.getClosestPartsFrom)

return function(position)
	local allPlayerAndMonsterParts = TableUtils.append(getAllPLayerRootParts(), MonsterManager.getAllMonsterParts())

	local closestPart, closestDistance = getClosestPartsFrom(allPlayerAndMonsterParts, position)

	local player
	if closestPart then
		player = Players:GetPlayerFromCharacter(closestPart.Parent)
	end

	return player, closestDistance
end
