"""POSE-NMS-01: public greedy IoU NMS; returned indices retain whole observations."""
import numpy as np


def pose_nms_indices(observations, threshold=.85):
    from supervision import box_non_max_suppression
    if not 0 <= threshold <= 1 or len(observations)>300:
        raise ValueError('Invalid NMS boundary.')
    if not observations:
        return ()
    first=observations[0]
    if any(o.frame!=first.frame or o.model_key!=first.model_key for o in observations):
        raise ValueError('NMS cannot cross Frame or model.')
    boxes=np.asarray([o.bbox_xyxy for o in observations],dtype=np.float64)
    scores=np.asarray([o.score for o in observations],dtype=np.float64)
    order=np.lexsort((np.arange(len(scores)),-scores))
    # Library sorts by score using an unstable tie sort. Unique ranks encode the same
    # score order plus original-index ties without changing any output confidence.
    priority=np.empty(len(scores),dtype=np.float64)
    priority[order]=np.arange(len(scores),0,-1)
    predictions=np.column_stack((boxes,priority,[o.class_id for o in observations]))
    # Supervision's IoU matrix is float32; compare the boundary at that precision.
    keep=box_non_max_suppression(predictions,iou_threshold=float(np.float32(threshold)))
    return tuple(int(i) for i in np.flatnonzero(keep))
