import { dynamic } from "@solidjs/web";
import { omit } from "solid-js";
import { useDirection } from "../../direction-provider/direction-provider-context.jsx";
import { CompositeList } from "../../internals/composite/composite-list.jsx";
import { createControlledState } from "../../internals/create-controlled-state.jsx";
import { mergeRootProps } from "../../internals/merge-root-props.jsx";
import { resolveClassStyle } from "../../internals/resolve-class-style.jsx";
import { AccordionRootContext } from "../accordion-root-context.jsx";
import { createAccordion } from "../create-accordion.jsx";
/**
 * Groups all parts of the accordion. Renders a `<div role="region">`.
 *
 * Solid 2 notes:
 *
 * - Accessor-form reads for every reactive input — no top-level reactive
 *   reads, no destructuring of `props`.
 * - `value` is always stored internally as an array: single-expand mode is
 *   encoded as `[]` or `[currentlyOpenValue]`, multi-expand as the full
 *   open set. This lets the item's `isOpen` predicate be the same regardless
 *   of mode.
 * - Context provider sits INSIDE `get children()` (not wrapped around the
 *   root element) so the attribute getters and the `state` memo stay owned
 *   by this component's setup scope — matches `radio-group.tsx`.
 * - Items register with a `<CompositeList>`. The trigger's keyboard handler
 *   walks that list to find sibling triggers (see `accordion-trigger.tsx`).
 */
export function AccordionRoot(props) {
  const direction = useDirection();
  const hiddenUntilFound = () => props.hiddenUntilFound ?? false;
  const keepMounted = () => props.keepMounted ?? false;
  const loopFocus = () => props.loopFocus ?? true;

  // Controlled / uncontrolled value state (Base UI `value` / `defaultValue`
  // contract): the `value` prop wins while it is defined — an explicitly
  // passed empty array IS controlled; otherwise an internal signal (seeded
  // once from `defaultValue`, else `[]`) backs the state.
  const [resolvedValue, setUncontrolledValue] = createControlledState({
    controlled: () => props.value,
    default: () => props.defaultValue,
    fallback: [],
    name: "<Accordion.Root>",
    state: "value"
  });

  // Compose the headless primitive — it owns value-array normalization and
  // the single/multi toggle logic. The wrapper layers CompositeList +
  // region role + direction-provider on top. Change flow mirrors React's
  // `AccordionRoot.handleValueChange`: fire `onValueChange` first, honor
  // `details.cancel()`, then write the uncontrolled state (ignored for
  // reads while the `value` prop is controlled).
  const c = createAccordion({
    value: resolvedValue,
    multiple: () => props.multiple === true,
    disabled: () => props.disabled === true,
    orientation: () => props.orientation ?? "vertical",
    onValueChange: (next, details) => {
      props.onValueChange?.(next, details);
      if (details.isCanceled) return;
      setUncontrolledValue(next);
    }
  });
  const value = c.value;
  const disabled = c.disabled;
  const orientation = c.orientation;
  const handleValueChange = (newValue, nextOpen, _incoming) => {
    c.toggle(newValue, nextOpen);
  };
  const state = () => ({
    value: value(),
    disabled: disabled(),
    orientation: orientation()
  });

  // Shared array that `<CompositeList>` keeps in DOM order. The trigger's
  // keyboard handler reads it synchronously during event dispatch, so it
  // must be a plain mutable array (no signal indirection).
  const accordionItemRefs = [];
  const contextValue = {
    accordionItemRefs,
    direction,
    disabled,
    handleValueChange,
    hiddenUntilFound,
    keepMounted,
    loopFocus,
    orientation,
    state,
    value
  };
  const userStyling = resolveClassStyle(props, () => state());
  const userHtmlProps = omit(props, "class", "style", "attrs", "as", "children", "defaultValue", "disabled", "hiddenUntilFound", "keepMounted", "loopFocus", "multiple", "onValueChange", "orientation", "value");
  const internalProps = {
    role: "region",
    get dir() {
      return direction();
    },
    get "data-orientation"() {
      return orientation();
    },
    get "data-disabled"() {
      return disabled() ? "" : undefined;
    }
  };
  const styledUserProps = mergeRootProps(userStyling, userHtmlProps);
  const merged = mergeRootProps(internalProps, styledUserProps);
  const Tag = dynamic(() => props.as ?? "div");
  return <Tag {...merged}>
			<AccordionRootContext value={contextValue}>
				<CompositeList elementsRef={accordionItemRefs}>
					{props.children}
				</CompositeList>
			</AccordionRootContext>
		</Tag>;
}
//# sourceMappingURL=accordion-root.jsx.map
