// generated by diplomat-tool
// dart format off

part of 'lib.g.dart';

/// An ICU4X Locale, capable of representing strings like `"en-US"`.
///
/// See the [Rust documentation for `Locale`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html) for more information.
final class Locale implements ffi.Finalizable, core.Comparable<Locale> {
  final ffi.Pointer<ffi.Opaque> _ffi;

  // These are "used" in the sense that they keep dependencies alive
  // ignore: unused_field
  final core.List<Object> _selfEdge;

  // This takes in a list of lifetime edges (including for &self borrows)
  // corresponding to data this may borrow from. These should be flat arrays containing
  // references to objects, and this object will hold on to them to keep them alive and
  // maintain borrow validity.
  Locale._fromFfi(this._ffi, this._selfEdge) {
    if (_selfEdge.isEmpty) {
      _icu4x_Locale_destroy_mv1(this, _ffi.cast());
    }
  }

  // ignore: experimental_member_use
  @meta.RecordUse()
  // ignore: non_constant_identifier_names
  static void _icu4x_Locale_destroy_mv1(Locale cl, ffi.Pointer<ffi.Void> pointer) => _finalizer.attach(cl, pointer);

  static final _finalizer = ffi.NativeFinalizer(ffi.Native.addressOf(_internal_icu4x_Locale_destroy_mv1));

