"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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _StatelessElementBrowser = _interopRequireDefault(require("./components/StatelessElementBrowser"));
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 _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
var ElementBrowser = exports.default = /*#__PURE__*/function (_PureComponent) {
  function ElementBrowser() {
    var _this;
    (0, _classCallCheck2.default)(this, ElementBrowser);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ElementBrowser, [].concat(args));
    (0, _defineProperty2.default)(_this, "state", {
      categories: [],
      items: [],
      searchTerm: '',
      selectedCategory: _this.props.defaultCategory
    });
    (0, _defineProperty2.default)(_this, "getCategories", function () {
      var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.fetchItems();
      return (
        // NOTE: we fetch all items to determine available categories.
        _this.filterCategories(items, _this.props.categories)
      );
    });
    (0, _defineProperty2.default)(_this, "filterCategories", function (items) {
      var categories = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
      var showCategories = _this.props.showCategories;
      if (!showCategories) {
        return [];
      }
      return categories.filter(function (category) {
        return category.name === 'all' || items.some(function (item) {
          return (item.categories || []).includes(category.name);
        });
      });
    });
    (0, _defineProperty2.default)(_this, "fetchItems", function (query, category) {
      return _this.props.getItems(query, category);
    });
    (0, _defineProperty2.default)(_this, "handleSearch", function (searchTerm) {
      var defaultCategory = _this.props.defaultCategory;
      _this.setState({
        searchTerm: searchTerm,
        selectedCategory: defaultCategory
      });
    });
    (0, _defineProperty2.default)(_this, "handleCategorySelection", function (clickedCategory) {
      var stateCategoryValue = _this.state.selectedCategory;

      /**
       * Reset selection if clicked on the same category twice.
       */
      if (stateCategoryValue === clickedCategory.name) {
        return _this.setState({
          selectedCategory: _this.props.defaultCategory
        });
      }
      _this.setState({
        selectedCategory: clickedCategory.name,
        searchTerm: ''
      });
    });
    return _this;
  }
  (0, _inherits2.default)(ElementBrowser, _PureComponent);
  return (0, _createClass2.default)(ElementBrowser, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var items = this.fetchItems();
      this.setState({
        items: items,
        categories: this.getCategories(items)
      });
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      var _this$state = this.state,
        searchTerm = _this$state.searchTerm,
        selectedCategory = _this$state.selectedCategory;

      // Update both items and categories when there's a new getItems
      if (this.props.getItems !== prevProps.getItems) {
        this.setState({
          categories: this.getCategories(),
          items: this.fetchItems(searchTerm, selectedCategory)
        });
      } else if (searchTerm !== prevState.searchTerm || selectedCategory !== prevState.selectedCategory) {
        this.setState({
          items: this.fetchItems(searchTerm, selectedCategory)
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        onInsertItem = _this$props.onInsertItem,
        onSelectItem = _this$props.onSelectItem,
        showSearch = _this$props.showSearch,
        showCategories = _this$props.showCategories,
        mode = _this$props.mode,
        emptyStateHandler = _this$props.emptyStateHandler,
        onViewMore = _this$props.onViewMore,
        cache = _this$props.cache,
        autoFocusSearch = _this$props.autoFocusSearch;
      var _this$state2 = this.state,
        categories = _this$state2.categories,
        searchTerm = _this$state2.searchTerm,
        selectedCategory = _this$state2.selectedCategory,
        items = _this$state2.items;
      return /*#__PURE__*/_react.default.createElement(_StatelessElementBrowser.default, {
        items: items,
        categories: categories,
        onSearch: this.handleSearch,
        onSelectCategory: this.handleCategorySelection,
        onSelectItem: onSelectItem,
        onInsertItem: onInsertItem,
        selectedCategory: selectedCategory,
        showSearch: showSearch,
        showCategories: showCategories,
        mode: mode,
        searchTerm: searchTerm,
        emptyStateHandler: emptyStateHandler,
        cache: cache,
        onViewMore: onViewMore,
        autoFocusSearch: autoFocusSearch
      });
    }
  }]);
}(_react.PureComponent);
(0, _defineProperty2.default)(ElementBrowser, "defaultProps", {
  defaultCategory: 'all',
  onInsertItem: function onInsertItem() {}
});