I can't say exactly what caused it, because I'm not you, but from what I can see, and what you've told me, what's caused it is unresolved trauma in your life mixed with self destructive thoughts and if you've been suicidal for a while, that can cause your Survival instinct to kick in and try and fight the suicidal thoughts, thus sending you into a panic because your mind thinks it's in danger. Does that make sense?
As for how to fix it?
There's no magic cure, sadly.
Mental Health Professionals will tell you that meds, therapy, etc… will just cure your suffering, but there's so much more to it, but ultimately, it starts with you.
If you want to get better, if you really truly want to enter recovery then that's the first step, because that is where it starts— the wanting. Once you want to get better, you need to start doing a lot of self exploration and figure out what you need and how to help yourself find peace and tranquility.
For some, that may mean meds and therapy, for others, it means moving and starting over— a fresh start and for others, it may be as simple as getting good sleep and eating right. At the end of the day it's up to you.
My advice? If you're at the end of your rope and got nothing left to lose or live for, then take a risk and do something. Move, get a fresh start, because when you've got nothing left to live for then what else do you have to lose? Taking a risk and starting over to try and enter recovery could only benefit you in the end when you've got nothing left.