"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.FacebookAuthenticationController = void 0;
const openapi = require("@nestjs/swagger");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const facebook_oauth_helper_1 = require("../helpers/facebook-oauth.helper");
const authentication_service_1 = require("../services/authentication.service");
const setting_service_1 = require("../services/setting.service");
const public_decorator_1 = require("../decorators/public.decorator");
const auth_decorator_1 = require("../decorators/auth.decorator");
const auth_type_enum_1 = require("../enums/auth-type.enum");
const facebook_oauth_strategy_1 = require("../passport-strategies/facebook-oauth.strategy");
const user_service_1 = require("../services/user.service");
let FacebookAuthenticationController = class FacebookAuthenticationController {
    constructor(userService, authService, settingService) {
        this.userService = userService;
        this.authService = authService;
        this.settingService = settingService;
    }
    async getConfiguration() {
        return {
            clientID: await this.settingService.getConfigValue("FACEBOOK_CLIENT_ID"),
            clientSecret: await this.settingService.getConfigValue("FACEBOOK_CLIENT_SECRET"),
            callbackURL: await this.settingService.getConfigValue("FACEBOOK_CALLBACK_URL"),
            redirectURL: await this.settingService.getConfigValue("FACEBOOK_REDIRECT_URL"),
        };
    }
    async validateConfiguration() {
        const config = await this.getConfiguration();
        if (!(0, facebook_oauth_helper_1.isFacebookOAuthConfigured)(config)) {
            throw new common_1.InternalServerErrorException("Facebook OAuth is not configured");
        }
        return config;
    }
    async connect() {
        await this.validateConfiguration();
    }
    async facebookAuthCallback(req, res) {
        const config = await this.validateConfiguration();
        const user = req.user;
        return res.redirect(`${config.redirectURL}?accessCode=${user['accessCode']}`);
    }
    async dummyFacebookAuthRedirect(accessCode) {
        await this.validateConfiguration();
        const user = await this.userService.findOneByAccessCode(accessCode);
        if (user) {
            delete user['password'];
        }
        return user;
    }
    async facebookAuth(accessCode) {
        await this.validateConfiguration();
        return this.authService.signInUsingFacebook(accessCode);
    }
};
exports.FacebookAuthenticationController = FacebookAuthenticationController;
__decorate([
    (0, public_decorator_1.Public)(),
    (0, common_1.UseGuards)(facebook_oauth_strategy_1.FacebookOauthGuard),
    (0, common_1.Get)("connect"),
    openapi.ApiResponse({ status: 200 }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], FacebookAuthenticationController.prototype, "connect", null);
__decorate([
    (0, public_decorator_1.Public)(),
    (0, common_1.Get)("connect/callback"),
    (0, common_1.UseGuards)(facebook_oauth_strategy_1.FacebookOauthGuard),
    openapi.ApiResponse({ status: 200 }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], FacebookAuthenticationController.prototype, "facebookAuthCallback", null);
__decorate([
    (0, public_decorator_1.Public)(),
    (0, common_1.Get)('dummy-redirect'),
    openapi.ApiResponse({ status: 200, type: require("../entities/user.entity").User }),
    __param(0, (0, common_1.Query)('accessCode')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], FacebookAuthenticationController.prototype, "dummyFacebookAuthRedirect", null);
__decorate([
    (0, public_decorator_1.Public)(),
    (0, common_1.Get)("authenticate"),
    (0, swagger_1.ApiQuery)({ name: "accessCode", required: true, type: String }),
    openapi.ApiResponse({ status: 200 }),
    __param(0, (0, common_1.Query)("accessCode")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], FacebookAuthenticationController.prototype, "facebookAuth", null);
exports.FacebookAuthenticationController = FacebookAuthenticationController = __decorate([
    (0, auth_decorator_1.Auth)(auth_type_enum_1.AuthType.None),
    (0, swagger_1.ApiTags)("Solid Core"),
    (0, common_1.Controller)("iam/facebook"),
    __metadata("design:paramtypes", [user_service_1.UserService,
        authentication_service_1.AuthenticationService,
        setting_service_1.SettingService])
], FacebookAuthenticationController);
//# sourceMappingURL=facebook-authentication.controller.js.map