function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { scrollIntoView } from '../_utils/scrollIntoView';
function noop() {}
var MOUSE_ENTER = 'mouseenter';
var MOUSE_LEAVE = 'mouseleave';
var menuItemsWrapperAttributeName = 'data-core-menuimperative-items-wrapper';
export var menuItemsWrapperAttribute = _defineProperty({}, menuItemsWrapperAttributeName, 'true');
var isItem = function isItem(element) {
  return element === null || element === void 0 ? void 0 : element.hasAttribute('data-value');
};
function isGroup(el) {
  return JSON.parse(el.dataset.group || 'false');
}
var isWrapper = function isWrapper(element) {
  return (element === null || element === void 0 ? void 0 : element.getAttribute(menuItemsWrapperAttributeName)) === 'true';
};
function getFirstItem(menu) {
  var firstItem = menu === null || menu === void 0 ? void 0 : menu.querySelector('[data-value]:first-child');
  if (firstItem && isGroup(firstItem)) {
    return getNextItem(firstItem);
  }
  return firstItem;
}
function getPrevItem(item) {
  var _item$parentElement2;
  if (item.previousElementSibling) {
    // LocationFilter in data-table has div-wrapper around some items
    if (isWrapper(item.previousElementSibling)) {
      return getLastItem(item.previousElementSibling);
    }
    return item.previousElementSibling;
  }
  if (isWrapper(item === null || item === void 0 ? void 0 : item.parentElement)) {
    var _item$parentElement;
    return item === null || item === void 0 ? void 0 : (_item$parentElement = item.parentElement) === null || _item$parentElement === void 0 ? void 0 : _item$parentElement.previousElementSibling;
  }
  if ((item === null || item === void 0 ? void 0 : (_item$parentElement2 = item.parentElement) === null || _item$parentElement2 === void 0 ? void 0 : _item$parentElement2.getAttribute('data-droppable')) === 'true') {
    var _item$parentElement3, _item$parentElement3$;
    var prevDroppableItems = item === null || item === void 0 ? void 0 : (_item$parentElement3 = item.parentElement) === null || _item$parentElement3 === void 0 ? void 0 : (_item$parentElement3$ = _item$parentElement3.previousElementSibling) === null || _item$parentElement3$ === void 0 ? void 0 : _item$parentElement3$.querySelectorAll('[data-group="false"]');
    return prevDroppableItems !== null && prevDroppableItems !== void 0 && prevDroppableItems.length ? prevDroppableItems[prevDroppableItems.length - 1] : null;
  }
  return null;
}
function getNextItem(item) {
  var _item$parentElement5;
  if (item.nextElementSibling) {
    // LocationFilter in data-table has div-wrapper around some items
    if (isWrapper(item.nextElementSibling)) {
      return getFirstItem(item.previousElementSibling);
    }
    return item.nextElementSibling;
  }
  if (isWrapper(item === null || item === void 0 ? void 0 : item.parentElement)) {
    var _item$parentElement4;
    return item === null || item === void 0 ? void 0 : (_item$parentElement4 = item.parentElement) === null || _item$parentElement4 === void 0 ? void 0 : _item$parentElement4.nextElementSibling;
  }
  if ((item === null || item === void 0 ? void 0 : (_item$parentElement5 = item.parentElement) === null || _item$parentElement5 === void 0 ? void 0 : _item$parentElement5.getAttribute('data-droppable')) === 'true') {
    var _item$parentElement6, _item$parentElement6$;
    return item === null || item === void 0 ? void 0 : (_item$parentElement6 = item.parentElement) === null || _item$parentElement6 === void 0 ? void 0 : (_item$parentElement6$ = _item$parentElement6.nextElementSibling) === null || _item$parentElement6$ === void 0 ? void 0 : _item$parentElement6$.querySelector('[data-group="false"]');
  }
  return null;
}
function getLastItem(menu) {
  var items = menu === null || menu === void 0 ? void 0 : menu.querySelectorAll('[data-value]');
  var lastItem = items === null || items === void 0 ? void 0 : items[items.length - 1];
  if (lastItem && isGroup(lastItem)) {
    return getPrevItem(lastItem);
  }
  return lastItem;
}
function updateHighlightedAttribute(item, highlight) {
  item === null || item === void 0 ? void 0 : item.setAttribute('data-highlighted', highlight ? 'true' : 'false');
  item === null || item === void 0 ? void 0 : item.dispatchEvent(new MouseEvent(highlight ? MOUSE_ENTER : MOUSE_LEAVE));
}
export function createSensors(ref, circular, onChangeActiveDescendant) {
  var highlightedItem = null;
  var dragAndDropApi = null;
  var currentDrag = null;
  var onSelect = noop;
  function select(event) {
    if (ref.current && highlightedItem) {
      var _highlightedItem, _highlightedItem2, _highlightedItem3, _highlightedItem4, _highlightedItem5, _onSelect;
      var disabled = JSON.parse(((_highlightedItem = highlightedItem) === null || _highlightedItem === void 0 ? void 0 : _highlightedItem.dataset.disabled) || 'false');
      if (disabled) return;
      var item = JSON.parse(((_highlightedItem2 = highlightedItem) === null || _highlightedItem2 === void 0 ? void 0 : _highlightedItem2.dataset.value) || 'null');
      var selected = JSON.parse(((_highlightedItem3 = highlightedItem) === null || _highlightedItem3 === void 0 ? void 0 : _highlightedItem3.dataset.selected) || 'false');
      var restoreFocus = (_highlightedItem4 = highlightedItem) !== null && _highlightedItem4 !== void 0 && _highlightedItem4.dataset.restorefocus ? JSON.parse((_highlightedItem5 = highlightedItem) === null || _highlightedItem5 === void 0 ? void 0 : _highlightedItem5.dataset.restorefocus) : undefined;
      if (highlightedItem.tagName === 'A') {
        highlightedItem.click();
      }
      (_onSelect = onSelect) === null || _onSelect === void 0 ? void 0 : _onSelect(_objectSpread({
        event: event,
        item: item,
        group: false,
        action: selected ? 'unselected' : 'selected'
      }, restoreFocus === undefined ? {} : {
        restoreFocus: restoreFocus
      }));
    }
  }
  function highlightItem(item) {
    var shouldScroll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
    // performance improvement, highlightItem is called onMouseMove for Item
    if (item === highlightedItem) {
      return;
    }
    if (item) {
      var _item$getAttribute;
      updateHighlightedAttribute(highlightedItem, false);

      // TODO add localized screenreader instructions for the item
      updateHighlightedAttribute(item, true);
      var _id = (_item$getAttribute = item.getAttribute('id')) !== null && _item$getAttribute !== void 0 ? _item$getAttribute : undefined;
      onChangeActiveDescendant(_id, ref.current);
      highlightedItem = item;
      if (shouldScroll) {
        scrollIntoView(-1, item);
      }
    } else {
      onChangeActiveDescendant('', ref.current);
    }
  }
  function getPrevHighlightableItem(currentItem) {
    var prev = getPrevItem(currentItem);
    if (prev) {
      if (isItem(prev)) {
        if (isGroup(prev)) {
          return getPrevHighlightableItem(prev);
        } else {
          return prev;
        }
      }
    } else if (circular) {
      return getLastItem(ref.current);
    }
    return prev;
  }
  function getNextHighlightableItem(currentItem) {
    var next = getNextItem(currentItem);
    if (next) {
      if (isItem(next)) {
        if (isGroup(next)) {
          return getNextHighlightableItem(next);
        }
        return next;
      }
    } else if (circular) {
      return getFirstItem(ref.current);
    }
  }
  function highlightFirstItem() {
    var shouldScroll = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
    highlightItem(getFirstItem(ref.current), shouldScroll);
  }
  function highlightLastItem() {
    var shouldScroll = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
    highlightItem(getLastItem(ref.current), shouldScroll);
  }
  function highlightPrevItem() {
    var _ref$current;
    if (highlightedItem && (_ref$current = ref.current) !== null && _ref$current !== void 0 && _ref$current.contains(highlightedItem)) {
      var prev = getPrevHighlightableItem(highlightedItem);
      if (prev) {
        highlightItem(prev);
      }
    } else {
      highlightLastItem();
    }
  }
  function highlightNextItem() {
    var _ref$current2;
    if (highlightedItem && (_ref$current2 = ref.current) !== null && _ref$current2 !== void 0 && _ref$current2.contains(highlightedItem)) {
      var next = getNextHighlightableItem(highlightedItem);
      if (next) {
        highlightItem(next);
      }
    } else {
      highlightFirstItem();
    }
  }
  return {
    handleKeyDown: function handleKeyDown(event) {
      if (ref.current) {
        var _currentDrag4;
        if (event.key === 'ArrowUp' || event.key === 'Up') {
          var _currentDrag;
          event.preventDefault();
          (_currentDrag = currentDrag) !== null && _currentDrag !== void 0 && _currentDrag.isActive() ? currentDrag.moveUp() : highlightPrevItem();
        } else if (event.key === 'ArrowDown' || event.key === 'Down') {
          var _currentDrag2;
          event.preventDefault();
          (_currentDrag2 = currentDrag) !== null && _currentDrag2 !== void 0 && _currentDrag2.isActive() ? currentDrag.moveDown() : highlightNextItem();
        } else if (event.key === 'Enter') {
          var _currentDrag3;
          event.preventDefault();
          if (event.ctrlKey || event.metaKey) {
            var _highlightedItem6;
            var draggableId = (_highlightedItem6 = highlightedItem) === null || _highlightedItem6 === void 0 ? void 0 : _highlightedItem6.getAttribute('data-rbd-draggable-id');
            if (draggableId) {
              var _dragAndDropApi;
              var preDrag = (_dragAndDropApi = dragAndDropApi) === null || _dragAndDropApi === void 0 ? void 0 : _dragAndDropApi.tryGetLock(draggableId);
              if (preDrag) {
                currentDrag = preDrag.snapLift();
              }
            }
          } else if ((_currentDrag3 = currentDrag) !== null && _currentDrag3 !== void 0 && _currentDrag3.isActive()) {
            currentDrag.drop();
          } else {
            select(event);
          }
        } else if (event.key === 'Escape' && (_currentDrag4 = currentDrag) !== null && _currentDrag4 !== void 0 && _currentDrag4.isActive()) {
          currentDrag.cancel();
        }
      }
    },
    handleItemHover: function handleItemHover(el) {
      highlightItem(el, false);
    },
    useKeyboardSensor: function useKeyboardSensor(api) {
      dragAndDropApi = api;
    },
    updateSelectCallback: function updateSelectCallback(callback) {
      onSelect = callback;
    },
    highlight: highlightItem,
    highlighted: function highlighted() {
      var _highlightedItem7;
      return JSON.parse(((_highlightedItem7 = highlightedItem) === null || _highlightedItem7 === void 0 ? void 0 : _highlightedItem7.dataset.value) || 'null');
    },
    highlightFirst: highlightFirstItem,
    highlightLast: highlightLastItem,
    rehighlightCurrent: function rehighlightCurrent() {
      var _highlightedItem8;
      // Rehighlights an item that may have lost its styling due to the re-render of the list items
      var draggableId = (_highlightedItem8 = highlightedItem) === null || _highlightedItem8 === void 0 ? void 0 : _highlightedItem8.getAttribute('data-rbd-draggable-id');
      if (draggableId) {
        var _ref$current3;
        var newItem = (_ref$current3 = ref.current) === null || _ref$current3 === void 0 ? void 0 : _ref$current3.querySelector("[data-rbd-draggable-id=\"".concat(draggableId.replace(/"/g, '\\"'), "\"]"));
        highlightItem(newItem);
      }
    },
    highlightSelected: function highlightSelected() {
      var _ref$current4;
      var shouldScroll = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      var selectedItem = (_ref$current4 = ref.current) === null || _ref$current4 === void 0 ? void 0 : _ref$current4.querySelector('[data-selected="true"]');
      highlightItem(selectedItem, shouldScroll);
    },
    highlightSuggested: function highlightSuggested() {
      var _ref$current5;
      var shouldScroll = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      var suggestedItem = (_ref$current5 = ref.current) === null || _ref$current5 === void 0 ? void 0 : _ref$current5.querySelector('[data-suggested="true"]');
      highlightItem(suggestedItem, shouldScroll);
    },
    prev: highlightPrevItem,
    next: highlightNextItem,
    select: select
  };
}
//# sourceMappingURL=sensors.js.map