We don't lock people up who have cancer and force them to get treatment. Similarly we should not lock up mentally ill people. In particular we need to remember defining "illness" is not strictly a scientific enterprise. Science may categorize and delineate differences in correlations between phenomenon such as neurology, thoughts, behavior,... But it is a subjective judgment to say "this is illness" or "this is a good thing". These subjective judgments are important in guiding treatment. Without them even having cancer is nothing more than a neutral fact about the Universe and not an illness to be treated.
But the mind is a murkier sort of thing. People's ideas about how the mind should work have changed throughout history. This is why we should not cure mental illness except perhaps serious cases like Alzheimer's, otherwise treat but not cure. We would be fossilizing society's understanding of how the mind should work and would lose our adaptivity.
Chances are in the future things currently categorized as "illness" will be thought to have actually been "good differences".
If someone is violent then it may be worthwhile to criminalize that behavior, arrest the person, and detain them to protect society. BUT treatment should not be forced. It should be offered even more widely than it already is for criminals, but not forced. And parole or probation offered at times in exchange for agreeing to undergo treatment. But a person should not be directly forced into treatment. The option of just sitting in one's cell should always be there.
You either break a law or you dont. Confining people for mental illness gives the power to arrest people just because they disagree. If you don't a prove of fay marriage you are mentally ill for instance. It's a far fetch. But it lays the ground work for these interpretations. It may not happen now but ten years from now people's minds change
I used to believe that involuntary confinement for the mentally ill was justified. But government has shown that it is too often willing to abuse its power to treat, or restrain,the mentally ill and expand that power to include those who question the legitimacy of the government. Also, the psychiatric community has put itself the pocket of the governments by creating guidelines that treat nonconformity as a mental illness.
Governments should not have the power to detain people with serious mental illnesses. It is not the government's business to monitor people's mental health and it certainly is not their right to detain people have not done something wrong. Mental illness is a condition that requires medical help, not detainment.