"""Safer phase-correlation wrapper: reject exactly featureless inputs."""
import numpy as np
try:
 from .phase_alignment import phase_displacement,accepted_shift
except ImportError:
 from phase_alignment import phase_displacement,accepted_shift

def checked_alignment(reference,target,min_response=.1,max_shift=32.):
 """Return tentative correction or a reason for abstention.

 A nonconstant image and a high response do not prove reliable registration;
 this only catches the exact no-information case and applies fixed heuristics.
 """
 if not isinstance(reference,np.ndarray) or not isinstance(target,np.ndarray) or reference.ndim!=2 or reference.shape!=target.shape or min(reference.shape)<8:raise ValueError('Expected equal-sized 2D images')
 if not np.issubdtype(reference.dtype,np.number) or not np.issubdtype(target.dtype,np.number) or not np.isfinite(reference).all() or not np.isfinite(target).all():raise ValueError('Expected finite numeric inputs')
 if np.ptp(reference)==0 or np.ptp(target)==0:return {'status':'unidentifiable_constant','estimate':None,'correction':None}
 estimate=phase_displacement(reference,target);correction=accepted_shift(estimate,min_response,max_shift)
 return {'status':'tentative' if correction is not None else 'heuristic_reject','estimate':estimate,'correction':correction}
