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 { ButtonOptions } from "../button/button.tsx";
import { useButton } from "../button/button.tsx";
import { useFormContext } from "./form-context.tsx";
import type { FormStore } from "./form-store.ts";

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

/**
 * Returns props to create a `FormSubmit` component.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx
 * const store = useFormStore();
 * const props = useFormSubmit({ store });
 * <Form store={store}>
 *   <Role {...props}>Submit</Role>
 * </Form>
 * ```
 */
export const useFormSubmit = createHook<TagName, FormSubmitOptions>(
  function useFormSubmit({ store, accessibleWhenDisabled = true, ...props }) {
    const context = useFormContext();
    store = store || context;

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

    props = {
      type: "submit",
      disabled: useStoreState(store, "submitting"),
      ...props,
    };

    props = useButton({ ...props, accessibleWhenDisabled });

    return props;
  },
);

/**
 * Renders a native submit button inside a form. The button will be
 * [`disabled`](https://ariakit.com/reference/form-submit#disabled) while the
 * form is submitting, but it will remain accessible to keyboard and screen
 * reader users thanks to the
 * [`accessibleWhenDisabled`](https://ariakit.com/reference/form-submit#accessiblewhendisabled)
 * prop that's enabled by default.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx {4}
 * const form = useFormStore();
 *
 * <Form store={form}>
 *   <FormSubmit>Submit</FormSubmit>
 * </Form>
 * ```
 */
export const FormSubmit = forwardRef(function FormSubmit(
  props: FormSubmitProps,
) {
  const htmlProps = useFormSubmit(props);
  return createElement(TagName, htmlProps);
});

export interface FormSubmitOptions<
  T extends ElementType = TagName,
> extends ButtonOptions<T> {
  /**
   * Object returned by the
   * [`useFormStore`](https://ariakit.com/reference/use-form-store) hook. If not
   * provided, the closest [`Form`](https://ariakit.com/reference/form) or
   * [`FormProvider`](https://ariakit.com/reference/form-provider) components'
   * context will be used.
   */
  store?: FormStore;
  /**
   * @default true
   */
  accessibleWhenDisabled?: ButtonOptions<T>["accessibleWhenDisabled"];
}

export type FormSubmitProps<T extends ElementType = TagName> = Props<
  T,
  FormSubmitOptions<T>
>;
