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 `FormReset` component.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx
 * const store = useFormStore();
 * const props = useFormReset({ store });
 * <Form store={store}>
 *   <Role {...props}>Reset</Role>
 * </Form>
 * ```
 */
export const useFormReset = createHook<TagName, FormResetOptions>(
  function useFormReset({ store, ...props }) {
    const context = useFormContext();
    store = store || context;

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

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

    props = useButton(props);

    return props;
  },
);

/**
 * Renders a button that resets the form to its initial values, as defined by
 * the
 * [`defaultValues`](https://ariakit.com/reference/use-form-store#defaultvalues)
 * prop given to the form store.
 * @see https://ariakit.com/components/form
 * @example
 * ```jsx {4}
 * const form = useFormStore();
 *
 * <Form store={form}>
 *   <FormReset>Reset</FormReset>
 * </Form>
 * ```
 */
export const FormReset = forwardRef(function FormReset(props: FormResetProps) {
  const htmlProps = useFormReset(props);
  return createElement(TagName, htmlProps);
});

export interface FormResetOptions<
  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;
}

export type FormResetProps<T extends ElementType = TagName> = Props<
  T,
  FormResetOptions<T>
>;
