"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.wrapFunction = exports.scrollIntoView = exports.safeInvoke = exports.normalizeSize = exports.mergeRefs = exports.isClickable = exports.createProxyProps = exports.createFocusManager = exports.EventEmitter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _scrollIntoViewIfNeeded = _interopRequireDefault(require("scroll-into-view-if-needed"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

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);
  }
};
exports.safeInvoke = safeInvoke;
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);
};
exports.wrapFunction = wrapFunction;
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;
};
exports.isClickable = isClickable;
const createProxyProps = cb => {
  const ProxyProps = props => cb(props);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(ProxyProps, {});
};
exports.createProxyProps = createProxyProps;
const scrollIntoView = (target, options) => (0, _scrollIntoViewIfNeeded.default)(target, {
  block: 'center',
  behavior: 'smooth',
  ...options
});
exports.scrollIntoView = scrollIntoView;
class EventEmitter {
  constructor() {
    (0, _defineProperty2.default)(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);
        }
      });
    }
  }
}
exports.EventEmitter = EventEmitter;
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
  };
};
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
  };
};
exports.createFocusManager = createFocusManager;
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;
      }
    });
  };
};
exports.mergeRefs = mergeRefs;
const normalizeSize = value => String(Number(value)) === String(value) ? `${value}px` : value;
exports.normalizeSize = normalizeSize;