"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _analyticsNext = require("@atlaskit/analytics-next");
var _customThemeButton = _interopRequireDefault(require("@atlaskit/button/custom-theme-button"));
var _colors = require("@atlaskit/theme/colors");
var _analytics = require("../../analytics");
var _constants = require("../constants");
var _useFocus = _interopRequireDefault(require("../hooks/use-focus"));
var _excluded = ["categories"],
  _excluded2 = ["buttonStyles"];
/** @jsx jsx */
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 && Object.prototype.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 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; }
function CategoryList(_ref) {
  var _ref$categories = _ref.categories,
    categories = _ref$categories === void 0 ? [] : _ref$categories,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _React$useState = _react.default.useState(null),
    _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
    focusedCategoryIndex = _React$useState2[0],
    setFocusedCategoryIndex = _React$useState2[1];
  return (0, _react2.jsx)(_react.Fragment, null, categories.map(function (category, index) {
    return (0, _react2.jsx)(CategoryListItem, (0, _extends2.default)({
      key: category.title,
      index: index,
      category: category,
      focus: focusedCategoryIndex === index,
      setFocusedCategoryIndex: setFocusedCategoryIndex
    }, props));
  }));
}
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;
  var ref = (0, _useFocus.default)(focus);
  var onClick = (0, _react.useCallback)(function () {
    onSelectCategory(category);
    /**
     * When user double clicks on same category, focus on first item.
     */
    if (selectedCategory === category.name) {
      setFocusedCategoryIndex(0);
    } else {
      setFocusedCategoryIndex(index);
    }
    (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, ".concat(_colors.N800, ")") : "var(--ds-text-selected, ".concat(_colors.B400, ")")
      }, category.name === selectedCategory && {
        background: "var(--ds-background-selected, ".concat(_colors.B50, ")")
      })
    }, rest);
  }, [category.name, selectedCategory]);
  return (0, _react2.jsx)("div", {
    css: buttonWrapper,
    role: "presentation"
  }, (0, _react2.jsx)(_customThemeButton.default, {
    appearance: "subtle",
    isSelected: selectedCategory === category.name,
    onClick: onClick,
    onFocus: onFocus,
    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"
  }, category.title));
}
var buttonWrapper = (0, _react2.css)((0, _defineProperty2.default)({
  height: "".concat(_constants.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(_constants.DEVICE_BREAKPOINT_NUMBERS.medium, "px)"), {
  ':not(:last-child)': {
    marginBottom: 0
  }
}));
var MemoizedCategoryListWithAnalytics = /*#__PURE__*/(0, _react.memo)((0, _analyticsNext.withAnalyticsContext)({
  component: 'CategoryList'
})(CategoryList));
var _default = exports.default = MemoizedCategoryListWithAnalytics;