Joseph and Mary both had to go through alot before Jesus was born and afterwards, in the path chosen for them by God. In that, for Joseph, he wasn't always sure what to do, though being both a righteous and good man, he wanted to act in a way of both love to God and to his neighbor.
God helped Joseph, of course, to think clearly. I think of thinking clearly as in thinking and then living according to the will of God in any given situation.
There are times in my life where my thinking can be muddled by my sin, or temptation to sin, or less than good thoughts. Or muddled by disparaging, discouraging thoughts. Often less than noble thoughts. Thoughts that don't line up with what we read of here.
What do we need to do? In the midst of it all we need to be in prayer, as Joseph was. We need to be seeking to meditate on God's truth, which again, Joseph was surely doing. Then we can be open to the revelation of God to our hearts and minds of his truth by the Spirit.
Clear thinking in the will of God can come in the midst of a battleground in our thought world, where for awhile the thought that won't last has the upper hand, but is cast down, for the obedience of Christ. Good, clear thinking has ultimately to do with obeying and being in the will of God. Joseph is a good model of this for us, in Scripture.