// Exact Rust translations of LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs and
// LibreMetaverse.Tests/ExperiencePreferencesMessageTests.cs public message cases.
// Source SHA-256: b023ec7ac43124fcd47b15474a9d66d1fca522d1166dc0d5c21b8c82189051f2
// Source SHA-256: 7b67b72c2ad652626126d0e4eda7e8c26fc94c28f0f915238b2925f80a3d4af2

use libremetaverse::messages::linden::{
    ExperienceListMessage, ExperiencePreferencesMessage, RegionExperiencesMessage,
};
use libremetaverse_structured_data::{OSD, OSDMap};
use libremetaverse_types::UUID;

fn uuid() -> UUID {
    UUID::random().expect("UUID Random")
}

fn map(entries: impl IntoIterator<Item = (&'static str, OSD)>) -> OSDMap {
    OSDMap::new_with_dictionary(
        entries
            .into_iter()
            .map(|(key, value)| (key.to_owned(), value))
            .collect(),
    )
    .expect("OSDMap dictionary constructor")
}

fn ids(values: impl IntoIterator<Item = UUID>) -> OSD {
    OSD::Array(values.into_iter().map(OSD::UUID).collect())
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.ExperienceListMessage_Deserialize_ReadsExperienceIdsKey::test aa1f7d09f6c79118ea2e60621d57bd63632085ce81bc979e42294aca53d4f0d9 translated
#[test]
fn experience_list_deserialize_reads_experience_ids_key() {
    let id = uuid();
    let mut message = ExperienceListMessage::new().expect("ExperienceListMessage constructor");
    message
        .deserialize(map([("experience_ids", ids([id]))]))
        .unwrap();
    assert_eq!(message.experience_i_ds, vec![id]);
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.ExperienceListMessage_Deserialize_IgnoresLegacyExperienceKeysKey::test cd1d372b9a976c04dee11da1ade182e4f9239e1f7633e788347c6d5f255835ec translated
#[test]
fn experience_list_deserialize_ignores_legacy_experience_keys_key() {
    let mut message = ExperienceListMessage::new().expect("ExperienceListMessage constructor");
    message
        .deserialize(map([("experience_keys", ids([uuid()]))]))
        .unwrap();
    assert!(message.experience_i_ds.is_empty());
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.ExperienceListMessage_Serialize_WritesExperienceIdsKey::test 3fa4cb5d598940f693285bcc64179a4dea927a3643a76a061a933a265488d4be translated
#[test]
fn experience_list_serialize_writes_experience_ids_key() {
    let mut message = ExperienceListMessage::new().expect("ExperienceListMessage constructor");
    message.experience_i_ds.push(uuid());
    let encoded = message.serialize().unwrap();
    assert!(encoded.contains_key("experience_ids".into()).unwrap());
    assert!(!encoded.contains_key("experience_keys".into()).unwrap());
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.RegionExperiencesMessage_Deserialize_ReadsBlockedTrustedAllowedAndDefault::test 61df3b0c60d32b6c9bb843eb7a1a11fe8c0b1fd59c7a09853ad1b0e1d93cd4fd translated
#[test]
fn region_experiences_deserialize_reads_all_lists_and_default() {
    let blocked = uuid();
    let trusted = uuid();
    let allowed = uuid();
    let default_experience = uuid();
    let mut message =
        RegionExperiencesMessage::new().expect("RegionExperiencesMessage constructor");
    message
        .deserialize(map([
            ("blocked", ids([blocked])),
            ("trusted", ids([trusted])),
            ("allowed", ids([allowed])),
            ("default", OSD::UUID(default_experience)),
        ]))
        .unwrap();
    assert_eq!(message.blocked, vec![blocked]);
    assert_eq!(message.trusted, vec![trusted]);
    assert_eq!(message.allowed, vec![allowed]);
    assert_eq!(message.default, default_experience);
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.RegionExperiencesMessage_Deserialize_IgnoresLegacyContribKey::test 851d037919003ebb414105e57bf37f73ba37dff8f733c36183c447f1349f3316 translated
#[test]
fn region_experiences_deserialize_ignores_legacy_contrib_key() {
    let mut message =
        RegionExperiencesMessage::new().expect("RegionExperiencesMessage constructor");
    message
        .deserialize(map([
            ("blocked", OSD::Array(Vec::new())),
            ("trusted", OSD::Array(Vec::new())),
            ("contrib", ids([uuid()])),
        ]))
        .unwrap();
    assert!(message.allowed.is_empty());
}

// parity-case: LibreMetaverse.Tests/ExperienceListAndRegionMessageTests.cs::ExperienceListAndRegionMessageTests.RegionExperiencesMessage_Serialize_WritesAllowedKeyNotContrib::test 53c3a7d6a200816615a9efaa222ccf294e3a8334d320812c6ec56a0a3e7e05bb translated
#[test]
fn region_experiences_serialize_writes_allowed_not_contrib() {
    let mut message =
        RegionExperiencesMessage::new().expect("RegionExperiencesMessage constructor");
    message.allowed.push(uuid());
    let encoded = message.serialize().unwrap();
    assert!(encoded.contains_key("allowed".into()).unwrap());
    assert!(!encoded.contains_key("contrib".into()).unwrap());
}

// parity-case: LibreMetaverse.Tests/ExperiencePreferencesMessageTests.cs::ExperiencePreferencesMessageTests.Deserialize_ReadsAllowedListFromExperiencesKey::test 9bab78dbe142a3da6ad21a278f25d7da90776fce3861c7d602203c1e1842c9af translated
#[test]
fn experience_preferences_deserialize_reads_experiences_and_blocked() {
    let allowed = uuid();
    let blocked = uuid();
    let mut message =
        ExperiencePreferencesMessage::new().expect("ExperiencePreferencesMessage constructor");
    message
        .deserialize(map([
            ("experiences", ids([allowed])),
            ("blocked", ids([blocked])),
        ]))
        .unwrap();
    assert_eq!(message.allowed, vec![allowed]);
    assert_eq!(message.blocked, vec![blocked]);
}

// parity-case: LibreMetaverse.Tests/ExperiencePreferencesMessageTests.cs::ExperiencePreferencesMessageTests.Deserialize_IgnoresLegacyAllowedKey::test ec6d5e7c81214ea4e3d99740fdd33961578f895a90a42082067ca9819d93d2b0 translated
#[test]
fn experience_preferences_deserialize_ignores_legacy_allowed_key() {
    let mut message =
        ExperiencePreferencesMessage::new().expect("ExperiencePreferencesMessage constructor");
    message
        .deserialize(map([
            ("allowed", ids([uuid()])),
            ("blocked", OSD::Array(Vec::new())),
        ]))
        .unwrap();
    assert!(message.allowed.is_empty());
}

// parity-case: LibreMetaverse.Tests/ExperiencePreferencesMessageTests.cs::ExperiencePreferencesMessageTests.Serialize_WritesAllowedListUnderExperiencesKey::test 45a0bffe39ab7bf0734ec5fce4435dbdda7ae94cf4e933210669e91dc45b1baf translated
#[test]
fn experience_preferences_serialize_writes_experiences_not_allowed() {
    let mut message =
        ExperiencePreferencesMessage::new().expect("ExperiencePreferencesMessage constructor");
    message.allowed.push(uuid());
    let encoded = message.serialize().unwrap();
    assert!(encoded.contains_key("experiences".into()).unwrap());
    assert!(!encoded.contains_key("allowed".into()).unwrap());
}
