import { dynamic } from "@solidjs/web";
import { createEffect, createSignal, omit } from "solid-js";
import { composeRefs, userRef } from "../../internals/compose-refs.jsx";
import { CompositeList } from "../../internals/composite/composite-list.jsx";
import { mergeRootProps } from "../../internals/merge-root-props.jsx";
import { resolveClassStyle } from "../../internals/resolve-class-style.jsx";
import { useComboboxRootContext } from "../root/combobox-root-context.jsx";
import { ComboboxChipsContext } from "./chips-context.jsx";
const EMPTY_STATE = {};

/**
 * A container for chips that represent values in a multiselectable input.
 * Renders a `<div>` — gets `role="toolbar"` once at least one chip is
 * rendered so NVDA stays in focus mode during arrow-key traversal.
 */
export function ComboboxChips(props) {
  const root = useComboboxRootContext();
  const [highlightedChipIndex, setHighlightedChipIndex] = createSignal(undefined, {
    ownedWrite: true
  });
  createEffect(() => root.open(), openVal => {
    if (openVal && highlightedChipIndex() !== undefined) {
      setHighlightedChipIndex(undefined);
    }
  });
  const chipsRef = {
    current: []
  };
  const hasChips = () => {
    const sv = root.selectedValue();
    return Array.isArray(sv) && sv.length > 0;
  };
  const contextValue = {
    highlightedChipIndex,
    setHighlightedChipIndex,
    chipsRef
  };
  function onMouseDown() {
    if (root.readOnly() || root.disabled()) return;
    root.inputRef.current?.focus();
  }
  const setContainerRef = el => {
    root.chipsContainerRef.current = el;
  };
  const internal = {
    get role() {
      return hasChips() ? "toolbar" : undefined;
    },
    onMouseDown
  };
  const userClassStyle = resolveClassStyle(props, () => EMPTY_STATE);
  const userProps = omit(props, "as", "class", "style", "attrs");
  let merged = mergeRootProps(internal, userClassStyle);
  merged = mergeRootProps(merged, userProps);
  const Tag = dynamic(() => props.as ?? "div");
  return <ComboboxChipsContext value={contextValue}>
			<CompositeList elementsRef={chipsRef.current}>
				<Tag {...merged} ref={composeRefs(setContainerRef, userRef(props))} />
			</CompositeList>
		</ComboboxChipsContext>;
}
//# sourceMappingURL=combobox-chips.jsx.map
