How to do grid based building?

I know it’s possible to achieve this kind of construction using standalone sprites or inserting tiles in a new layer. What I’d like to know is how to create complex constructions like a fence for example.
Screenshot_20241010_103104_Draw Pixel Art Pro
The player could build the fence for his farm and the correct part would be automatically selected. Something like the autotiles in rpg maker. Does anyone have any ideas?