Min<TSource>
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Reactive.Linq.ObservableImpl
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(new byte[] {
        0,
        1,
        1,
        1
    })]
    internal sealed class Min<[System.Runtime.CompilerServices.Nullable(2)] TSource> : Producer<TSource, Min<TSource>._>
    {
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1
        })]
        internal abstract class _ : IdentitySink<TSource>
        {
            protected readonly IComparer<TSource> _comparer;
            protected _(IComparer<TSource> comparer, IObserver<TSource> observer)
                : base(observer)
            {
                _comparer = comparer;
            }
        }
        [System.Runtime.CompilerServices.Nullable(0)]
        private sealed class NonNull : _
        {
            private bool _hasValue;
            [System.Runtime.CompilerServices.Nullable(2)]
            private TSource _lastValue;
            public NonNull(IComparer<TSource> comparer, IObserver<TSource> observer)
                : base(comparer, observer)
            {
            }
            public override void OnNext(TSource value)
            {
                if (_hasValue) {
                    int num;
                    try {
                        num = _comparer.Compare(value, _lastValue);
                    } catch (Exception error) {
                        ForwardOnError(error);
                        return;
                    }
                    if (num < 0)
                        _lastValue = value;
                } else {
                    _hasValue = true;
                    _lastValue = value;
                }
            }
            public override void OnError(Exception error)
            {
                ForwardOnError(error);
            }
            public override void OnCompleted()
            {
                if (!_hasValue)
                    try {
                        throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
                    } catch (Exception error) {
                        ForwardOnError(error);
                    }
                else {
                    ForwardOnNext(_lastValue);
                    ForwardOnCompleted();
                }
            }
        }
        [System.Runtime.CompilerServices.Nullable(0)]
        private sealed class Null : _
        {
            [System.Runtime.CompilerServices.Nullable(2)]
            private TSource _lastValue;
            public Null(IComparer<TSource> comparer, IObserver<TSource> observer)
                : base(comparer, observer)
            {
            }
            public override void OnNext(TSource value)
            {
                if (value != null) {
                    if (_lastValue == null)
                        _lastValue = value;
                    else {
                        int num;
                        try {
                            num = _comparer.Compare(value, _lastValue);
                        } catch (Exception error) {
                            ForwardOnError(error);
                            return;
                        }
                        if (num < 0)
                            _lastValue = value;
                    }
                }
            }
            public override void OnCompleted()
            {
                ForwardOnNext(_lastValue);
                ForwardOnCompleted();
            }
        }
        private readonly IObservable<TSource> _source;
        private readonly IComparer<TSource> _comparer;
        public Min(IObservable<TSource> source, IComparer<TSource> comparer)
        {
            _source = source;
            _comparer = comparer;
        }
        [return: System.Runtime.CompilerServices.Nullable(new byte[] {
            1,
            0
        })]
        protected override _ CreateSink(IObserver<TSource> observer)
        {
            if (default(TSource) != null)
                return new NonNull(_comparer, observer);
            return new Null(_comparer, observer);
        }
        protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
            1,
            0
        })] _ sink)
        {
            sink.Run(_source);
        }
    }
}