// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AutoRefreshTokenCredential, } from "./autoRefreshTokenCredential";
import { StaticTokenCredential } from "./staticTokenCredential";
import { parseToken } from "./tokenParser";
/**
 * The CommunicationTokenCredential implementation with support for proactive token refresh.
 */
export class AzureCommunicationTokenCredential {
    constructor(tokenOrRefreshOptions) {
        this.disposed = false;
        if (typeof tokenOrRefreshOptions === "string") {
            this.tokenCredential = new StaticTokenCredential(parseToken(tokenOrRefreshOptions));
        }
        else {
            this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptions);
        }
    }
    /**
     * Gets an `AccessToken` for the user. Throws if already disposed.
     * @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
     */
    async getToken(options) {
        this.throwIfDisposed();
        const token = await this.tokenCredential.getToken(options);
        this.throwIfDisposed();
        return token;
    }
    /**
     * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
     */
    dispose() {
        this.disposed = true;
        this.tokenCredential.dispose();
    }
    throwIfDisposed() {
        if (this.disposed) {
            throw new Error("User credential is disposed");
        }
    }
}
//# sourceMappingURL=azureCommunicationTokenCredential.js.map