import { useStoreState } from "@ariakit/react-store";
import { createElement, createHook, forwardRef } from "@ariakit/react-utils";
import type { Props } from "@ariakit/react-utils";
import { invariant } from "@ariakit/utils";
import type { ElementType } from "react";
import type { SeparatorOptions } from "../separator/separator.tsx";
import { useSeparator } from "../separator/separator.tsx";
import { useCompositeScopedContext } from "./composite-context.tsx";
import type { CompositeStore } from "./composite-store.ts";

const TagName = "hr" satisfies ElementType;
type TagName = typeof TagName;

/**
 * Returns props to create a `CompositeSeparator` component.
 * @see https://ariakit.com/components/composite
 * @example
 * ```jsx
 * const store = useCompositeStore();
 * const props = useCompositeSeparator({ store });
 * <Composite store={store}>
 *   <CompositeItem>Item 1</CompositeItem>
 *   <Role {...props} />
 *   <CompositeItem>Item 2</CompositeItem>
 * </Composite>
 * ```
 */
export const useCompositeSeparator = createHook<
  TagName,
  CompositeSeparatorOptions
>(function useCompositeSeparator({
  store,
  orientation: orientationProp,
  ...props
}) {
  const context = useCompositeScopedContext();
  store = store || context;

  invariant(
    store,
    process.env.NODE_ENV !== "production" &&
      "CompositeSeparator must be wrapped in a Composite component.",
  );

  const orientation = useStoreState(
    store,
    ["orientation"],
    (state) =>
      orientationProp ??
      (state.orientation === "horizontal" ? "vertical" : "horizontal"),
  );

  props = useSeparator({ ...props, orientation });

  return props;
});

/**
 * Renders a divider between
 * [`CompositeItem`](https://ariakit.com/reference/composite-item) elements.
 * @see https://ariakit.com/components/composite
 * @example
 * ```jsx {4}
 * <CompositeProvider>
 *   <Composite>
 *     <CompositeItem>Item 1</CompositeItem>
 *     <CompositeSeparator />
 *     <CompositeItem>Item 2</CompositeItem>
 *   </Composite>
 * </CompositeProvider>
 * ```
 */
export const CompositeSeparator = forwardRef(function CompositeSeparator(
  props: CompositeSeparatorProps,
) {
  const htmlProps = useCompositeSeparator(props);
  return createElement(TagName, htmlProps);
});

export interface CompositeSeparatorOptions<
  T extends ElementType = TagName,
> extends SeparatorOptions<T> {
  /**
   * Object returned by the
   * [`useCompositeStore`](https://ariakit.com/reference/use-composite-store)
   * hook. If not provided, the closest
   * [`Composite`](https://ariakit.com/reference/composite) or
   * [`CompositeProvider`](https://ariakit.com/reference/composite-provider)
   * components' context will be used.
   */
  store?: CompositeStore;
  /**
   * The orientation of the separator. By default, this is the opposite of the
   * [`orientation`](https://ariakit.com/reference/composite-provider#orientation)
   * state of the composite widget. Which means it doesn't need to be explicitly
   * set in most cases.
   */
  orientation?: SeparatorOptions<T>["orientation"];
}

export type CompositeSeparatorProps<T extends ElementType = TagName> = Props<
  T,
  CompositeSeparatorOptions<T>
>;
