"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MicrosoftProvider = void 0;
const axios_1 = require("@nestjs/axios");
const common_1 = require("@nestjs/common");
const setting_service_1 = require("../../setting/setting.service");
const abstract_provider_1 = require("./abstract.provider");
let MicrosoftProvider = class MicrosoftProvider extends abstract_provider_1.BaseOAuthProvider {
    constructor(http, setting) {
        super(http);
        this.setting = setting;
    }
    getProviderType() {
        return 'MICROSOFT';
    }
    async getAuthUrl(callbackPath) {
        const settings = await this.setting.getSettingValues([
            'microsoft_client_id',
            'microsoft_client_secret',
            'microsoft_scopes',
            'url',
        ]);
        const redirectURI = new URL(callbackPath, settings['url']).toString();
        const scopes = settings['microsoft_scopes'];
        const params = new URLSearchParams({
            client_id: settings['microsoft_client_id'],
            redirect_uri: redirectURI,
            response_type: 'code',
            scope: scopes.join(' '),
            response_mode: 'query',
            prompt: 'consent',
        });
        return `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${params}`;
    }
    async getProfile(code, type) {
        const settings = await this.setting.getSettingValues([
            'microsoft_client_id',
            'microsoft_client_secret',
            'microsoft_scopes',
            'url',
        ]);
        const token = await this.fetchToken({
            code,
            url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
            clientId: settings['microsoft_client_id'],
            clientSecret: settings['microsoft_client_secret'],
            redirectUri: `${settings['url']}/callback/microsoft/${type}`,
        });
        const profile = await this.fetchProfile(token.access_token, 'https://graph.microsoft.com/v1.0/me');
        const pictureUrl = 'https://graph.microsoft.com/v1.0/me/photo/$value';
        return {
            id: profile.id,
            email: profile.mail || profile.userPrincipalName,
            name: profile.displayName,
            picture: pictureUrl,
            oauth_tokens: {
                access_token: token.access_token,
                refresh_token: token.refresh_token,
                expires_in: token.expires_in,
                token_type: token.token_type,
            },
        };
    }
};
exports.MicrosoftProvider = MicrosoftProvider;
exports.MicrosoftProvider = MicrosoftProvider = __decorate([
    (0, common_1.Injectable)(),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => setting_service_1.SettingService))),
    __metadata("design:paramtypes", [axios_1.HttpService,
        setting_service_1.SettingService])
], MicrosoftProvider);
//# sourceMappingURL=microsoft.provider.js.map