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

part of 'lib.g.dart';

/// An ICU4X grapheme-cluster-break segmenter, capable of finding grapheme cluster breakpoints
/// in strings.
///
/// See the [Rust documentation for `GraphemeClusterSegmenter`](https://docs.rs/icu/2.2.0/icu/segmenter/struct.GraphemeClusterSegmenter.html) for more information.
final class GraphemeClusterSegmenter implements ffi.Finalizable {
  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.
  GraphemeClusterSegmenter._fromFfi(this._ffi, this._selfEdge) {
    if (_selfEdge.isEmpty) {
      _icu4x_GraphemeClusterSegmenter_destroy_mv1(this, _ffi.cast());
    }
  }

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

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

  /// Construct an [GraphemeClusterSegmenter] using compiled data.
  ///
  /// See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.new) for more information.
  factory GraphemeClusterSegmenter() {
    final result = _icu4x_GraphemeClusterSegmenter_create_mv1();
    return GraphemeClusterSegmenter._fromFfi(result, []);
  }

  /// Construct an [GraphemeClusterSegmenter].
  ///
  /// See the [Rust documentation for `new`](https://docs.rs/icu/2.2.0/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.new) for more information.
  ///
  /// Throws [DataError] on failure.
  factory GraphemeClusterSegmenter.withProvider(DataProvider provider) {
    final result = _icu4x_GraphemeClusterSegmenter_create_with_provider_mv1(provider._ffi);
    if (!result.isOk) {
      throw DataError.values[result.union.err];
    }
    return GraphemeClusterSegmenter._fromFfi(result.union.ok, []);
  }

  /// Segments a string.
  ///
  /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
  /// to the WHATWG Encoding Standard.
  ///
  /// See the [Rust documentation for `segment_utf16`](https://docs.rs/icu/2.2.0/icu/segmenter/struct.GraphemeClusterSegmenterBorrowed.html#method.segment_utf16) for more information.
  GraphemeClusterBreakIteratorUtf16 segment(String input) {
    final inputArena = _FinalizedArena();
    // This lifetime edge depends on lifetimes: 'a
    final aEdges = [this, inputArena];
    final result = _icu4x_GraphemeClusterSegmenter_segment_utf16_mv1(_ffi, input._utf16AllocIn(inputArena.arena));
    return GraphemeClusterBreakIteratorUtf16._fromFfi(result, [], aEdges);
  }

}

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

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

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

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

// dart format on
