Need help with creating sensor based on sequence of events

Hello,

Assuming that I have existing binary sensors A, B and C, I am trying to create a custom binary sensor that is triggered on and off by a specific sequence of events happening on sensors A and B:
Turn custom Sensor on when: Starting with Sensors A, B and C all off, when Sensor A or Sensor B are switched ON, then sensor C is switched ON within 10 seconds.
Turn custom Sensor off when: Starting with Sensors A, B and C all off and Custom sensor is ON, when Sensor A or Sensor B are switched ON.

I am really struggling to even understand how I could code this using template integration and I would appreciate any help on this.

Thanks,
Maxim