export default function ifMatches(
  selectors: string | string[],
  fn: (...args: unknown[]) => unknown,
) {
  const selector = Array.isArray(selectors) ? selectors.join(',') : selectors

  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 instanceof Element && target.matches(selector)) {
        fn.apply(target, args)
      }
    }
  }
}
