def choose_middlewares(data: DataModel):
    chosen_middlewares_names, chosen_middlewares_ids = data.get_selected_middleware_lists()
    widget = ReorderBullet(_("3. Choose middlewares (optional)."),
                           choices=chosen_middlewares_names,
                           choices_id=chosen_middlewares_ids)

    middlewares_names, middlewares_ids = data.get_middleware_lists()
    list_widget: Optional[KeyValueBullet]
    if middlewares_ids:
        list_widget = KeyValueBullet(prompt=_("Choose a middleware to add."),
                                     choices=middlewares_names,
                                     choices_id=middlewares_ids)
    else:
        list_widget = None

    while True:
        print()
        chosen_middlewares_names, chosen_middlewares_ids, action = widget.launch()
        if action == 'add':
            print()
            if not list_widget:
                print_wrapped(_("No installed middleware is detected, press ENTER to go back."))
                input()
            else:
                add_middleware_name, add_middleware_id = list_widget.launch()
                add_middleware_instance = input(_("Instance name to use with {middleware_name}: [{default_instance}]")
                                                .format(middleware_name=add_middleware_name,
                                                        default_instance=_("default instance"))
                                                + " ").strip()
                if add_middleware_instance:
                    add_middleware_id += "#" + add_middleware_instance

                display_name = data.get_instance_display_name(add_middleware_id)
                if add_middleware_id in widget.choices_id:
                    print_wrapped(_("{instance_name} ({instance_id}) is already enabled. "
                                    "Please try another one.")
                                  .format(instance_name=display_name, instance_id=add_middleware_id))
                else:
                    widget.choices.insert(-2, display_name)
                    widget.choices_id.insert(-2, add_middleware_id)
        else:  # action == 'submit'
            break
    data.config['middlewares'] = chosen_middlewares_ids