// 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";

export namespace BearerAuthProvider {
    export interface Options {
        token?: core.Supplier<core.BearerToken | undefined>;
    }
}

export class BearerAuthProvider implements core.AuthProvider {
    private readonly token: core.Supplier<core.BearerToken | undefined> | undefined;

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

    public static canCreate(options: BearerAuthProvider.Options): boolean {
        return options.token != null || process.env?.INTERCOM_API_KEY != null;
    }

    public async getAuthRequest(_arg?: { endpointMetadata?: core.EndpointMetadata }): Promise<core.AuthRequest> {
        const token = (await core.Supplier.get(this.token)) ?? process.env?.INTERCOM_API_KEY;
        if (token == null) {
            throw new errors.IntercomError({
                message:
                    "Please specify a token by either passing it in to the constructor or initializing a INTERCOM_API_KEY environment variable",
            });
        }

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