// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// {{ crate::properties::GENERATED_BY }}

#[rustfmt::skip]

#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
pub mod ffi {
    use crate::unstable::properties_gcg::ffi::GeneralCategoryGroup;
    use alloc::boxed::Box;

    #[cfg(feature = "buffer_provider")]
    use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider};

    /// A type capable of looking up a property value from a string name.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::properties::PropertyParser, Struct)]
    #[diplomat::rust_link(icu::properties::PropertyParserBorrowed, Struct)]
    #[diplomat::rust_link(icu::properties::PropertyParser::new, FnInStruct)]
    #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::new, FnInStruct, hidden)]
    #[diplomat::rust_link(
        icu::properties::props::NamedEnumeratedProperty::try_from_str,
        FnInTrait,
        hidden
    )]
    #[diplomat::attr(demo_gen, disable)] // TODO needs custom page
    pub struct PropertyValueNameToEnumMapper(icu_properties::PropertyParser<u16>);

    impl PropertyValueNameToEnumMapper {
        /// Get the property value matching the given name, using strict matching
        ///
        /// Returns -1 if the name is unknown for this property
        #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_strict, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::PropertyParserBorrowed::get_strict_u16,
            FnInStruct,
            hidden
        )]
        pub fn get_strict(&self, name: &DiplomatStr) -> i16 {
            self.0
                .as_borrowed()
                .get_strict_utf8(name)
                .map(|u_16| u_16 as i16)
                .unwrap_or(-1)
        }

        /// Get the property value matching the given name, using loose matching
        ///
        /// Returns -1 if the name is unknown for this property
        #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_loose, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::PropertyParserBorrowed::get_loose_u16,
            FnInStruct,
            hidden
        )]
        pub fn get_loose(&self, name: &DiplomatStr) -> i16 {
            self.0
                .as_borrowed()
                .get_loose_utf8(name)
                .map(|u_16| u_16 as i16)
                .unwrap_or(-1)
        }
    }

    /// A type capable of looking up General Category Group values from a string name.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::properties::PropertyParser, Struct)]
    #[diplomat::rust_link(icu::properties::props::GeneralCategory, Enum)]
    #[diplomat::attr(demo_gen, disable)] // TODO needs custom page
    pub struct GeneralCategoryNameToGroupMapper(
        icu_properties::PropertyParser<icu_properties::props::GeneralCategoryGroup>,
    );

    impl GeneralCategoryNameToGroupMapper {
        /// Get the mask value matching the given name, using strict matching
        ///
        /// Returns 0 if the name is unknown for this property
        #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_strict, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::PropertyParserBorrowed::get_strict_u16,
            FnInStruct,
            hidden
        )]
        pub fn get_strict(&self, name: &DiplomatStr) -> GeneralCategoryGroup {
            self.0
                .as_borrowed()
                .get_strict_utf8(name)
                .map(Into::into)
                .unwrap_or_default()
        }

        /// Get the mask value matching the given name, using loose matching
        ///
        /// Returns 0 if the name is unknown for this property
        #[diplomat::rust_link(icu::properties::PropertyParserBorrowed::get_loose, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::PropertyParserBorrowed::get_loose_u16,
            FnInStruct,
            hidden
        )]
        pub fn get_loose(&self, name: &DiplomatStr) -> GeneralCategoryGroup {
            self.0
                .as_borrowed()
                .get_loose_utf8(name)
                .map(Into::into)
                .unwrap_or_default()
        }
        /// Create a name-to-mask mapper for the `General_Category` property, using compiled data.
        #[diplomat::rust_link(icu::properties::props::GeneralCategoryGroup, Struct)]
        #[diplomat::attr(auto, constructor)]
        #[cfg(feature = "compiled_data")]
        pub fn create() -> Box<GeneralCategoryNameToGroupMapper> {
            Box::new(GeneralCategoryNameToGroupMapper(
                icu_properties::PropertyParser::<icu_properties::props::GeneralCategoryGroup>::new(
                )
                .static_to_owned(),
            ))
        }
        /// Create a name-to-mask mapper for the `General_Category` property, using a particular data source.
        #[diplomat::rust_link(icu::properties::props::GeneralCategoryGroup, Struct)]
        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_with_provider(
            provider: &DataProvider,
        ) -> Result<Box<GeneralCategoryNameToGroupMapper>, DataError> {
            Ok(Box::new(
                GeneralCategoryNameToGroupMapper(icu_properties::PropertyParser::<
                    icu_properties::props::GeneralCategoryGroup,
                >::try_new_unstable(
                    &provider.get_unstable()?
                )?),
            ))
        }
    }

    impl PropertyValueNameToEnumMapper {
        {%- for prop in props %}

        /// Create a name-to-enum mapper for the `{{ prop.name.to_upper_camel_case() }}` property, using compiled data.
        #[diplomat::rust_link(icu::properties::props::{{ prop.name.to_upper_camel_case() }}, {{ prop.ffi_kind() }})]
        #[diplomat::attr(auto, named_constructor = "{{ prop.name.to_snake_case() }}")]
        #[cfg(feature = "compiled_data")]
        pub fn create_{{ prop.name.to_snake_case() }}() -> Box<PropertyValueNameToEnumMapper> {
            Box::new(PropertyValueNameToEnumMapper(
                icu_properties::PropertyParser::<icu_properties::props::{{ prop.name.to_upper_camel_case() }}>::new()
                    .static_to_owned()
                    .erase(),
            ))
        }

        /// Create a name-to-enum mapper for the `{{ prop.name.to_upper_camel_case() }}` property, using a particular data source.
        #[diplomat::rust_link(icu::properties::props::{{ prop.name.to_upper_camel_case() }}, {{ prop.ffi_kind() }})]
        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "{{ prop.name.to_snake_case() }}_with_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_{{ prop.name.to_snake_case() }}_with_provider(
            provider: &DataProvider,
        ) -> Result<Box<PropertyValueNameToEnumMapper>, DataError> {
            Ok(Box::new(PropertyValueNameToEnumMapper(
                icu_properties::PropertyParser::<
                                    icu_properties::props::{{ prop.name.to_upper_camel_case() }},
                                >::try_new_unstable(&provider.get_unstable()?)?
                    .erase(),
            )))
        }

        {%- endfor %}
    }
}

