import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["categories"],
  _excluded2 = ["buttonStyles"];
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; }
/**
 * @jsxRuntime classic
 * @jsx jsx
 */
import React, { Fragment, memo, useCallback } from 'react';

// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { css, jsx } from '@emotion/react';
import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext';
import Button from '@atlaskit/button/custom-theme-button';
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, fireAnalyticsEvent } from '../../analytics';
import { DEVICE_BREAKPOINT_NUMBERS, GRID_SIZE } from '../constants';
import useFocus from '../hooks/use-focus';
var arrowsKeys = new Set(['ArrowUp', 'ArrowDown']);
function CategoryList(_ref) {
  var _ref$categories = _ref.categories,
    categories = _ref$categories === void 0 ? [] : _ref$categories,
    props = _objectWithoutProperties(_ref, _excluded);
  var focusedCategoryIndex = props.focusedCategoryIndex,
    setFocusedCategoryIndex = props.setFocusedCategoryIndex,
    onSelectCategory = props.onSelectCategory;
  return jsx(Fragment, null, categories.map(function (category, index) {
    var categoriesLength = categories === null || categories === void 0 ? void 0 : categories.length;
    var selectNextCategory;
    var selectPreviousCategory;
    if (categoriesLength > 1) {
      selectNextCategory = function selectNextCategory() {
        if (index !== categoriesLength - 1) {
          setFocusedCategoryIndex(index + 1);
          onSelectCategory(categories[index + 1]);
        } else {
          setFocusedCategoryIndex(0);
          onSelectCategory(categories[0]);
        }
        return;
      };
      selectPreviousCategory = function selectPreviousCategory() {
        if (index !== 0) {
          setFocusedCategoryIndex(index - 1);
          onSelectCategory(categories[index - 1]);
        } else {
          setFocusedCategoryIndex(categoriesLength - 1);
          onSelectCategory(categories[categoriesLength - 1]);
        }
        return;
      };
    }
    return jsx(CategoryListItem, _extends({
      key: category.title,
      index: index,
      category: category,
      focus: focusedCategoryIndex === index
      // Ignored via go/ees005
      // eslint-disable-next-line react/jsx-props-no-spreading
    }, props, {
      setFocusedCategoryIndex: setFocusedCategoryIndex,
      selectPreviousCategory: selectPreviousCategory,
      selectNextCategory: selectNextCategory
    }));
  }));
}
function CategoryListItem(_ref2) {
  var category = _ref2.category,
    onSelectCategory = _ref2.onSelectCategory,
    selectedCategory = _ref2.selectedCategory,
    index = _ref2.index,
    focus = _ref2.focus,
    setFocusedCategoryIndex = _ref2.setFocusedCategoryIndex,
    createAnalyticsEvent = _ref2.createAnalyticsEvent,
    setFocusedItemIndex = _ref2.setFocusedItemIndex,
    setFocusOnSearch = _ref2.setFocusOnSearch,
    selectPreviousCategory = _ref2.selectPreviousCategory,
    selectNextCategory = _ref2.selectNextCategory;
  var ref = useFocus(focus);
  var onClick = useCallback(function () {
    /**
     * When user double clicks on same category, focus on first item.
     */
    if (selectedCategory === category.name) {
      setFocusedCategoryIndex(0);
    } else {
      setFocusedCategoryIndex(index);
    }
    onSelectCategory(category);
    fireAnalyticsEvent(createAnalyticsEvent)({
      payload: {
        action: ACTION.CLICKED,
        actionSubject: ACTION_SUBJECT.BUTTON,
        actionSubjectId: ACTION_SUBJECT_ID.BUTTON_CATEGORY,
        eventType: EVENT_TYPE.TRACK
      }
    });
  }, [onSelectCategory, category, index, selectedCategory, setFocusedCategoryIndex, createAnalyticsEvent]);
  var onFocus = useCallback(function () {
    if (!focus) {
      setFocusedCategoryIndex(index);
    }
  }, [focus, index, setFocusedCategoryIndex]);
  var getTheme = useCallback(function (currentTheme, themeProps) {
    var _currentTheme = currentTheme(themeProps),
      buttonStyles = _currentTheme.buttonStyles,
      rest = _objectWithoutProperties(_currentTheme, _excluded2);
    return _objectSpread({
      buttonStyles: _objectSpread(_objectSpread({}, buttonStyles), {}, {
        textAlign: 'start',
        marginLeft: "var(--ds-space-025, 2px)",
        height: '100%',
        width: '100%',
        color: category.name !== selectedCategory ? "var(--ds-text, #292A2E)" : "var(--ds-text-selected, #1868DB)"
      }, category.name === selectedCategory && {
        background: "var(--ds-background-selected, #E9F2FE)"
      })
    }, rest);
  }, [category.name, selectedCategory]);
  var onTabPress = useCallback(function (e) {
    var isShiftPressed = e.shiftKey;
    if (!isShiftPressed) {
      // set focus from focused category to first item in it
      if (setFocusedItemIndex) {
        setFocusedItemIndex(0);
        e.preventDefault();
      }
    } else {
      // jump from first category back to search
      if (setFocusOnSearch) {
        setFocusOnSearch();
        e.preventDefault();
      }
    }
    return;
  }, [setFocusedItemIndex, setFocusOnSearch]);
  var onArrowPress = useCallback(function (e) {
    if (e.key === 'ArrowUp' && selectPreviousCategory) {
      return selectPreviousCategory();
    }
    if (e.key === 'ArrowDown' && selectNextCategory) {
      return selectNextCategory();
    }
  }, [selectPreviousCategory, selectNextCategory]);
  var onKeyDown = useCallback(function (e) {
    var isTabPressed = e.key === 'Tab';
    var isArrowPressed = arrowsKeys.has(e.key);
    if (isTabPressed) {
      return onTabPress(e);
    }
    if (isArrowPressed) {
      return onArrowPress(e);
    }
  }, [onTabPress, onArrowPress]);
  return jsx("div", {
    css: buttonWrapper,
    role: "presentation"
  }, jsx(Button, {
    appearance: "subtle",
    isSelected: selectedCategory === category.name,
    onClick: onClick,
    onFocus: onFocus,
    onKeyDown: onKeyDown,
    theme: getTheme,
    role: "tab",
    "aria-selected": selectedCategory === category.name ? 'true' : 'false',
    "aria-controls": "browse-category-".concat(category.name, "-tab"),
    id: "browse-category--".concat(category.name, "-button"),
    ref: ref,
    testId: "element-browser-category-item",
    tabIndex: -1
  }, category.title));
}
var buttonWrapper = css(_defineProperty({
  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
  height: "".concat(GRID_SIZE * 4, "px"),
  margin: "var(--ds-space-050, 4px)".concat(" ", "var(--ds-space-050, 4px)", " ", "var(--ds-space-050, 4px)", " 0")
}, "@media (min-width: ".concat(DEVICE_BREAKPOINT_NUMBERS.medium, "px)"), {
  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766
  ':not(:last-child)': {
    marginBottom: 0
  }
}));
var MemoizedCategoryListWithAnalytics = /*#__PURE__*/memo(withAnalyticsContext({
  component: 'CategoryList'
})(CategoryList));
export default MemoizedCategoryListWithAnalytics;