"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.useBlockEditorAutocompleteProps = useBlockEditorAutocompleteProps;
var _hooks = require("@wordpress/hooks");
var _components = require("@wordpress/components");
var _element = require("@wordpress/element");
var _blocks = require("@wordpress/blocks");
var _context = require("../block-edit/context");
var _block = _interopRequireDefault(require("../../autocompleters/block"));
var _link = _interopRequireDefault(require("../../autocompleters/link"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/**
 * Shared reference to an empty array for cases where it is important to avoid
 * returning a new array reference on every invocation.
 *
 * @type {Array}
 */const EMPTY_ARRAY = [];
function useCompleters({
  completers = EMPTY_ARRAY
}) {
  const {
    name
  } = (0, _context.useBlockEditContext)();
  return (0, _element.useMemo)(() => {
    let filteredCompleters = [...completers, _link.default];
    if (name === (0, _blocks.getDefaultBlockName)() || (0, _blocks.getBlockSupport)(name, '__experimentalSlashInserter', false)) {
      filteredCompleters = [...filteredCompleters, _block.default];
    }
    if ((0, _hooks.hasFilter)('editor.Autocomplete.completers')) {
      // Provide copies so filters may directly modify them.
      if (filteredCompleters === completers) {
        filteredCompleters = filteredCompleters.map(completer => ({
          ...completer
        }));
      }
      filteredCompleters = (0, _hooks.applyFilters)('editor.Autocomplete.completers', filteredCompleters, name);
    }
    return filteredCompleters;
  }, [completers, name]);
}
function useBlockEditorAutocompleteProps(props) {
  return (0, _components.__unstableUseAutocompleteProps)({
    ...props,
    completers: useCompleters(props)
  });
}

/**
 * Wrap the default Autocomplete component with one that supports a filter hook
 * for customizing its list of autocompleters.
 *
 * @type {import('react').FC}
 */
function BlockEditorAutocomplete(props) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Autocomplete, {
    ...props,
    completers: useCompleters(props)
  });
}

/**
 * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/autocomplete/README.md
 */
var _default = exports.default = BlockEditorAutocomplete;
//# sourceMappingURL=index.js.map