"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 FacebookOAuthStrategy_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FacebookOAuthStrategy = exports.FacebookOauthGuard = void 0;
const common_1 = require("@nestjs/common");
const passport_1 = require("@nestjs/passport");
const passport_facebook_1 = require("passport-facebook");
const facebook_oauth_helper_1 = require("../helpers/facebook-oauth.helper");
const uuid_1 = require("uuid");
const setting_service_1 = require("../services/setting.service");
const user_service_1 = require("../services/user.service");
const DUMMY_CLIENT_ID = "DUMMY_CLIENT_ID";
const DUMMY_CLIENT_SECRET = "DUMMY_CLIENT_SECRET";
const DUMMY_CALLBACK_URL = "DUMMY_CALLBACK_URL";
let FacebookOauthGuard = class FacebookOauthGuard extends (0, passport_1.AuthGuard)("facebook") {
};
exports.FacebookOauthGuard = FacebookOauthGuard;
exports.FacebookOauthGuard = FacebookOauthGuard = __decorate([
    (0, common_1.Injectable)()
], FacebookOauthGuard);
let FacebookOAuthStrategy = FacebookOAuthStrategy_1 = class FacebookOAuthStrategy extends (0, passport_1.PassportStrategy)(passport_facebook_1.Strategy, "facebook") {
    constructor(userService, settingService) {
        const clientID = settingService.getConfigValue("FACEBOOK_CLIENT_ID") ??
            process.env.IAM_FACEBOOK_OAUTH_CLIENT_ID ??
            DUMMY_CLIENT_ID;
        const clientSecret = settingService.getConfigValue("FACEBOOK_CLIENT_SECRET") ??
            process.env.IAM_FACEBOOK_OAUTH_CLIENT_SECRET ??
            DUMMY_CLIENT_SECRET;
        const callbackURL = settingService.getConfigValue("FACEBOOK_CALLBACK_URL") ??
            process.env.IAM_FACEBOOK_OAUTH_CALLBACK_URL ??
            DUMMY_CALLBACK_URL;
        const redirectURL = settingService.getConfigValue("FACEBOOK_REDIRECT_URL") ?? process.env.IAM_FACEBOOK_OAUTH_REDIRECT_URL;
        super({
            clientID,
            clientSecret,
            callbackURL,
            scope: ["email"],
            profileFields: ["id", "name", "email", "picture.type(large)"],
        });
        this.userService = userService;
        this.settingService = settingService;
        this.logger = new common_1.Logger(FacebookOAuthStrategy_1.name);
        const facebookOauth = {
            clientID,
            clientSecret,
            callbackURL,
            redirectURL,
        };
        if (!(0, facebook_oauth_helper_1.isFacebookOAuthConfigured)(facebookOauth)) {
            this.logger.debug("Facebook OAuth strategy is not configured");
        }
        else {
            this.logger.debug(`Facebook OAuth configured with callbackURL=${callbackURL}`);
        }
    }
    async validate(_accessToken, _refreshToken, profile, done) {
        const { id, name, emails, photos } = profile;
        const loginAccessCode = (0, uuid_1.v4)();
        const email = emails && emails.length > 0 ? emails[0].value : null;
        const firstName = name?.givenName || "";
        const lastName = name?.familyName || "";
        const fullName = firstName || lastName
            ? `${firstName} ${lastName}`.trim()
            : profile.displayName;
        const user = {
            provider: "facebook",
            providerId: id,
            email: email,
            name: `${name.givenName} ${name.familyName}`,
            picture: photos?.[0]?.value,
            accessCode: loginAccessCode,
        };
        await this.userService.resolveUserOnOauthFacebook({
            ...user,
            accessToken: _accessToken,
            refreshToken: null,
        });
        done(null, user);
    }
};
exports.FacebookOAuthStrategy = FacebookOAuthStrategy;
exports.FacebookOAuthStrategy = FacebookOAuthStrategy = FacebookOAuthStrategy_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [user_service_1.UserService,
        setting_service_1.SettingService])
], FacebookOAuthStrategy);
//# sourceMappingURL=facebook-oauth.strategy.js.map