What must be established first when assessing leaving the mosque?

Bab al-Iʿtikaf: Rules, Restrictions, Invalidation and Vows