"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.FacebookProvider = 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 FacebookProvider = class FacebookProvider extends abstract_provider_1.BaseOAuthProvider {
    constructor(http, setting) {
        super(http);
        this.setting = setting;
    }
    getProviderType() {
        return 'FACEBOOK';
    }
    async getAuthUrl(callbackPath) {
        var _a;
        const settings = await this.setting.getSettingValues([
            'facebook_client_id',
            'facebook_client_secret',
            'facebook_scopes',
            'url',
        ]);
        const redirectURI = new URL(callbackPath, settings['url']).toString();
        const params = new URLSearchParams({
            client_id: settings['facebook_client_id'],
            redirect_uri: redirectURI,
            response_type: 'code',
            scope: ((_a = settings['facebook_scopes']) !== null && _a !== void 0 ? _a : ['email']).join(','),
            auth_type: 'rerequest',
        });
        return `https://www.facebook.com/v17.0/dialog/oauth?${params}`;
    }
    async getProfile(code, type) {
        const settings = await this.setting.getSettingValues([
            'facebook_client_id',
            'facebook_client_secret',
            'facebook_scopes',
            'url',
        ]);
        const token = await this.fetchToken({
            code,
            url: 'https://graph.facebook.com/v17.0/oauth/access_token',
            clientId: settings['facebook_client_id'],
            clientSecret: settings['facebook_client_secret'],
            redirectUri: `${settings['url']}/oauth/facebook/callback/${type}`,
        });
        const profile = await this.fetchProfile(token.access_token, 'https://graph.facebook.com/me?fields=id,name,email,gender,birthday,picture');
        return Object.assign(Object.assign({}, profile), { oauth_tokens: {
                access_token: token.access_token,
                refresh_token: token.refresh_token,
                expires_in: token.expires_in,
                token_type: token.token_type,
            } });
    }
};
exports.FacebookProvider = FacebookProvider;
exports.FacebookProvider = FacebookProvider = __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])
], FacebookProvider);
//# sourceMappingURL=facebook.provider.js.map