import { dynamic } from "@solidjs/web";
import { createMemo, omit } from "solid-js";
import { createChangeEventDetails, REASONS } from "../../internals/change-event-details.jsx";
import { composeRefs, userRef } from "../../internals/compose-refs.jsx";
import { useCompositeListItem } from "../../internals/composite/composite-list.jsx";
import { stopEvent } from "../../internals/floating-ui/utils/event.jsx";
import { mergeRootProps } from "../../internals/merge-root-props.jsx";
import { resolveClassStyle } from "../../internals/resolve-class-style.jsx";
import { useComboboxChipsContext } from "../chips/chips-context.jsx";
import { useComboboxRootContext } from "../root/combobox-root-context.jsx";
import { ComboboxChipContext } from "./chip-context.jsx";
/**
 * An individual chip that represents a value in a multiselectable input.
 * Renders a `<div>` element.
 */
export function ComboboxChip(props) {
  const root = useComboboxRootContext();
  const chipsCtxRaw = useComboboxChipsContext();
  if (chipsCtxRaw == null) {
    throw new Error("Base UI: <Combobox.Chip> must be rendered inside <Combobox.Chips>.");
  }
  const chipsCtx = chipsCtxRaw;
  const disabled = () => root.disabled();
  const readOnly = () => root.readOnly();
  const listItem = useCompositeListItem();
  const state = createMemo(() => ({
    disabled: disabled()
  }));
  function handleKeyDown(event) {
    const index = listItem.index();
    const chipsCount = chipsCtx.chipsRef.current.length;
    const selected = root.selectedValue();
    const selectedArr = Array.isArray(selected) ? selected : [];
    let nextIndex = index;
    if (event.key === "ArrowLeft") {
      event.preventDefault();
      nextIndex = index > 0 ? index - 1 : undefined;
    } else if (event.key === "ArrowRight") {
      event.preventDefault();
      nextIndex = index < chipsCount - 1 ? index + 1 : undefined;
    } else if (event.key === "Backspace" || event.key === "Delete") {
      const computedNext = index >= selectedArr.length - 1 ? selectedArr.length - 2 : index;
      nextIndex = computedNext >= 0 ? computedNext : undefined;
      stopEvent(event);
      root.setIndices({
        activeIndex: null,
        selectedIndex: null,
        type: "keyboard"
      });
      const details = createChangeEventDetails(REASONS.none, event);
      root.setSelectedValue(selectedArr.filter((_, i) => i !== index), details);
    } else if (event.key === "Enter" || event.key === " ") {
      stopEvent(event);
      nextIndex = undefined;
    } else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
      stopEvent(event);
      const details = createChangeEventDetails(REASONS.listNavigation, event);
      root.setOpen(true, details);
      nextIndex = undefined;
    } else if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
      nextIndex = undefined;
    }
    return nextIndex;
  }
  function onKeyDown(event) {
    if (disabled() || readOnly()) return;
    const next = handleKeyDown(event);
    chipsCtx.setHighlightedChipIndex(next);
    if (next === undefined) {
      root.inputRef.current?.focus();
    } else {
      chipsCtx.chipsRef.current[next]?.focus();
    }
  }
  const internal = {
    tabIndex: -1,
    get "aria-disabled"() {
      return disabled() ? "true" : undefined;
    },
    get "aria-readonly"() {
      return readOnly() ? "true" : undefined;
    },
    onKeyDown,
    // Default state-attr projection: data-disabled present when true.
    get "data-disabled"() {
      return state().disabled ? "" : undefined;
    },
    get children() {
      return props.children;
    }
  };
  const userClassStyle = resolveClassStyle(props, () => state());
  const userProps = omit(props, "as", "class", "style", "attrs", "children");
  let merged = mergeRootProps(internal, userClassStyle);
  merged = mergeRootProps(merged, userProps);
  const Tag = dynamic(() => props.as ?? "div");
  return <ComboboxChipContext value={{
    index: listItem.index
  }}>
			<Tag {...merged} ref={composeRefs(listItem.ref, userRef(props))} />
		</ComboboxChipContext>;
}
//# sourceMappingURL=combobox-chip.jsx.map
