this line is repeated multiple times, which increases the complexity of the code quite a bit. Adding 6 ifs (some nested) to this function is overcomplicating the thing.

Maybe creating a helper that deals with those conditions inside keeping the same structure that previously it had, or restructure the whole function in another way that keeps it readable