import { Injectable } from '@angular/core';
import { INGXLoggerConfig, NgxLoggerLevel, NGXLoggerRulesService } from 'ngx-logger';
import { ILoggerConfig } from '@red/domain';

@Injectable()
export class LoggerRulesService extends NGXLoggerRulesService {
    shouldCallWriter(level: NgxLoggerLevel, config: ILoggerConfig, message?: unknown, additional?: unknown[]): boolean {
        if (message && typeof message === 'string') {
            const matches = message.match('\\[(.*?)\\]');

            if (matches && matches.length > 0) {
                const firstMatch = matches[1]?.toUpperCase();
                const featureConfig = config.features[firstMatch];

                if (featureConfig) {
                    if (!featureConfig.enabled || (featureConfig.level && featureConfig.level < config.level)) {
                        return false;
                    }
                }
            }
        }

        return super.shouldCallWriter(level, config as INGXLoggerConfig, message, additional);
    }
}
