"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/components/src/form-token-field/token-input.tsx
var token_input_exports = {};
__export(token_input_exports, {
  TokenInput: () => TokenInput,
  UnForwardedTokenInput: () => UnForwardedTokenInput,
  default: () => token_input_default
});
module.exports = __toCommonJS(token_input_exports);
var import_clsx = __toESM(require("clsx"), 1);
var import_element = require("@wordpress/element");
var import_jsx_runtime = require("react/jsx-runtime");
function UnForwardedTokenInput(props, ref) {
  const {
    value,
    isExpanded,
    instanceId,
    selectedSuggestionIndex,
    className,
    onChange,
    onFocus,
    onBlur,
    ...restProps
  } = props;
  const [hasFocus, setHasFocus] = (0, import_element.useState)(false);
  const size = value ? value.length + 1 : 0;
  const onChangeHandler = (event) => {
    if (onChange) {
      onChange({
        value: event.target.value
      });
    }
  };
  const onFocusHandler = (e) => {
    setHasFocus(true);
    onFocus?.(e);
  };
  const onBlurHandler = (e) => {
    setHasFocus(false);
    onBlur?.(e);
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
    ref,
    id: `components-form-token-input-${instanceId}`,
    type: "text",
    ...restProps,
    value: value || "",
    onChange: onChangeHandler,
    onFocus: onFocusHandler,
    onBlur: onBlurHandler,
    size,
    className: (0, import_clsx.default)(className, "components-form-token-field__input"),
    autoComplete: "off",
    role: "combobox",
    "aria-expanded": isExpanded,
    "aria-autocomplete": "list",
    "aria-owns": isExpanded ? `components-form-token-suggestions-${instanceId}` : void 0,
    "aria-activedescendant": (
      // Only add the `aria-activedescendant` attribute when:
      // - the user is actively interacting with the input (`hasFocus`)
      // - there is a selected suggestion (`selectedSuggestionIndex !== -1`)
      // - the list of suggestions are rendered in the DOM (`isExpanded`)
      hasFocus && selectedSuggestionIndex !== -1 && isExpanded ? `components-form-token-suggestions-${instanceId}-${selectedSuggestionIndex}` : void 0
    ),
    "aria-describedby": `components-form-token-suggestions-howto-${instanceId}`
  });
}
var TokenInput = (0, import_element.forwardRef)(UnForwardedTokenInput);
var token_input_default = TokenInput;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  TokenInput,
  UnForwardedTokenInput
});
//# sourceMappingURL=token-input.cjs.map
