// This file was auto-generated by Fern from our API Definition.

import * as core from "../core/index.js";
import * as errors from "../errors/index.js";

const TOKEN_PARAM = "token" as const;

export class BearerAuthProvider implements core.AuthProvider {
    private readonly options: BearerAuthProvider.Options;

    constructor(options: BearerAuthProvider.Options) {
        this.options = options;
    }

    public static canCreate(options: Partial<BearerAuthProvider.Options>): boolean {
        return options?.[TOKEN_PARAM] != null;
    }

    public async getAuthRequest({
        endpointMetadata,
    }: {
        endpointMetadata?: core.EndpointMetadata;
    } = {}): Promise<core.AuthRequest> {
        const token = await core.EndpointSupplier.get(this.options[TOKEN_PARAM], { endpointMetadata });
        if (token == null) {
            throw new errors.CoinbaseApiError({
                message: BearerAuthProvider.AUTH_CONFIG_ERROR_MESSAGE,
            });
        }

        return {
            headers: { Authorization: `Bearer ${token}` },
        };
    }
}

export namespace BearerAuthProvider {
    export const AUTH_SCHEME = "apiKeyAuth" as const;
    export const AUTH_CONFIG_ERROR_MESSAGE: string =
        `Please provide '${TOKEN_PARAM}' when initializing the client` as const;
    export type Options = AuthOptions;
    export type AuthOptions = { [TOKEN_PARAM]: core.EndpointSupplier<core.BearerToken> };

    export function createInstance(options: Options): core.AuthProvider {
        return new BearerAuthProvider(options);
    }
}
