use std::sync::Arc;

use common_error::DaftResult;

use crate::{
    array::DataArray,
    datatypes::{DaftArrowBackedType, UInt64Array},
    kernels::search_sorted,
};

impl<T> DataArray<T>
where
    T: DaftArrowBackedType + 'static,
{
    pub fn search_sorted(&self, keys: &Self, descending: bool) -> DaftResult<UInt64Array> {
        let array = search_sorted::search_sorted(
            self.to_arrow().as_ref(),
            keys.to_arrow().as_ref(),
            descending,
        )?;

        DataArray::from_arrow(self.field.clone(), Arc::new(array))
    }
}
