I don't know the answer
This varies from state to state. Some states hold primaries, some hold caucuses, and some hold state conventions. Some states may reserve delegate slots for locally elected officials. Those are the so-called "superdelegates." Also, the parties have some of their own rules and procedures. Even in the same state, the different parties may select convention delegates differently. So you see it varies across states and across parties.