import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import React from 'react';
import { mount } from '../../build/enzyme';
import { KeyCodes } from '../../utils/constants';
import { TabPanel, TabItem, NavDropdown } from '../index';

var TestContainer =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(TestContainer, _React$Component);

  function TestContainer() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = TestContainer.prototype;

  _proto.render = function render() {
    var _this$props = this.props,
        children = _this$props.children,
        props = _objectWithoutPropertiesLoose(_this$props, ["children"]);

    return React.createElement("li", props, children);
  };

  return TestContainer;
}(React.Component);

var fakeContext = {
  router: {
    isActive: function isActive(location, indexOnly) {
      return location === '/active/path';
    },
    createHref: function createHref() {}
  }
};
var simplePanel = React.createElement(TabPanel, {
  "data-test-automation-id": "simplePanel"
}, React.createElement(TabItem, {
  to: "/not/active/path",
  "data-test-automation-id": "notActiveItem"
}, "Item 1"), React.createElement(TabItem, {
  to: "/active/path",
  "data-test-automation-id": "activeItem"
}, "Item 2"), React.createElement(TabItem, {
  to: "/another/not/active/path",
  "data-test-automation-id": "anotherNotActiveItem"
}, "Item 3"));
var complexPanel = React.createElement(TabPanel, {
  "data-test-automation-id": "complexPanel"
}, React.createElement(TabItem, {
  to: "/not/active/path",
  id: "tab-item-1"
}, "Valid Item 1"), React.createElement(NavDropdown, {
  to: "/active/path",
  title: "valid item",
  "data-test-automation-id": "navDropdown"
}, React.createElement(TabItem, {
  to: "/item1"
}, "NavDropdown Item 1"), React.createElement(TabItem, {
  to: "/item2"
}, "NavDropdown Item 2"), React.createElement(TabItem, {
  to: "/item3"
}, "NavDropdown Item 3")), React.createElement(TestContainer, {
  role: "tab",
  id: "test-container-1"
}, React.createElement("span", null, "Valid Item 3")), React.createElement(TestContainer, {
  role: "menuitem",
  id: "test-container-2"
}, React.createElement("span", null, "Valid Item 4")), React.createElement(TestContainer, {
  role: "unknown",
  id: "test-container-3"
}, React.createElement("span", null, "Invalid Item 5")));
var COMPLEX_PANEL_TAB_INDEXES = {
  TAB_ITEM_0: 0,
  NAV_DROPDOWN_1: 1,
  TEST_CONTAINER_2: 2,
  TEST_CONTAINER_3: 3,
  TEST_CONTAINER_4_INVALID: 4
};
describe('TabPanel', function () {
  //---------------------------------------------------------
  it('activates a tab according to current url', function () {
    var wrapper = mount(simplePanel, {
      context: fakeContext
    });

    var _wrapper$findByTestAu = wrapper.findByTestAutomation('activeItem').find('NavItem').props(),
        active = _wrapper$findByTestAu.active;

    expect(active).toBe(true);
  }); //---------------------------------------------------------

  it('opens NavDropdown with keyboard', function () {
    var wrapper = mount(complexPanel, {
      context: fakeContext
    });
    wrapper.findByTestAutomation('navDropdown').last().simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    var dropDownMenu = wrapper.find('DropdownMenu');

    var _dropDownMenu$props = dropDownMenu.props(),
        open = _dropDownMenu$props.open;

    expect(open).toBe(true);
  }); //---------------------------------------------------------

  it('will work if appears several times at a page', function () {
    var panels = React.createElement(React.Fragment, null, simplePanel, complexPanel);
    var wrapper = mount(panels, {
      context: fakeContext
    });
    var simplePanelWrapper = wrapper.findByTestAutomation('simplePanel').first();
    var simplePanelInstance = simplePanelWrapper.instance();
    var complexPanelWrapper = wrapper.findByTestAutomation('complexPanel').first();
    var complexPanelInstance = complexPanelWrapper.instance();
    expect(simplePanelWrapper.exists()).toBe(true);
    expect(complexPanelWrapper.exists()).toBe(true);
    var actualSimplePanelFocusedItemIndex = 2;
    simplePanelWrapper.find('TabItem a').last().simulate('click');
    expect(simplePanelInstance.state.focusedItemIndex).toBe(actualSimplePanelFocusedItemIndex);
    var expectedSimplePanelFocusedItemIndex = 1;
    simplePanelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_UP
    });
    expect(simplePanelInstance.state.focusedItemIndex).toBe(expectedSimplePanelFocusedItemIndex);
    var actualComplexPanelFocusedItemIndex = COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_3;
    complexPanelWrapper.find('#test-container-2 span').simulate('click');
    expect(complexPanelInstance.state.focusedItemIndex).toBe(actualComplexPanelFocusedItemIndex);
    var expectedComplexPanelFocusedItemIndex = COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0;
    complexPanelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    expect(complexPanelInstance.state.focusedItemIndex).toBe(expectedComplexPanelFocusedItemIndex);
  }); //---------------------------------------------------------

  it('correctly moves focus on right keyboard button keyDown event', function () {
    var panelWrapper = mount(complexPanel, {
      context: fakeContext
    });
    var panelInstance = panelWrapper.instance();
    panelInstance.setFocusedItemByIndex(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_RIGHT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.NAV_DROPDOWN_1);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_RIGHT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_2);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_RIGHT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_3);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_RIGHT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
  }); //---------------------------------------------------------

  it('correctly moves focus on left keyboard button keyDown event', function () {
    var panelWrapper = mount(complexPanel, {
      context: fakeContext
    });
    var panelInstance = panelWrapper.instance();
    panelInstance.setFocusedItemByIndex(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_LEFT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_3);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_LEFT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_2);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_LEFT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.NAV_DROPDOWN_1);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_LEFT
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
  }); //---------------------------------------------------------

  it('correctly moves focus on up keyboard button keyDown event', function () {
    var panelWrapper = mount(complexPanel, {
      context: fakeContext
    });
    var panelInstance = panelWrapper.instance();
    panelInstance.setFocusedItemByIndex(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_UP
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_3);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_UP
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_2);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_UP
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.NAV_DROPDOWN_1);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_UP
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
  }); //---------------------------------------------------------

  it('correctly moves focus on down keyboard button keyDown event', function () {
    var panelWrapper = mount(complexPanel, {
      context: fakeContext
    });
    var panelInstance = panelWrapper.instance();
    panelInstance.setFocusedItemByIndex(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_2);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TEST_CONTAINER_3);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.TAB_ITEM_0);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.NAV_DROPDOWN_1);
    panelWrapper.simulate('keyDown', {
      keyCode: KeyCodes.ARROW_DOWN
    });
    expect(panelInstance.state.focusedItemIndex).toBe(COMPLEX_PANEL_TAB_INDEXES.NAV_DROPDOWN_1);
    expect(panelInstance.state.isDropdownOpen).toBe(true);
  });
});