const teamsfxSdk = require("@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 teamsfxSdk.TeamsFx(teamsfxSdk.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 teamsfxSdk.TeamsFx(teamsfxSdk.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 teamsfxSdk.BearerTokenAuthProvider(
  // TODO: Replace '<your-api-scope>' with your required API scope
  async () => (await appCredential.getToken("<your-api-scope>")).token
);
const {{config.APIName}}Client = teamsfxSdk.createApiClient(
  process.env.TEAMSFX_API_{{capitalName}}_ENDPOINT,
  authProvider
);
module.exports.{{config.APIName}}Client = {{config.APIName}}Client;
