import { useState } from 'react';
import {
  CustomFieldsAllNamespacesFormState,
  CustomFieldsFormState,
} from '../../state';

export interface UseCustomFieldsFormStateParams {
  state: CustomFieldsAllNamespacesFormState;
  namespace: string;
}

export function useCustomFieldsFormState(
  params: UseCustomFieldsFormStateParams,
) {
  const { state: allNamespacesFormState, namespace } = params;

  const translate =
    allNamespacesFormState._customFieldsWidgetState.container.translate;

  const [state] = useState(() =>
    allNamespacesFormState.addFormIfAbsent(
      new CustomFieldsFormState({
        customFieldsWidgetState:
          allNamespacesFormState._customFieldsWidgetState,
        translate,
        namespace,
      }),
    ),
  );

  return state;
}
