"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _withAnalyticsContext = _interopRequireDefault(require("@atlaskit/analytics-next/withAnalyticsContext"));
var _customThemeButton = _interopRequireDefault(require("@atlaskit/button/custom-theme-button"));
var _analytics = require("../../analytics");
var _constants = require("../constants");
var _useFocus = _interopRequireDefault(require("../hooks/use-focus"));
var _excluded = ["categories"],
  _excluded2 = ["buttonStyles"];
/**
 * @jsxRuntime classic
 * @jsx jsx
 */
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
var arrowsKeys = new Set(['ArrowUp', 'ArrowDown']);
function CategoryList(_ref) {
  var _ref$categories = _ref.categories,
    categories = _ref$categories === void 0 ? [] : _ref$categories,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var focusedCategoryIndex = props.focusedCategoryIndex,
    setFocusedCategoryIndex = props.setFocusedCategoryIndex,
    onSelectCategory = props.onSelectCategory;
  return (0, _react2.jsx)(_react.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 (0, _react2.jsx)(CategoryListItem, (0, _extends2.default)({
      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 = (0, _useFocus.default)(focus);
  var onClick = (0, _react.useCallback)(function () {
    /**
     * When user double clicks on same category, focus on first item.
     */
    if (selectedCategory === category.name) {
      setFocusedCategoryIndex(0);
    } else {
      setFocusedCategoryIndex(index);
    }
    onSelectCategory(category);
    (0, _analytics.fireAnalyticsEvent)(createAnalyticsEvent)({
      payload: {
        action: _analytics.ACTION.CLICKED,
        actionSubject: _analytics.ACTION_SUBJECT.BUTTON,
        actionSubjectId: _analytics.ACTION_SUBJECT_ID.BUTTON_CATEGORY,
        eventType: _analytics.EVENT_TYPE.TRACK
      }
    });
  }, [onSelectCategory, category, index, selectedCategory, setFocusedCategoryIndex, createAnalyticsEvent]);
  var onFocus = (0, _react.useCallback)(function () {
    if (!focus) {
      setFocusedCategoryIndex(index);
    }
  }, [focus, index, setFocusedCategoryIndex]);
  var getTheme = (0, _react.useCallback)(function (currentTheme, themeProps) {
    var _currentTheme = currentTheme(themeProps),
      buttonStyles = _currentTheme.buttonStyles,
      rest = (0, _objectWithoutProperties2.default)(_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 = (0, _react.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 = (0, _react.useCallback)(function (e) {
    if (e.key === 'ArrowUp' && selectPreviousCategory) {
      return selectPreviousCategory();
    }
    if (e.key === 'ArrowDown' && selectNextCategory) {
      return selectNextCategory();
    }
  }, [selectPreviousCategory, selectNextCategory]);
  var onKeyDown = (0, _react.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 (0, _react2.jsx)("div", {
    css: buttonWrapper,
    role: "presentation"
  }, (0, _react2.jsx)(_customThemeButton.default, {
    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 = (0, _react2.css)((0, _defineProperty2.default)({
  // 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: "32px",
  margin: "var(--ds-space-050, 4px)".concat(" ", "var(--ds-space-050, 4px)", " ", "var(--ds-space-050, 4px)", " 0")
}, "@media (min-width: ".concat(_constants.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__*/(0, _react.memo)((0, _withAnalyticsContext.default)({
  component: 'CategoryList'
})(CategoryList));
var _default = exports.default = MemoizedCategoryListWithAnalytics;