use std::collections::BTreeSet;

use crate::state::RuntimeCompanyControllerKind;

pub(super) fn validate_known_ids(
    ids: &[u32],
    known_ids: &BTreeSet<u32>,
    missing_message: impl Fn(u32) -> String,
) -> Result<(), String> {
    for id in ids {
        if !known_ids.contains(id) {
            return Err(missing_message(*id));
        }
    }
    Ok(())
}

pub(super) fn ensure_no_unknown_company_controller_kinds(
    companies: &[crate::state::RuntimeCompany],
    error: &str,
) -> Result<(), String> {
    if companies
        .iter()
        .any(|company| company.controller_kind == RuntimeCompanyControllerKind::Unknown)
    {
        Err(error.to_string())
    } else {
        Ok(())
    }
}

pub(super) fn ensure_no_unknown_player_controller_kinds(
    players: &[crate::state::RuntimePlayer],
    error: &str,
) -> Result<(), String> {
    if players
        .iter()
        .any(|player| player.controller_kind == RuntimeCompanyControllerKind::Unknown)
    {
        Err(error.to_string())
    } else {
        Ok(())
    }
}
