text: "use strict";
  
  exports.__esModule = true;
  exports.default = exports.Search = void 0;
  
  var _propTypes = _interopRequireDefault(require("prop-types"));
  
  var _react = _interopRequireDefault(require("react"));
  
  var _fuse = _interopRequireDefault(require("fuse.js"));
  
  var _Autocomplete = _interopRequireDefault(require("../../Autocomplete"));
  
  var _SearchResult = _interopRequireDefault(require("./SearchResult"));
  
  var _Translate = _interopRequireDefault(require("../../Translate"));
  
  var _withStyles = _interopRequireDefault(require("../../../composers/withStyles"));
  
  var _styles = require("./styles");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  const defaultFuseOptions = {
    shouldSort: true,
    includeMatches: true,
    tokenize: true,
    matchAllTokens: true,
    findAllMatches: true,
    threshold: 0.1,
    distance: 200,
    maxPatternLength: 32,
    minMatchCharLength: 3
  };
  const defaultFuseKeys = [{
    name: 'label',
    weight: 0.8
  }, {
    name: 'keywords',
    weight: 0.7
  }, {
    name: 'description',
    weight: 0.5
  }];
  
  class Search extends _react.default.Component {
    constructor() {
      super(...arguments);
  
      _defineProperty(this, "fuse", void 0);
  
      _defineProperty(this, "getItemValue", result => result.item.name);
  
      _defineProperty(this, "handleItemPicked", (itemValue, result) => {
        const {
          query,
          onItemPicked
        } = this.props;
        onItemPicked(result && result.item.definition || null, {
          origin: 'Search',
          charCount: query.length
        });
      });
  
      _defineProperty(this, "handleSearch", query => {
        const trimmedQuery = query.trim();
  
        if (!trimmedQuery || !this.fuse) {
          return [];
        }
  
        return this.fuse.search(trimmedQuery);
      });
  
      _defineProperty(this, "handleAsyncSearch", query => Promise.resolve(this.handleSearch(query)));
  
      _defineProperty(this, "renderItem", (_ref) => {
        let {
          matches,
          item: {
            formattedParents
          }
        } = _ref,
            item = _objectWithoutPropertiesLoose(_ref.item, ["formattedParents"]);
  
        const {
          query
        } = this.props;
        return _react.default.createElement(_SearchResult.default, {
          query: (query || '').toLowerCase(),
          item: item,
          formattedParents: formattedParents,
          matches: matches
        });
      });
    }
  
    componentDidMount() {
      this.buildIndex(this.props.items);
    }
  
    componentDidUpdate(prevProps) {
      const {
        items,
        indexParentPath
      } = this.props;
  
      if (items !== prevProps.items || indexParentPath !== prevProps.indexParentPath) {
        this.buildIndex(items);
      }
    }
  
    buildIndex(inputItems) {
      var _this = this;
  
      if (inputItems === void 0) {
        inputItems = [];
      }
  
      const flatItemList = [];
  
      const walk = function walk(one, parents) {
        if (parents === void 0) {
          parents = [];
        }
  
        const definition = [...parents, one.name];
  
        const {
          items
        } = one,
              item = _objectWithoutPropertiesLoose(one, ["items"]);
  
        const {
          readonly
        } = item;
  
        if (!readonly) {
          flatItemList.push(_extends({}, item, {
            definition,
            label: item.label || item.name,
            formattedParents: parents.length > 0 ? _this.props.formatter([...parents, '']) : ''
          }));
        }
  
        (items || []).forEach(sub => walk(sub, definition));
      };
  
      if (inputItems) {
        inputItems.forEach(item => walk(item));
      }
  
      const fuseKeys = [...defaultFuseKeys];
  
      if (this.props.indexParentPath) {
        fuseKeys.push({
          name: 'formattedParents',
          weight: 0.2
        });
      }
  
      const fuseOptions = _extends({}, defaultFuseOptions, {
        keys: fuseKeys
      });
  
      this.fuse = new _fuse.default(flatItemList, _extends({}, fuseOptions, {}, this.props.fuseOptions));
    }
  
    render() {
      const {
        cx,
        noResultsLabel,
        maxHeight,
        onSearch,
        placeholder,
        query,
        styles,
        width
      } = this.props;
      return _react.default.createElement("div", {
        className: cx(styles.container, {
          width: query ? width : undefined
        })
      }, _react.default.createElement(_Autocomplete.default, {
        hideLabel: true,
        optional: true,
        accessibilityLabel: _Translate.default.phrase('Hierarchy item search', {}, {
          context: 'Search functionality to find items within the hierarchy menu.',
          key: 'lunar.picker.searchLabel'
        }),
        getItemValue: this.getItemValue,
        maxHeight: maxHeight,
        name: "autocomplete-search",
        noResultsText: noResultsLabel,
        value: query,
        type: "search",
        renderItem: this.renderItem,
        label: "",
        placeholder: placeholder,
        onSelectItem: this.handleItemPicked,
        onLoadItems: this.handleAsyncSearch,
        onChange: onSearch
      }));
    }
  
  }
  
  exports.Search = Search;
  
  _defineProperty(Search, "propTypes", {
    items: _propTypes.default.arrayOf(_propTypes.default.any),
    onItemPicked: _propTypes.default.func.isRequired,
    formatter: _propTypes.default.any.isRequired,
    query: _propTypes.default.string,
    noResultsLabel: _propTypes.default.any.isRequired,
    onSearch: _propTypes.default.func.isRequired,
    placeholder: _propTypes.default.string,
    indexParentPath: _propTypes.default.bool,
    fuseOptions: _propTypes.default.any,
    width: _propTypes.default.number.isRequired,
    maxHeight: _propTypes.default.number
  });
  
  _defineProperty(Search, "defaultProps", {
    fuseOptions: {},
    items: [],
    query: ''
  });
  
  var _default = (0, _withStyles.default)(_styles.styleSheet)(Search);
  
  exports.default = _default;