"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiditService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const axios_1 = require("axios");
let DiditService = class DiditService {
    constructor(configService) {
        this.configService = configService;
        const apiKey = this.configService.get('DIDIT_API_KEY');
        if (!apiKey) {
            throw new Error('Missing DIDIT_API_KEY in environment');
        }
        this.http = axios_1.default.create({
            baseURL: 'https://verification.didit.me',
            headers: {
                'x-api-key': apiKey,
                'Content-Type': 'application/json',
            },
            timeout: 15000,
        });
    }
    getDefaultKycWorkflowId() {
        return this.configService.get('DIDIT_KYC_WORKFLOW_ID') || undefined;
    }
    getDefaultKycCallbackUrl() {
        return this.configService.get('DIDIT_KYC_CALLBACK_URL') || undefined;
    }
    async createSession(payload) {
        try {
            const res = await this.http.post('/v3/session/', payload);
            return res.data;
        }
        catch (error) {
            const err = error;
            const status = err.response?.status;
            const detail = err.response?.data?.detail ||
                err.response?.data?.message ||
                err.message ||
                'Didit request failed';
            if (status && status >= 400 && status < 500) {
                throw new common_1.BadRequestException(detail);
            }
            throw new common_1.InternalServerErrorException(detail);
        }
    }
    async getSession(sessionId) {
        try {
            const res = await this.http.get(`/v3/session/${sessionId}`);
            return res.data;
        }
        catch (error) {
            const err = error;
            const status = err.response?.status;
            if (status === 404 || status === 403) {
                return null;
            }
            const detail = err.response?.data?.detail ||
                err.response?.data?.message ||
                err.message ||
                'Didit session fetch failed';
            throw new common_1.InternalServerErrorException(detail);
        }
    }
};
exports.DiditService = DiditService;
exports.DiditService = DiditService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.ConfigService])
], DiditService);
//# sourceMappingURL=didit.service.js.map