import UserMiddleware from "../Middleware/UserAuthorization";
import UserWebAuthnService from "../Services/UserWebAuthnService";
import BaseAPI from "./BaseAPI";
import UserWebAuthn from "../../Models/DatabaseModels/UserWebAuthn";
import BadDataException from "../../Types/Exception/BadDataException";
import Response from "../Utils/Response";
import CommonAPI from "./CommonAPI";
export default class UserWebAuthnAPI extends BaseAPI {
    constructor() {
        var _a, _b, _c;
        super(UserWebAuthn, UserWebAuthnService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/generate-registration-options`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            try {
                const userId = req.userAuthorization
                    .userId;
                const result = await UserWebAuthnService.generateRegistrationOptions({
                    userId: userId,
                });
                return Response.sendJsonObjectResponse(req, res, result);
            }
            catch (err) {
                next(err);
            }
        });
        this.router.post(`${(_b = new this.entityType().getCrudApiPath()) === null || _b === void 0 ? void 0 : _b.toString()}/verify-registration`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            try {
                const data = req.body;
                const databaseProps = await CommonAPI.getDatabaseCommonInteractionProps(req);
                const credential = data["credential"];
                const name = data["name"];
                await UserWebAuthnService.verifyRegistration({
                    credential: credential,
                    name: name,
                    props: databaseProps,
                });
                return Response.sendEmptySuccessResponse(req, res);
            }
            catch (err) {
                next(err);
            }
        });
        this.router.post(`${(_c = new this.entityType().getCrudApiPath()) === null || _c === void 0 ? void 0 : _c.toString()}/generate-authentication-options`, async (req, res, next) => {
            try {
                const data = req.body["data"];
                if (!data) {
                    throw new BadDataException("Data is required");
                }
                const email = data["email"];
                if (!email) {
                    throw new BadDataException("Email is required");
                }
                const result = await UserWebAuthnService.generateAuthenticationOptions({
                    email: email,
                });
                return Response.sendJsonObjectResponse(req, res, result);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
//# sourceMappingURL=UserWebAuthnAPI.js.map