import { FormType, ResponseCategory } from '@spiffy-ai/commerce-api-client';
import { useMemo } from 'react';
import { Response } from 'src/application/models';
import { MessageInterceptor } from './types';

export const useMessageInterceptor = () => {
  const interceptorMap = useMemo(() => {
    const interceptors = new Map<FormType, MessageInterceptor>();
    // Form escalation interceptor removed as per user instruction
    return interceptors;
  }, []);

  const intercept = (response?: Response) => {
    if (response?.category === ResponseCategory.Form) {
      const formType = response.attributes.formCategory?.formType;
      if (formType) {
        const interceptor = interceptorMap.get(formType);
        interceptor?.execute(response);
        return interceptor?.blockExecution();
      }
    }

    return false;
  };

  return {
    intercept,
  };
};
