"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _TagListSt = require("./TagList.st.css.js");
var _Tag = _interopRequireDefault(require("../Tag"));
var _TagListAction = _interopRequireDefault(require("./TagListAction"));
var _ToggleMoreButton = _interopRequireDefault(require("./ToggleMoreButton"));
var _TagList = require("./TagList.constants");
var _excluded = ["className"];
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/TagList/TagList.tsx";
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); }
var buttonSize = {
  small: 'tiny',
  medium: 'small',
  large: 'medium'
};
var TagList = _ref => {
  var {
    dataHook,
    tags,
    actionButton,
    size = 'small',
    onTagRemove,
    maxVisibleTags = 3,
    toggleMoreButton,
    initiallyExpanded = false
  } = _ref;
  var [isExpanded, setIsExpanded] = (0, _react.useState)(initiallyExpanded);
  var closeButtonRefs = (0, _react.useRef)(new Map());
  var pendingFocusRef = (0, _react.useRef)(null);
  var visibleTags = isExpanded ? tags : tags.slice(0, maxVisibleTags);
  var actionButtonSize = buttonSize[size];
  var cleanupStaleRefs = (0, _react.useCallback)(() => {
    var currentTagIds = new Set(visibleTags.map(tag => tag.id));
    var refsToDelete = [];
    closeButtonRefs.current.forEach((_, tagId) => {
      if (!currentTagIds.has(tagId)) {
        refsToDelete.push(tagId);
      }
    });
    refsToDelete.forEach(tagId => closeButtonRefs.current.delete(tagId));
  }, [visibleTags]);
  (0, _react.useEffect)(() => {
    cleanupStaleRefs();
    if (pendingFocusRef.current) {
      var closeButtonToFocus = closeButtonRefs.current.get(pendingFocusRef.current);
      if (closeButtonToFocus) {
        closeButtonToFocus.focus();
      }
      pendingFocusRef.current = null;
    }
  }, [visibleTags, cleanupStaleRefs]);
  var handleTagRemove = (0, _react.useCallback)(tagId => {
    var currentIndex = visibleTags.findIndex(tag => tag.id === tagId);
    if (currentIndex !== -1) {
      var nextFocusIndex;
      if (currentIndex < visibleTags.length - 1) {
        nextFocusIndex = currentIndex + 1;
      } else if (currentIndex > 0) {
        nextFocusIndex = currentIndex - 1;
      } else {
        nextFocusIndex = -1;
      }
      if (nextFocusIndex !== -1) {
        var nextTag = visibleTags[nextFocusIndex];
        if (nextTag) {
          pendingFocusRef.current = nextTag.id;
        }
      }
    }
    onTagRemove == null || onTagRemove(tagId);
  }, [visibleTags, onTagRemove]);
  var setCloseButtonRef = (0, _react.useCallback)((tagId, element) => {
    if (element) {
      closeButtonRefs.current.set(tagId, element);
    } else {
      closeButtonRefs.current.delete(tagId);
    }
  }, []);
  var toggleExpanded = (0, _react.useCallback)(() => {
    setIsExpanded(previous => !previous);
  }, []);
  var renderToggleMoreButton = (0, _react.useCallback)(() => {
    var amountOfHiddenTags = tags.length - maxVisibleTags;
    if (toggleMoreButton && amountOfHiddenTags > 0) {
      return /*#__PURE__*/_react.default.createElement(_ToggleMoreButton.default, {
        size: actionButtonSize,
        toggleMoreButton: toggleMoreButton,
        amountOfHiddenTags: amountOfHiddenTags,
        isExpanded: isExpanded,
        dataHook: _TagList.DATA_HOOKS.toggleMoreButton,
        onClick: toggleExpanded,
        __self: void 0,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 106,
          columnNumber: 9
        }
      });
    }
    return null;
  }, [isExpanded, maxVisibleTags, actionButtonSize, tags.length, toggleExpanded, toggleMoreButton]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _TagListSt.classes.root,
    "data-hook": dataHook,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 128,
      columnNumber: 5
    }
  }, visibleTags.map(_ref2 => {
    var {
        className
      } = _ref2,
      tagProps = (0, _objectWithoutProperties2.default)(_ref2, _excluded);
    return /*#__PURE__*/_react.default.createElement(_Tag.default, (0, _extends2.default)({}, tagProps, {
      closeButtonRef: el => setCloseButtonRef(tagProps.id, el),
      className: (0, _TagListSt.st)(_TagListSt.classes.item, className),
      size: size,
      onRemove: handleTagRemove,
      key: tagProps.id,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 130,
        columnNumber: 9
      }
    }));
  }), renderToggleMoreButton(), actionButton && /*#__PURE__*/_react.default.createElement(_TagListAction.default, {
    dataHook: _TagList.DATA_HOOKS.actionButton,
    size: actionButtonSize,
    onClick: actionButton.onClick,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 141,
      columnNumber: 9
    }
  }, actionButton.label));
};
TagList.displayName = 'TagList';
var _default = exports.default = TagList;
//# sourceMappingURL=TagList.js.map