let mousedownTarget: EventTarget | null

window.addEventListener('mousedown', ({ target }) => {
  mousedownTarget = target
})

export default function ifClick(fn: (...args: unknown[]) => unknown) {
  return function (...args: unknown[]) {
    if (args[0] instanceof Event || args[0] instanceof window.jQuery.Event) {
      const event = args[0]
      const target = (event as Event).target

      if (target === mousedownTarget) {
        fn.apply(target, args)
      }
    }
  }
}
