import {
  TeamsFx,
  createApiClient,
  BearerTokenAuthProvider,
  IdentityType,
} from "@microsoft/teamsfx";

// Load application configuration. This sample uses the client credential flow to acquire a token for your API.
{{#if (equals config.AuthConfig.ReuseTeamsApp true)}}
const teamsFx = new TeamsFx(IdentityType.App, {
  authorityHost: process.env.M365_AUTHORITY_HOST!,
  tenantId: process.env.M365_TENANT_ID!,
  clientId: process.env.M365_CLIENT_ID!,
  clientSecret: process.env.M365_CLIENT_SECRET!,
});
{{else}}
const teamsFx = new TeamsFx(IdentityType.App, {
  // You can replace the default authorityHost URL
  authorityHost: "https://login.microsoftonline.com",
  tenantId: process.env.TEAMSFX_API_{{capitalName}}_TENANT_ID!,
  clientId: process.env.TEAMSFX_API_{{capitalName}}_CLIENT_ID!,
  // This references the client secret that you must add in the file `.env.teamsfx.local`.
  clientSecret: process.env.TEAMSFX_API_{{capitalName}}_CLIENT_SECRET!,
});
{{/if}}
// Initialize a new axios instance to call {{config.APIName}}
const appCredential = teamsFx.getCredential();
const authProvider = new BearerTokenAuthProvider(
  // TODO: Replace '<your-api-scope>' with your required API scope
  async () => (await appCredential.getToken("<your-api-scope>"))!.token
);
const {{config.APIName}}Client = createApiClient(
  process.env.TEAMSFX_API_{{capitalName}}_ENDPOINT,
  authProvider
);
export { {{config.APIName}}Client };
