// 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.Supplier.get(this.options[TOKEN_PARAM]);
        if (token == null) {
            throw new errors.LatitudeApiError({
                message: BearerAuthProvider.AUTH_CONFIG_ERROR_MESSAGE,
            });
        }

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

export namespace BearerAuthProvider {
    export const AUTH_SCHEME = "BearerAuthScheme" 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.Supplier<core.BearerToken> };

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