  /// Construct an [Locale] from an locale identifier.
  ///
  /// This will run the complete locale parsing algorithm. If code size and
  /// performance are critical and the locale is of a known shape (such as
  /// `aa-BB`) use `create_und`, `set_language`, `set_script`, and `set_region`.
  ///
  /// See the [Rust documentation for `try_from_str`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.try_from_str) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  factory Locale.fromString(String name) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_from_string_mv1(name._utf8AllocIn(temp.arena));
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
    return Locale._fromFfi(result.union.ok, []);
  }

  /// Construct a unknown [Locale] "und".
  ///
  /// See the [Rust documentation for `UNKNOWN`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#associatedconstant.UNKNOWN) for more information.
  factory Locale.unknown() {
    final result = _icu4x_Locale_unknown_mv1();
    return Locale._fromFfi(result, []);
  }

  /// Clones the [Locale].
  ///
  /// See the [Rust documentation for `Locale`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html) for more information.
  Locale clone() {
    final result = _icu4x_Locale_clone_mv1(_ffi);
    return Locale._fromFfi(result, []);
  }

  /// Returns a string representation of the `LanguageIdentifier` part of
  /// [Locale].
  ///
  /// See the [Rust documentation for `id`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.id) for more information.
  String get basename {
    final write = _Write();
    _icu4x_Locale_basename_mv1(_ffi, write._ffi);
    return write.finalize();
  }

  /// Returns a string representation of the unicode extension.
  ///
  /// See the [Rust documentation for `extensions`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.extensions) for more information.
  String? getUnicodeExtension(String s) {
    final temp = _FinalizedArena();
    final write = _Write();
    final result = _icu4x_Locale_get_unicode_extension_mv1(_ffi, s._utf8AllocIn(temp.arena), write._ffi);
    if (!result.isOk) {
      return null;
    }
    return write.finalize();
  }

  /// Set a Unicode extension.
  ///
  /// See the [Rust documentation for `extensions`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.extensions) for more information.
  bool setUnicodeExtension(String k, String v) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_set_unicode_extension_mv1(_ffi, k._utf8AllocIn(temp.arena), v._utf8AllocIn(temp.arena));
    return result.isOk;
  }

  /// Returns a string representation of [Locale] language.
  ///
  /// See the [Rust documentation for `id`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.id) for more information.
  String get language {
    final write = _Write();
    _icu4x_Locale_language_mv1(_ffi, write._ffi);
    return write.finalize();
  }

  /// Set the language part of the [Locale].
  ///
  /// See the [Rust documentation for `try_from_str`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.try_from_str) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  set language(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_set_language_mv1(_ffi, s._utf8AllocIn(temp.arena));
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
  }

  /// Returns a string representation of [Locale] region.
  ///
  /// See the [Rust documentation for `id`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.id) for more information.
  String? get region {
    final write = _Write();
    final result = _icu4x_Locale_region_mv1(_ffi, write._ffi);
    if (!result.isOk) {
      return null;
    }
    return write.finalize();
  }

  /// Set the region part of the [Locale].
  ///
  /// See the [Rust documentation for `try_from_str`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.try_from_str) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  void setRegion(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_set_region_mv1(_ffi, s._utf8AllocIn(temp.arena));
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
  }

  /// Returns a string representation of [Locale] script.
  ///
  /// See the [Rust documentation for `id`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#structfield.id) for more information.
  String? get script {
    final write = _Write();
    final result = _icu4x_Locale_script_mv1(_ffi, write._ffi);
    if (!result.isOk) {
      return null;
    }
    return write.finalize();
  }

  /// Set the script part of the [Locale]. Pass an empty string to remove the script.
  ///
  /// See the [Rust documentation for `try_from_str`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.try_from_str) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  void setScript(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_set_script_mv1(_ffi, s._utf8AllocIn(temp.arena));
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
  }

  /// Returns a string representation of the [Locale] variants.
  ///
  /// See the [Rust documentation for `Variants`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html) for more information.
  String variants() {
    final write = _Write();
    _icu4x_Locale_variants_mv1(_ffi, write._ffi);
    return write.finalize();
  }

  /// Returns the number of variants in this [Locale].
  ///
  /// See the [Rust documentation for `Variants`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html) for more information.
  int get variantCount {
    final result = _icu4x_Locale_variant_count_mv1(_ffi);
    return result;
  }

  /// Returns the variant at the given index, or nothing if the index is out of bounds.
  ///
  /// See the [Rust documentation for `Variants`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html) for more information.
  String? variantAt(int index) {
    final write = _Write();
    final result = _icu4x_Locale_variant_at_mv1(_ffi, index, write._ffi);
    if (!result.isOk) {
      return null;
    }
    return write.finalize();
  }

  /// Returns whether the [Locale] has a specific variant.
  ///
  /// See the [Rust documentation for `Variants`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html) for more information.
  bool hasVariant(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_has_variant_mv1(_ffi, s._utf8AllocIn(temp.arena));
    return result;
  }

  /// Adds a variant to the [Locale].
  ///
  /// Returns an error if the variant string is invalid.
  /// Returns `true` if the variant was added, `false` if already present.
  ///
  /// See the [Rust documentation for `push`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html#method.push) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  bool addVariant(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_add_variant_mv1(_ffi, s._utf8AllocIn(temp.arena));
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
    return result.union.ok;
  }

  /// Removes a variant from the [Locale].
  ///
  /// Returns `true` if the variant was removed, `false` if not present.
  /// Returns `false` for invalid variant strings (they cannot exist in the locale).
  ///
  /// See the [Rust documentation for `remove`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html#method.remove) for more information.
  bool removeVariant(String s) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_remove_variant_mv1(_ffi, s._utf8AllocIn(temp.arena));
    return result;
  }

  /// Clears all variants from the [Locale].
  ///
  /// See the [Rust documentation for `clear`](https://docs.rs/icu/2.2.0/icu/locale/struct.Variants.html#method.clear) for more information.
  void clearVariants() {
    _icu4x_Locale_clear_variants_mv1(_ffi);
  }

  /// Normalizes a locale string.
  ///
  /// See the [Rust documentation for `normalize`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.normalize) for more information.
  ///
  /// Throws [LocaleParseError] on failure.
  static String normalize(String s) {
    final temp = _FinalizedArena();
    final write = _Write();
    final result = _icu4x_Locale_normalize_mv1(s._utf8AllocIn(temp.arena), write._ffi);
    if (!result.isOk) {
      throw LocaleParseError.values[result.union.err];
    }
    return write.finalize();
  }

  /// Returns a string representation of [Locale].
  ///
  /// See the [Rust documentation for `write_to`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.write_to) for more information.
  @override
  String toString() {
    final write = _Write();
    _icu4x_Locale_to_string_mv1(_ffi, write._ffi);
    return write.finalize();
  }

  /// See the [Rust documentation for `normalizing_eq`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.normalizing_eq) for more information.
  bool normalizingEq(String other) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_normalizing_eq_mv1(_ffi, other._utf8AllocIn(temp.arena));
    return result;
  }

  /// See the [Rust documentation for `strict_cmp`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.strict_cmp) for more information.
  int compareToString(String other) {
    final temp = _FinalizedArena();
    final result = _icu4x_Locale_compare_to_string_mv1(_ffi, other._utf8AllocIn(temp.arena));
    return result;
  }

  /// See the [Rust documentation for `total_cmp`](https://docs.rs/icu/2.2.0/icu/locale/struct.Locale.html#method.total_cmp) for more information.
  @override
  int compareTo(Locale other) {
    final result = _icu4x_Locale_compare_to_mv1(_ffi, other._ffi);
    return result;
  }


  @override
  bool operator ==(Object other) => other is Locale && compareTo(other) == 0;
  @override
  int get hashCode => 42; // Cannot get hash from Rust, so a constant is the only correct impl
}

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(isLeaf: true, symbol: 'icu4x_Locale_destroy_mv1')
// ignore: non_constant_identifier_names
external void _internal_icu4x_Locale_destroy_mv1(ffi.Pointer<ffi.Void> self);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultOpaqueInt32 Function(_SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_from_string_mv1')
// ignore: non_constant_identifier_names
external _ResultOpaqueInt32 _icu4x_Locale_from_string_mv1(_SliceUtf8 name);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Pointer<ffi.Opaque> Function()>(isLeaf: true, symbol: 'icu4x_Locale_unknown_mv1')
// ignore: non_constant_identifier_names
external ffi.Pointer<ffi.Opaque> _icu4x_Locale_unknown_mv1();

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Pointer<ffi.Opaque> Function(ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_clone_mv1')
// ignore: non_constant_identifier_names
external ffi.Pointer<ffi.Opaque> _icu4x_Locale_clone_mv1(ffi.Pointer<ffi.Opaque> self);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_basename_mv1')
// ignore: non_constant_identifier_names
external void _icu4x_Locale_basename_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidVoid Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_get_unicode_extension_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidVoid _icu4x_Locale_get_unicode_extension_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidVoid Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_set_unicode_extension_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidVoid _icu4x_Locale_set_unicode_extension_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 k, _SliceUtf8 v);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_language_mv1')
// ignore: non_constant_identifier_names
external void _icu4x_Locale_language_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidInt32 Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_set_language_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidInt32 _icu4x_Locale_set_language_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidVoid Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_region_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidVoid _icu4x_Locale_region_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidInt32 Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_set_region_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidInt32 _icu4x_Locale_set_region_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidVoid Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_script_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidVoid _icu4x_Locale_script_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidInt32 Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_set_script_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidInt32 _icu4x_Locale_set_script_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_variants_mv1')
// ignore: non_constant_identifier_names
external void _icu4x_Locale_variants_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Size Function(ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_variant_count_mv1')
// ignore: non_constant_identifier_names
external int _icu4x_Locale_variant_count_mv1(ffi.Pointer<ffi.Opaque> self);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidVoid Function(ffi.Pointer<ffi.Opaque>, ffi.Size, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_variant_at_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidVoid _icu4x_Locale_variant_at_mv1(ffi.Pointer<ffi.Opaque> self, int index, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Bool Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_has_variant_mv1')
// ignore: non_constant_identifier_names
external bool _icu4x_Locale_has_variant_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultBoolInt32 Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_add_variant_mv1')
// ignore: non_constant_identifier_names
external _ResultBoolInt32 _icu4x_Locale_add_variant_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Bool Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_remove_variant_mv1')
// ignore: non_constant_identifier_names
external bool _icu4x_Locale_remove_variant_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 s);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_clear_variants_mv1')
// ignore: non_constant_identifier_names
external void _icu4x_Locale_clear_variants_mv1(ffi.Pointer<ffi.Opaque> self);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<_ResultVoidInt32 Function(_SliceUtf8, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_normalize_mv1')
// ignore: non_constant_identifier_names
external _ResultVoidInt32 _icu4x_Locale_normalize_mv1(_SliceUtf8 s, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_to_string_mv1')
// ignore: non_constant_identifier_names
external void _icu4x_Locale_to_string_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> write);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Bool Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_normalizing_eq_mv1')
// ignore: non_constant_identifier_names
external bool _icu4x_Locale_normalizing_eq_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 other);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Int8 Function(ffi.Pointer<ffi.Opaque>, _SliceUtf8)>(isLeaf: true, symbol: 'icu4x_Locale_compare_to_string_mv1')
// ignore: non_constant_identifier_names
external int _icu4x_Locale_compare_to_string_mv1(ffi.Pointer<ffi.Opaque> self, _SliceUtf8 other);

// ignore: experimental_member_use
@meta.RecordUse()
@ffi.Native<ffi.Int8 Function(ffi.Pointer<ffi.Opaque>, ffi.Pointer<ffi.Opaque>)>(isLeaf: true, symbol: 'icu4x_Locale_compare_to_mv1')
// ignore: non_constant_identifier_names
external int _icu4x_Locale_compare_to_mv1(ffi.Pointer<ffi.Opaque> self, ffi.Pointer<ffi.Opaque> other);

// dart format on
