import { globalObject } from '@skbkontur/global-object';
var calculatePriority = function calculatePriority(priority) {
  if (typeof priority === 'string') {
    return componentPriorities[priority];
  }
  return priority;
};
export var componentPriorities = {
  MobilePopup: 9000,
  Toast: 10000,
  GlobalLoader: 10,
  Sidepage: 9,
  Modal: 9,
  Sticky: 7,
  PopupMenu: 5,
  Popup: 3
};
var priorityStep = 1000;
var globalWithRetailUiZIndexes = globalObject;
var getZIndexes = function getZIndexes() {
  return globalWithRetailUiZIndexes.__RetailUiZIndexes || (globalWithRetailUiZIndexes.__RetailUiZIndexes = [0]);
};
var getIndexPriority = function getIndexPriority(zIndex) {
  return Math.trunc(zIndex / priorityStep);
};
var getMaxAllowedValue = function getMaxAllowedValue(priority) {
  return (priority + 1) * priorityStep - 1;
};
export var upperBorder = getMaxAllowedValue(Math.max.apply(Math, Object.values(componentPriorities)));
export function incrementZIndex(priority, delta) {
  var calculatedPriority = calculatePriority(priority);
  var maxAllowedValue = getMaxAllowedValue(calculatedPriority);
  var zIndexes = getZIndexes();
  var prevIndexId = zIndexes.length - 1;
  while (zIndexes[prevIndexId] > maxAllowedValue && prevIndexId > 0) {
    prevIndexId--;
  }
  var zIndex = getIndexPriority(zIndexes[prevIndexId]) === calculatedPriority ? Math.min(zIndexes[prevIndexId] + delta, maxAllowedValue) : calculatedPriority * priorityStep;
  zIndexes.push(zIndex);
  zIndexes.sort(function (a, b) {
    return a - b;
  });
  return zIndex;
}
export function removeZIndex(zIndex) {
  var zIndexes = getZIndexes();
  var i = zIndexes.indexOf(zIndex);
  if (i !== -1) {
    zIndexes.splice(i, 1);
  }
}