Tinder Search should avoid starting a network request for every keystroke. Implement a debounce simulator that records when a callback would execute after a sequence of timed calls.
The function must support trailing execution, optional leading execution, cancellation, and immediate flushing. A call event contains a timestamp and an argument list. Events are provided in nondecreasing timestamp order. The simulated timer fires at last_call_time + wait, unless cancellation or flushing changes the pending work.
Implement debounce(events, wait, leading, trailing). events is a list of objects with time, type, and optionally args. type is one of call, cancel, or flush. wait is a nonnegative integer. Return a list of objects in execution order, each containing time and args for one callback invocation.
For a call, trailing mode invokes the callback with the latest arguments after no call has occurred for wait time units. Leading mode invokes immediately at the beginning of each burst. A burst ends when its timer fires. cancel discards pending work and ends the current burst. flush immediately invokes pending trailing work at the event timestamp.
def debounce(events, wait, leading, trailing):