1. If the court finds the defendant does not pose a danger to the public peace and safety, is otherwise qualified for pretrial release, and is willing to cooperate with treatment, the court shall order, as a condition of pretrial release, that the defendant obtain mental health treatment designed to restore the defendant to competency.
 2. If the court finds by clear and convincing evidence that the defendant poses a danger to the public peace or safety, or that the defendant is otherwise not qualified for pretrial release, or the defendant refuses to cooperate with treatment, the court shall commit the defendant to an appropriate inpatient treatment facility as provided in paragraph “a” or “b”. The defendant shall receive mental health treatment designed to restore the defendant to competency.

 a. A defendant who poses a danger to the public peace or safety, or who is otherwise not qualified for pretrial release, shall be committed as a safekeeper to the custody of the director of the department of corrections at the Iowa medical and classification center, or other appropriate treatment facility as designated by the director, for treatment designed to restore the defendant to competency. The costs of the treatment pursuant to this paragraph shall be borne by the department of corrections.
 b. A defendant who does not pose a danger to the public peace or safety, but is otherwise being held in custody, or who refuses to cooperate with treatment, shall be committed to the custody of the director of human services at a department of human services facility for treatment designed to restore the defendant to competency. The costs of the treatment pursuant to this paragraph shall be borne by the department of human services.
 3. A defendant ordered to obtain treatment or committed to a facility under this section may refuse treatment by chemotherapy or other somatic treatment. The defendant’s right to refuse chemotherapy treatment or other somatic treatment shall not apply if, in the judgment of the director or the director’s designee of the facility where the defendant has been committed, such treatment is necessary to preserve the life of the defendant or to appropriately control behavior of the defendant which is likely to result in physical injury to the defendant or others. If in the judgment of the director of the facility or the director’s designee where the defendant has been committed, chemotherapy or other somatic treatments are necessary and appropriate to restore the defendant to competency and the defendant refuses to consent to the use of these treatment modalities, the director of the facility or the director’s designee shall request from the district court which ordered the commitment of the defendant an order authorizing treatment by chemotherapy or other somatic treatments.