"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _ImageItem = require("../../ImageItem");
var _Item = _interopRequireDefault(require("../../Item"));
var _VirtualList = _interopRequireWildcard(require("../VirtualList"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["index"],
  _excluded2 = ["index"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var focus = function focus(elm) {
  return _react.fireEvent.focus(elm);
};
var keyDownUp = function keyDownUp(keyCode) {
  return function (elm) {
    _react.fireEvent.keyDown(elm, {
      keyCode: keyCode
    });
    _react.fireEvent.keyUp(elm, {
      keyCode: keyCode
    });
  };
};
var pressLeftKey = keyDownUp(37);
var pressRightKey = keyDownUp(39);
var pressUpKey = keyDownUp(38);
var pressDownKey = keyDownUp(40);
var pressPageDownKey = keyDownUp(34);
describe('VirtualList useEvent with translate `scrollMode`', function () {
  var clientSize, currentFocusIndex, dataSize, handlerOnFocus, imageItemSize, items, itemSize, renderImageItem, renderItem, svgGenerator;
  beforeEach(function () {
    clientSize = {
      clientWidth: 1280,
      clientHeight: 720
    };
    currentFocusIndex = -1;
    dataSize = 200;
    imageItemSize = {
      minWidth: 300,
      minHeight: 240
    };
    items = [];
    itemSize = 60;
    handlerOnFocus = function handlerOnFocus(index) {
      return function () {
        currentFocusIndex = index;
      };
    };
    renderImageItem = function renderImageItem(_ref) {
      var index = _ref.index,
        rest = _objectWithoutProperties(_ref, _excluded);
      // eslint-disable-line enact/display-name
      var _items$index = items[index],
        name = _items$index.name,
        source = _items$index.source,
        subText = _items$index.subText;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ImageItem.ImageItem, _objectSpread(_objectSpread({}, rest), {}, {
        label: subText,
        onFocus: handlerOnFocus(index),
        src: source,
        children: name
      }));
    };
    renderItem = function renderItem(_ref2) {
      var index = _ref2.index,
        rest = _objectWithoutProperties(_ref2, _excluded2);
      // eslint-disable-line enact/display-name
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Item["default"], _objectSpread(_objectSpread({}, rest), {}, {
        onFocus: handlerOnFocus(index),
        children: items[index].name
      }));
    };
    svgGenerator = function svgGenerator(width, height, bgColor, textColor, customText) {
      return "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ".concat(width, " ").concat(height, "' width='").concat(width, "' height='").concat(height, "'%3E") + "%3Crect width='".concat(width, "' height='").concat(height, "' fill='%23").concat(bgColor, "'%3E%3C/rect%3E") + "%3Ctext x='50%25' y='50%25' dominant-baseline='middle' text-anchor='middle' font-size='36px' fill='%23".concat(textColor, "'%3E").concat(customText, "%3C/text%3E%3C/svg%3E");
    };
    for (var i = 0; i < dataSize; i++) {
      var color = Math.floor(Math.random() * (0x1000000 - 0x101010) + 0x101010).toString(16);
      items.push({
        name: 'Account ' + i,
        // For VirtualGridList
        subText: 'SubText ' + i,
        source: svgGenerator(300, 300, color, 'ffffff', "Image ".concat(i))
      });
    }
  });
  afterEach(function () {
    clientSize = null;
    dataSize = null;
    imageItemSize = null;
    items = [];
    itemSize = 60;
    items = null;
    renderImageItem = null;
    renderItem = null;
    svgGenerator = null;
  });
  test('should navigate focus using Arrow Up/Down key', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize,
      scrollMode: "translate"
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    var item1 = list.children.item(1).children.item(0);
    var item2 = list.children.item(2).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressDownKey(item0);
    expect(currentFocusIndex).toBe(1);
    pressDownKey(item1);
    expect(currentFocusIndex).toBe(2);
    pressUpKey(item2);
    expect(currentFocusIndex).toBe(1);
  });
  test('should navigate focus from first item to last using Arrow Up key when `wrap` is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: 5,
      itemRenderer: renderItem,
      itemSize: itemSize,
      scrollMode: "translate",
      wrap: true
    }));
    jest.useFakeTimers();
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    (0, _react.act)(function () {
      return jest.advanceTimersByTime(1500);
    });
    pressUpKey(item0);
    expect(currentFocusIndex).toBe(4);
    jest.useRealTimers();
  });
  test('should navigate focus from last item to first using Arrow Down key when `wrap` is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: 5,
      itemRenderer: renderItem,
      itemSize: itemSize,
      scrollMode: "translate",
      wrap: true
    }));
    jest.useFakeTimers();
    var list = _react.screen.getByRole('list');
    var item4 = list.children.item(4).children.item(0);
    focus(item4);
    expect(currentFocusIndex).toBe(4);
    (0, _react.act)(function () {
      return jest.advanceTimersByTime(1500);
    });
    pressDownKey(item4);
    expect(currentFocusIndex).toBe(0);
    jest.useRealTimers();
  });
  test('should not navigate focus using Arrow Up/Down key when `direction` is horizontal', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: itemSize,
      scrollMode: "translate"
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressDownKey(item0);
    expect(currentFocusIndex).toBe(0);
    pressUpKey(item0);
    expect(currentFocusIndex).toBe(0);
  });
  test('should navigate focus using Arrow Left/Right key when `direction` is horizontal', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: itemSize,
      scrollMode: "translate"
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    var item1 = list.children.item(1).children.item(0);
    var item2 = list.children.item(2).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressRightKey(item0);
    expect(currentFocusIndex).toBe(1);
    pressRightKey(item1);
    expect(currentFocusIndex).toBe(2);
    pressLeftKey(item2);
    expect(currentFocusIndex).toBe(1);
  });
  test('should scroll by focus navigation using Arrow Down key', function () {
    var spy = jest.fn(function () {});
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize,
      onScrollStart: spy,
      scrollMode: "translate"
    }));
    var list = _react.screen.getByRole('list');
    var item13 = list.children.item(13).children.item(0);
    focus(item13);
    expect(currentFocusIndex).toBe(13);
    pressDownKey(item13);
    expect(currentFocusIndex).toBe(14);
    expect(spy).toHaveBeenCalled();
  });
  test('should scroll by Page Down key', function () {
    var spy = jest.fn(function () {});
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize,
      onScrollStart: spy,
      scrollMode: "translate"
    }));
    var list = _react.screen.getByRole('list');
    var item9 = list.children.item(9).children.item(0);
    var item10 = list.children.item(10).children.item(0);
    focus(item9);
    expect(currentFocusIndex).toBe(9);
    pressDownKey(item9);
    expect(currentFocusIndex).toBe(10);
    pressPageDownKey(item10);
    expect(spy).toHaveBeenCalled();
  });
  describe('VirtualGridList useEvent', function () {
    test('should navigate focus using Arrow Up/Down key', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "vertical",
        itemRenderer: renderImageItem,
        itemSize: imageItemSize,
        scrollMode: "translate"
      }));
      var list = _react.screen.getByRole('list');
      var item0 = list.children.item(0).children.item(0);
      var item4 = list.children.item(4).children.item(0);
      var item8 = list.children.item(8).children.item(0);
      focus(item0);
      expect(currentFocusIndex).toBe(0);
      pressDownKey(item0);
      pressDownKey(item0);
      expect(currentFocusIndex).toBe(4);
      pressDownKey(item4);
      expect(currentFocusIndex).toBe(8);
      pressUpKey(item8);
      expect(currentFocusIndex).toBe(4);
    });
    test('should navigate focus using Arrow Up/Down key when `direction` is horizontal', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        itemRenderer: renderImageItem,
        itemSize: imageItemSize,
        scrollMode: "translate"
      }));
      var list = _react.screen.getByRole('list');
      var item0 = list.children.item(0).children.item(0);
      var item1 = list.children.item(1).children.item(0);
      focus(item0);
      expect(currentFocusIndex).toBe(0);
      pressDownKey(item0);
      expect(currentFocusIndex).toBe(1);
      pressUpKey(item1);
      expect(currentFocusIndex).toBe(0);
    });
    test('should navigate focus using Arrow Left/Right key when `direction` is horizontal', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        itemRenderer: renderImageItem,
        itemSize: imageItemSize,
        scrollMode: "translate"
      }));
      var list = _react.screen.getByRole('list');
      var item0 = list.children.item(0).children.item(0);
      var item3 = list.children.item(3).children.item(0);
      var item6 = list.children.item(6).children.item(0);
      focus(item0);
      expect(currentFocusIndex).toBe(0);
      pressRightKey(item0);
      expect(currentFocusIndex).toBe(3);
      pressRightKey(item3);
      expect(currentFocusIndex).toBe(6);
      pressLeftKey(item6);
      expect(currentFocusIndex).toBe(3);
    });
    test('should scroll by focus navigation using Arrow Down key', function () {
      var spy = jest.fn(function () {});
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        itemRenderer: renderImageItem,
        itemSize: imageItemSize,
        onScrollStart: spy,
        scrollMode: "translate"
      }));
      var list = _react.screen.getByRole('list');
      var item13 = list.children.item(13).children.item(0);
      focus(item13);
      expect(currentFocusIndex).toBe(13);
      pressDownKey(item13);
      expect(currentFocusIndex).toBe(17);
      expect(spy).toHaveBeenCalled();
    });
    test('should scroll by Page Down key', function () {
      var spy = jest.fn(function () {});
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        itemRenderer: renderImageItem,
        itemSize: imageItemSize,
        onScrollStart: spy,
        scrollMode: "translate"
      }));
      var list = _react.screen.getByRole('list');
      var item9 = list.children.item(9).children.item(0);
      var item10 = list.children.item(10).children.item(0);
      focus(item9);
      expect(currentFocusIndex).toBe(9);
      pressDownKey(item9);
      expect(currentFocusIndex).toBe(13);
      pressPageDownKey(item10);
      expect(spy).toHaveBeenCalled();
    });
  });
});