That's the big question, the chicken or the egg. Everyone is asking that question.
Let's start by putting things in perspective. There are two types of mental health issues related to drugs.
One is the presence of comorbidities and self-medication. The most common example is alcohol use in response to anxiety disorders. A lot of people with anxiety self-medicate with alcohol. Anxiety was there first, and substance use problems came later.
The other is substance-induced disorders. For example, a lot of teenagers appear to have ADHD symptoms, but those symptoms are actually induced by cannabis use. In other words, the symptoms are triggered by substance use, not the other way around.
I would say that it's important to try to diagnose the primary disease, if there is one, and to treat that at the same time as the substance use problem. We shouldn't be doing what we used to do, which was ask patients to stop using for six months and provide care only at the end of that period. That doesn't work anymore. Addiction and the mental health problem, if there is one, must be treated together. That's called concurrent treatment, or management of comorbidity.
Your big chicken-and-egg question remains unanswered. Sometimes prolonged abstinence provides answers, as in the case of bipolar disorder or psychotic disorders.