"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/link-control/search-input.js
var search_input_exports = {};
__export(search_input_exports, {
  __experimentalLinkControlSearchInput: () => __experimentalLinkControlSearchInput,
  default: () => search_input_default
});
module.exports = __toCommonJS(search_input_exports);
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var import__ = require("../index.cjs");
var import_search_results = __toESM(require("./search-results.cjs"));
var import_constants = require("./constants.cjs");
var import_use_search_handler = __toESM(require("./use-search-handler.cjs"));
var import_jsx_runtime = require("react/jsx-runtime");
var noopSearchHandler = () => Promise.resolve([]);
var noop = () => {
};
var LinkControlSearchInput = (0, import_element.forwardRef)(
  ({
    value,
    children,
    currentLink = {},
    className = null,
    placeholder = null,
    withCreateSuggestion = false,
    onCreateSuggestion = noop,
    onChange = noop,
    onSelect = noop,
    showSuggestions = true,
    renderSuggestions = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_search_results.default, { ...props }),
    fetchSuggestions = null,
    allowDirectEntry = true,
    showInitialSuggestions = false,
    suggestionsQuery = {},
    withURLSuggestion = true,
    createSuggestionButtonText,
    hideLabelFromVision = false,
    suffix,
    isEntity = false,
    customValidity: customValidityProp
  }, ref) => {
    const genericSearchHandler = (0, import_use_search_handler.default)(
      suggestionsQuery,
      allowDirectEntry,
      withCreateSuggestion,
      withURLSuggestion
    );
    const searchHandler = showSuggestions ? fetchSuggestions || genericSearchHandler : noopSearchHandler;
    const [focusedSuggestion, setFocusedSuggestion] = (0, import_element.useState)();
    const onInputChange = (selection, suggestion) => {
      onChange(selection);
      setFocusedSuggestion(suggestion);
    };
    const handleRenderSuggestions = (props) => renderSuggestions({
      ...props,
      withCreateSuggestion,
      createSuggestionButtonText,
      suggestionsQuery,
      handleSuggestionClick: (suggestion) => {
        if (props.handleSuggestionClick) {
          props.handleSuggestionClick(suggestion);
        }
        onSuggestionSelected(suggestion);
      }
    });
    const onSuggestionSelected = async (selectedSuggestion) => {
      let suggestion = selectedSuggestion;
      if (import_constants.CREATE_TYPE === selectedSuggestion.type) {
        try {
          suggestion = await onCreateSuggestion(
            selectedSuggestion.title
          );
          if (suggestion?.url) {
            onSelect(suggestion);
          }
        } catch (e) {
        }
        return;
      }
      if (allowDirectEntry || suggestion && Object.keys(suggestion).length >= 1) {
        const { id, url, kind, type, ...restLinkProps } = currentLink ?? {};
        onSelect(
          // Some direct entries don't have types or IDs, and we still need to clear the previous ones.
          { ...restLinkProps, ...suggestion },
          suggestion
        );
      }
    };
    const _placeholder = placeholder ?? (0, import_i18n.__)("Search or type URL");
    const label = hideLabelFromVision && placeholder !== "" ? _placeholder : (0, import_i18n.__)("Link");
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "block-editor-link-control__search-input-container", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import__.URLInput,
        {
          disableSuggestions: currentLink?.url === value,
          label,
          hideLabelFromVision,
          className,
          value,
          onChange: onInputChange,
          placeholder: _placeholder,
          __experimentalRenderSuggestions: showSuggestions ? handleRenderSuggestions : null,
          __experimentalFetchLinkSuggestions: searchHandler,
          __experimentalHandleURLSuggestions: true,
          __experimentalShowInitialSuggestions: showInitialSuggestions,
          customValidity: customValidityProp,
          markWhenOptional: true,
          onSubmit: (suggestion, event) => {
            const hasSuggestion = suggestion || focusedSuggestion;
            if (!hasSuggestion && !value?.trim()?.length) {
              event.preventDefault();
            } else {
              onSuggestionSelected(
                hasSuggestion || { url: value }
              );
            }
          },
          inputRef: ref,
          suffix,
          disabled: isEntity
        }
      ),
      children
    ] });
  }
);
var search_input_default = LinkControlSearchInput;
var __experimentalLinkControlSearchInput = (props) => {
  (0, import_deprecated.default)("wp.blockEditor.__experimentalLinkControlSearchInput", {
    since: "6.8"
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LinkControlSearchInput, { ...props });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  __experimentalLinkControlSearchInput
});
//# sourceMappingURL=search-input.cjs.map
