import React from 'react';
import { useRegionalizationContext } from '../../context/RegionalizationContext';
import type { View } from '../../types/ContextProps';

interface SetViewButtonProps {
  children: React.ReactNode;
  view: View;
  closeModal?: boolean;
  className?: string;
  onClick?: () => void;
}

export const SetViewButton = ({
  children,
  view,
  closeModal,
  className,
  onClick,
}: SetViewButtonProps) => {
  const { setModalOpen, setView } = useRegionalizationContext();

  return (
    <button
      onClick={() => {
        onClick?.();

        if (closeModal) {
          setView?.('PostalCodeInputScreen');
          setModalOpen(false);
        } else {
          setView?.(view);
        }
      }}
      className={className}
    >
      {children}
    </button>
  );
};
