"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 GoogleOauthStrategy_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleOauthStrategy = exports.GoogleOauthGuard = void 0;
const common_1 = require("@nestjs/common");
const passport_1 = require("@nestjs/passport");
const passport_google_oauth2_1 = require("passport-google-oauth2");
const google_oauth_helper_1 = require("../helpers/google-oauth.helper");
const uuid_1 = require("uuid");
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 GoogleOauthGuard = class GoogleOauthGuard extends (0, passport_1.AuthGuard)('google') {
};
exports.GoogleOauthGuard = GoogleOauthGuard;
exports.GoogleOauthGuard = GoogleOauthGuard = __decorate([
    (0, common_1.Injectable)()
], GoogleOauthGuard);
let GoogleOauthStrategy = GoogleOauthStrategy_1 = class GoogleOauthStrategy extends (0, passport_1.PassportStrategy)(passport_google_oauth2_1.Strategy, 'google') {
    constructor(userService) {
        const clientID = process.env.IAM_GOOGLE_OAUTH_CLIENT_ID ?? DUMMY_CLIENT_ID;
        const clientSecret = process.env.IAM_GOOGLE_OAUTH_CLIENT_SECRET ?? DUMMY_CLIENT_SECRET;
        const callbackURL = process.env.IAM_GOOGLE_OAUTH_CALLBACK_URL ?? DUMMY_CALLBACK_URL;
        const redirectURL = process.env.IAM_GOOGLE_OAUTH_REDIRECT_URL;
        super({ clientID, clientSecret, callbackURL, scope: ['profile', 'email'] });
        this.userService = userService;
        this.logger = new common_1.Logger(GoogleOauthStrategy_1.name);
        const googleOauth = { clientID, clientSecret, callbackURL, redirectURL };
        if (!(0, google_oauth_helper_1.isGoogleOAuthConfigured)(googleOauth)) {
            this.logger.debug('Google OAuth strategy is not configured');
        }
    }
    async validate(_accessToken, _refreshToken, profile, done) {
        const { id, name, emails, photos } = profile;
        const loginAccessCode = (0, uuid_1.v4)();
        const user = {
            provider: 'google',
            providerId: id,
            email: emails[0].value,
            name: `${name.givenName} ${name.familyName}`,
            picture: photos[0].value,
            accessCode: loginAccessCode,
        };
        await this.userService.resolveUserOnOauthGoogle({ ...user, accessToken: _accessToken, refreshToken: null });
        done(null, user);
    }
};
exports.GoogleOauthStrategy = GoogleOauthStrategy;
exports.GoogleOauthStrategy = GoogleOauthStrategy = GoogleOauthStrategy_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [user_service_1.UserService])
], GoogleOauthStrategy);
//# sourceMappingURL=google-oauth.strategy.js.map