"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.DebugLoggerService = exports.DebugLogLevel = void 0;
const common_1 = require("@nestjs/common");
const auth_config_service_1 = require("./auth-config.service");
var DebugLogLevel;
(function (DebugLogLevel) {
    DebugLogLevel["ERROR"] = "error";
    DebugLogLevel["WARN"] = "warn";
    DebugLogLevel["INFO"] = "info";
    DebugLogLevel["DEBUG"] = "debug";
    DebugLogLevel["VERBOSE"] = "verbose";
})(DebugLogLevel || (exports.DebugLogLevel = DebugLogLevel = {}));
let DebugLoggerService = class DebugLoggerService {
    authConfig;
    logger = new common_1.Logger('NestAuth');
    config = {
        enabled: false,
        level: DebugLogLevel.VERBOSE,
        prefix: '[NestAuth]',
        includeTimestamp: true,
        includeContext: true
    };
    constructor(authConfig) {
        this.authConfig = authConfig;
        this.updateConfig();
    }
    updateConfig() {
        const authOptions = this.authConfig.getConfig();
        if (authOptions.debug) {
            this.config = {
                ...this.config,
                ...authOptions.debug
            };
        }
    }
    shouldLog(level) {
        if (!this.config.enabled)
            return false;
        const levels = [
            DebugLogLevel.ERROR,
            DebugLogLevel.WARN,
            DebugLogLevel.INFO,
            DebugLogLevel.DEBUG,
            DebugLogLevel.VERBOSE
        ];
        const currentLevelIndex = levels.indexOf(this.config.level);
        const requestedLevelIndex = levels.indexOf(level);
        return requestedLevelIndex <= currentLevelIndex;
    }
    formatMessage(message, context, data) {
        let formattedMessage = '';
        if (this.config.prefix) {
            formattedMessage += `${this.config.prefix} `;
        }
        if (this.config.includeTimestamp) {
            formattedMessage += `[${new Date().toISOString()}] `;
        }
        if (this.config.includeContext && context) {
            formattedMessage += `[${context}] `;
        }
        formattedMessage += message;
        if (data) {
            formattedMessage += ` | Data: ${JSON.stringify(data, null, 2)}`;
        }
        return formattedMessage;
    }
    error(message, context, data, trace) {
        if (this.shouldLog(DebugLogLevel.ERROR)) {
            this.logger.error(this.formatMessage(message, context, data), trace);
        }
    }
    warn(message, context, data) {
        if (this.shouldLog(DebugLogLevel.WARN)) {
            this.logger.warn(this.formatMessage(message, context, data));
        }
    }
    info(message, context, data) {
        if (this.shouldLog(DebugLogLevel.INFO)) {
            this.logger.log(this.formatMessage(message, context, data));
        }
    }
    debug(message, context, data) {
        if (this.shouldLog(DebugLogLevel.DEBUG)) {
            this.logger.debug(this.formatMessage(message, context, data));
        }
    }
    verbose(message, context, data) {
        if (this.shouldLog(DebugLogLevel.VERBOSE)) {
            this.logger.verbose(this.formatMessage(message, context, data));
        }
    }
    logUserOperation(operation, userId, data) {
        this.debug(`User Operation: ${operation}`, 'UserService', {
            userId,
            operation,
            ...data
        });
    }
    logAuthOperation(operation, providerName, userId, data) {
        this.debug(`Auth Operation: ${operation}`, 'AuthService', {
            operation,
            providerName,
            userId,
            ...data
        });
    }
    logRoleOperation(operation, roleId, data) {
        this.debug(`Role Operation: ${operation}`, 'RoleService', {
            operation,
            roleId,
            ...data
        });
    }
    logTenantOperation(operation, tenantId, data) {
        this.debug(`Tenant Operation: ${operation}`, 'TenantService', {
            operation,
            tenantId,
            ...data
        });
    }
    logSessionOperation(operation, sessionId, data) {
        this.debug(`Session Operation: ${operation}`, 'SessionService', {
            operation,
            sessionId,
            ...data
        });
    }
    logPermissionOperation(operation, permissionId, data) {
        this.debug(`Permission Operation: ${operation}`, 'PermissionService', {
            operation,
            permissionId,
            ...data
        });
    }
    logError(error, context, additionalData) {
        this.error(`Error in ${context}: ${error.message}`, context, {
            error: error.name,
            message: error.message,
            stack: error.stack,
            ...additionalData
        }, error.stack);
    }
    logFunctionEntry(functionName, context, params) {
        this.verbose(`Entering function: ${functionName}`, context, { params });
    }
    logFunctionExit(functionName, context, result) {
        this.verbose(`Exiting function: ${functionName}`, context, { result });
    }
};
exports.DebugLoggerService = DebugLoggerService;
exports.DebugLoggerService = DebugLoggerService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [auth_config_service_1.AuthConfigService])
], DebugLoggerService);
//# sourceMappingURL=debug-logger.service.js.map