Problems #dependecy of ctools with #ajax in form api

Hello, I have two fields in a form that alternate as I select a third. This function is made with #dependency of ctools. I am trying to do a validation with #ajax so that the page is not read every time it is pressed. The user presses submit but on the screen that returns #ajax the functionality of #dependency of ctools is lost and the two fields can be seen at the same time. I have already tried many things but I am stuck, if someone has had the same problem I would appreciate the help. This is the code of the three fields.

Each field has a #dependency attribute that contains an array with some values. Depends on what the user selects in a select field, one or the other appears.

Thanks for everything and greetings.

Drupal version: