You’re going to need to add some more information that I don’t see in any of your code, but I found a function in WP that will do this https://codex.wordpress.org/Function_Reference/wp_unique_post_slug
$data['post_name'] = wp_unique_post_slug(sanitize_title( $data['post_title'] ), $post_id, $post_status, $post_type, $post_parent);
It appears that all of the arguments for this function are required and there’s not much information on using it.