import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed';
import { jsx as _jsx } from "react/jsx-runtime";
export const safeInvoke = function (fn) {
  if (typeof fn === 'function') {
    for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }
    fn(...args);
  }
};
export const wrapFunction = (fn, wrapper) => function () {
  for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
    args[_key2] = arguments[_key2];
  }
  safeInvoke(fn, ...args);
  safeInvoke(wrapper, ...args);
};
export const isClickable = e => {
  const {
    target,
    currentTarget
  } = e;
  if (false === (target instanceof HTMLElement && currentTarget.contains(target))) {
    return true;
  }
  if (target instanceof HTMLElement && target.hasAttribute('disabled')) {
    return false;
  }
  let element = target;
  while (element && element !== currentTarget) {
    if (element instanceof HTMLElement) {
      switch (element.nodeName) {
        case 'A':
        case 'INPUT':
        case 'BUTTON':
        case 'LABEL':
          return true;
      }
      if (element.getAttribute('role') === 'button') {
        return true;
      }
      element = element.parentNode;
    } else {
      return true;
    }
  }
  return false;
};
export const createProxyProps = cb => {
  const ProxyProps = props => cb(props);
  return /*#__PURE__*/_jsx(ProxyProps, {});
};
export const scrollIntoView = (target, options) => scrollIntoViewIfNeeded(target, {
  block: 'center',
  behavior: 'smooth',
  ...options
});
export class EventEmitter {
  constructor() {
    _defineProperty(this, "listeners", {});
  }
  on(type, handler) {
    if (this.listeners[type] === undefined) {
      this.listeners[type] = [];
    }
    this.listeners[type].push(handler);
  }
  emit(type) {
    for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
      args[_key3 - 1] = arguments[_key3];
    }
    const handlers = this.listeners[type];
    if (handlers) {
      handlers.forEach(handler => {
        if (typeof handler === 'function') {
          handler(...args);
        }
      });
    }
  }
}
const createStrategy = () => {
  const getNext = (container, element) => {
    const elements = container.children;
    const index = Array.prototype.indexOf.call(elements, element);
    if (index !== -1) {
      return elements[index + 1];
    }
    return undefined;
  };
  const getPrev = (container, element) => {
    const elements = container.children;
    const index = Array.prototype.indexOf.call(elements, element);
    if (index !== -1) {
      return elements[index - 1];
    }
    return undefined;
  };
  const getFirst = container => container.firstElementChild;
  const getLast = container => container.lastElementChild;
  return {
    getNext,
    getPrev,
    getFirst,
    getLast
  };
};
export const createFocusManager = function (containerRef) {
  let isFitForFocus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : () => true;
  let strategy = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : createStrategy();
  const tryFocus = node => {
    if (isFitForFocus(node) && node instanceof HTMLElement) {
      node.focus();
      return true;
    }
    return false;
  };
  const focusNext = function () {
    let element = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document.activeElement;
    let attempt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    if (!containerRef.current) {
      return false;
    }
    const next = strategy.getNext(containerRef.current, element);
    if (next) {
      if (tryFocus(next) || focusNext(next, attempt)) {
        return true;
      }
    }
    return focusFirst(attempt + 1);
  };
  const focusPrev = function () {
    let element = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document.activeElement;
    let attempt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    if (!containerRef.current) {
      return false;
    }
    const prev = strategy.getPrev(containerRef.current, element);
    if (prev) {
      if (tryFocus(prev) || focusPrev(prev, attempt)) {
        return true;
      }
    }
    return focusLast(attempt + 1);
  };
  const focusFirst = function () {
    let attempt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
    if (attempt > 1) {
      return false;
    }
    if (!containerRef.current) {
      return false;
    }
    const next = strategy.getFirst(containerRef.current);
    if (next) {
      if (tryFocus(next) || focusNext(next, attempt)) {
        return true;
      }
    }
    return false;
  };
  const focusLast = function () {
    let attempt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
    if (attempt > 1) {
      return false;
    }
    if (!containerRef.current) {
      return false;
    }
    const prev = strategy.getLast(containerRef.current);
    if (prev) {
      if (tryFocus(prev) || focusPrev(prev, attempt)) {
        return true;
      }
    }
    return false;
  };
  return {
    focusNext,
    focusPrev,
    focusFirst,
    focusLast
  };
};
export const mergeRefs = function () {
  for (var _len4 = arguments.length, refs = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
    refs[_key4] = arguments[_key4];
  }
  return value => {
    refs.forEach(ref => {
      if (typeof ref === 'function') {
        ref(value);
      } else if (ref) {
        ref.current = value;
      }
    });
  };
};
export const normalizeSize = value => String(Number(value)) === String(value) ? `${value}px` : value;