import { useState } from 'react';
import { SettingsPageStateParams, SettingsPageState } from '../state';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { FieldValues } from 'react-hook-form';

export interface UseSettingsPageParams<T, V extends FieldValues = FieldValues>
  extends Pick<
    SettingsPageStateParams<T, V>,
    | 'fetch'
    | 'onSave'
    | 'onCancel'
    | 'saveSuccessToast'
    | 'saveErrorToast'
    | 'form'
  > {}

export function useSettingsPage<T, V extends FieldValues = FieldValues>(
  props: UseSettingsPageParams<T, V>,
): SettingsPageState<T, V> {
  const container = useWixPatternsContainer();
  const [state] = useState(() => {
    return new SettingsPageState({ container, ...props });
  });

  return state;
}
