// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.CompilerServices;

#pragma warning disable SA1649 // File name must match first type name

namespace OpenTelemetry.AutoInstrumentation.CallTarget.Handlers.Continuations;

internal struct NoThrowAwaiter : ICriticalNotifyCompletion
{
    private readonly Task _task;
    private readonly bool _preserveContext;

    public NoThrowAwaiter(Task task, bool preserveContext)
    {
        _task = task;
        _preserveContext = preserveContext;
    }

    public bool IsCompleted => _task.IsCompleted;

    public NoThrowAwaiter GetAwaiter() => this;

    public void GetResult()
    {
    }

    public void OnCompleted(Action continuation) => _task.ConfigureAwait(_preserveContext).GetAwaiter().OnCompleted(continuation);

    public void UnsafeOnCompleted(Action continuation) => OnCompleted(continuation);
}
