import { useState } from 'react';
import { EntityPageState, EntityPageStateParams } from '../state';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { FieldValues } from 'react-hook-form';
import { useWixPatternsRouter } from '../providers/WixPatternsRouterProvider';

export interface UseEntityPageParams<T, V extends FieldValues = FieldValues>
  extends Pick<
    EntityPageStateParams<T, V>,
    | 'fetch'
    | 'onSave'
    | 'saveSuccessToast'
    | 'saveErrorToast'
    | 'form'
    | 'parentPageId'
    | 'parentPath'
    | 'fqdn'
  > {}

export function useEntityPage<T, V extends FieldValues = FieldValues>(
  props: UseEntityPageParams<T, V>,
): EntityPageState<T, V> {
  const container = useWixPatternsContainer();
  const routerState = useWixPatternsRouter();
  const [state] = useState(() => {
    return new EntityPageState({ container, routerState, ...props });
  });

  return state;
}
