/**
 * `<Autocomplete.Input>` — the text input.
 */

import { dynamic } from "@solidjs/web";
import { createMemo, omit } from "solid-js";
import { useComboboxRootContext } from "../../combobox/root/combobox-root-context.jsx";
import { createChangeEventDetails, REASONS } from "../../internals/change-event-details.jsx";
import { composeRefs, userRef } from "../../internals/compose-refs.jsx";
import { mergeRootProps } from "../../internals/merge-root-props.jsx";
import { resolveClassStyle } from "../../internals/resolve-class-style.jsx";
import { useAutocompleteRootContext } from "../root/autocomplete-root-context.jsx";
export function AutocompleteInput(props) {
  const root = useComboboxRootContext();
  const autocomplete = useAutocompleteRootContext();
  const disabled = () => root.disabled() || (props.disabled ?? false);
  const readOnly = () => root.readOnly();
  const listEmpty = createMemo(() => root.flatFilteredItems().length === 0);
  const setInputRef = el => {
    root.setInputElement(el);
  };
  const state = createMemo(() => ({
    open: root.open(),
    disabled: disabled(),
    readOnly: readOnly(),
    popupSide: root.popupSide(),
    listEmpty: listEmpty()
  }));
  const ariaAutocomplete = () => {
    return autocomplete.mode();
  };
  function onInput(event) {
    const value = event.currentTarget.value;
    const details = createChangeEventDetails(REASONS.inputChange, event);
    root.setInputValue(value, details);
    if (!readOnly() && !disabled()) {
      if (!root.open() && (value !== "" || root.openOnInputClick())) {
        root.setOpen(true, details);
      }
    }
    if (!root.autoHighlightMode()) {
      root.setIndices({
        activeIndex: null,
        type: "keyboard"
      });
    }
  }
  function onKeyDown(event) {
    if (disabled() || readOnly()) return;
    root.keyboardActiveRef.current = true;
    if (event.key === "Enter" && root.open()) {
      const ai = root.activeIndex();
      if (ai == null) return;
      event.preventDefault();
      const li = root.listRef.current[ai];
      if (li) li.click();
      return;
    }
    if (event.key === "Escape" && !root.open() && root.inputValue() !== "") {
      const details = createChangeEventDetails(REASONS.escapeKey, event);
      root.setInputValue("", details);
    }
  }
  function onPointerMove() {
    root.keyboardActiveRef.current = false;
  }
  function onPointerDown() {
    root.keyboardActiveRef.current = false;
  }
  const internal = {
    type: "text",
    autocomplete: "off",
    spellcheck: false,
    role: "combobox",
    "aria-haspopup": "listbox",
    get "aria-expanded"() {
      return root.open() ? "true" : "false";
    },
    get "aria-controls"() {
      return root.open() ? root.listElement()?.id ?? "" : "";
    },
    get "aria-autocomplete"() {
      return ariaAutocomplete();
    },
    get value() {
      return root.inputValue();
    },
    get disabled() {
      return disabled();
    },
    get readOnly() {
      return readOnly();
    },
    get required() {
      return root.required();
    },
    onInput,
    onKeyDown,
    onPointerMove,
    onPointerDown,
    // inputStateAttributesMapping projection.
    get "data-popup-open"() {
      return state().open ? "" : undefined;
    },
    get "data-disabled"() {
      return state().disabled ? "" : undefined;
    },
    get "data-readonly"() {
      return state().readOnly ? "" : undefined;
    },
    get "data-list-empty"() {
      return state().listEmpty ? "" : undefined;
    },
    get "data-popup-side"() {
      return state().popupSide ?? undefined;
    }
  };
  const userClassStyle = resolveClassStyle(props, () => state());
  const userProps = omit(props, "as", "class", "style", "attrs", "disabled");
  let merged = mergeRootProps(root.interactions.getReferenceProps(), internal);
  merged = mergeRootProps(merged, userClassStyle);
  merged = mergeRootProps(merged, userProps);
  const Tag = dynamic(() => props.as ?? "input");
  return <Tag {...merged} ref={composeRefs(setInputRef, userRef(props))} />;
}
//# sourceMappingURL=autocomplete-input.jsx.map
