Read carefully all the qualifications in this article, to understand that straightforward use of that iMac as a main display through Target Display Mode has been deprecated.
Use your iMac as a display with target display mode - Apple Support
HOWEVER:
if you choose to run some version of MacOS in that iMac, another Mac's Primary display could appear in a window on the iMac using Mac Display Sharing:
For a Mac that does not already have a Physical display, use a "HDMI Headless 4K Display Emulator, US$12.99"
https://eshop.macsales.com/item/NewerTech/adp4khead/
Then share that 'emulated' display over your network to the iMac using Mac Display Sharing.
Turn Mac screen sharing on or off - Apple Support
if this is all just "word salad", post back and readers can provide more information in any area.