function MouseLockController.OnInputBegan(j,V,s)if s then return end if V.UserInputType==Enum.UserInputType.Keyboard then for s,l in pairs(j.boundKeys)do if l==V.KeyCode then j:OnMouseLockToggled()return end end end end function MouseLockController.DoMouseLockSwitch(l,V,s,j)if s==Enum.UserInputState.Begin then l:OnMouseLockToggled()return Enum.ContextActionResult.Sink end return Enum.ContextActionResult.Pass end function MouseLockController.BindContextActions(V)ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME,function(s,j,l)V:DoMouseLockSwitch(s,j,l)end,false,MOUSELOCK_ACTION_PRIORITY,unpack(V.boundKeys))end function MouseLockController.UnbindContextActions(V)ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)end function MouseLockController.IsMouseLocked(V)return V.enabled and V.isMouseLocked end function MouseLockController.EnableMouseLock(s,V)if V~=s.enabled then s.enabled=V if s.enabled then if FFlagPlayerScriptsBindAtPriority then s:BindContextActions()else if s.inputBeganConn then s.inputBeganConn:Disconnect()end s.inputBeganConn=UserInputService.InputBegan:Connect(function(V,j)s:OnInputBegan(V,j)end)end else if Mouse.Icon~=""then Mouse.Icon=""end if FFlagPlayerScriptsBindAtPriority then s:UnbindContextActions()else if s.inputBeganConn then s.inputBeganConn:Disconnect()end s.inputBeganConn=nil end if s.isMouseLocked then s.mouseLockToggledEvent:Fire()end s.isMouseLocked=false end end end return MouseLockController

function MouseLockController:OnInputBegan(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		for _, keyCode in pairs(self.boundKeys) do
			if keyCode == input.KeyCode then
				self:OnMouseLockToggled()
				return
			end
		end
	end
end

function MouseLockController:DoMouseLockSwitch(name, state, input)
	if state == Enum.UserInputState.Begin then 
		self:OnMouseLockToggled()
		return Enum.ContextActionResult.Sink
	end
	return Enum.ContextActionResult.Pass
end

function MouseLockController:BindContextActions()
	ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME, function(name, state, input) self:DoMouseLockSwitch(name, state, input) end, 
		false, MOUSELOCK_ACTION_PRIORITY, unpack(self.boundKeys))
end

function MouseLockController:UnbindContextActions()
		ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)
end

function MouseLockController:IsMouseLocked()
	return self.enabled and self.isMouseLocked
end

function MouseLockController:EnableMouseLock(enable)
	if enable~=self.enabled then
		
		self.enabled = enable

		if self.enabled then
			-- Enabling the mode
			if FFlagPlayerScriptsBindAtPriority then
				self:BindContextActions()
			else
				if self.inputBeganConn then
					self.inputBeganConn:Disconnect()
				end
				self.inputBeganConn = UserInputService.InputBegan:Connect(function(input, processed)
					self:OnInputBegan(input, processed)
				end)
			end
		else
			-- Disabling
			-- Restore mouse cursor
			if Mouse.Icon~="" then
				Mouse.Icon = ""
			end
			
			if FFlagPlayerScriptsBindAtPriority then
				self:UnbindContextActions()
			else
				if self.inputBeganConn then
					self.inputBeganConn:Disconnect()
				end
				self.inputBeganConn = nil
			end

			-- If the mode is disabled while being used, fire the event to toggle it off
			if self.isMouseLocked then
				self.mouseLockToggledEvent:Fire()
			end			
			
			self.isMouseLocked = false
		end		
		
	end
end

return MouseLockController
