/*
 * Copyright (c) 2018, 2025, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const CRITICAL="critical";const BLOCKER="blocker";const MAJOR="major";const MINOR="minor";const INFO="info";const ALL="all";const DEFAULT=CRITICAL;const SEVERITY=[INFO,MINOR,MAJOR,CRITICAL,BLOCKER];const SevLib=require("./SevLib");function Severity(){this._sevMap=null;this._sevSet=null;this._sevInv=null;this._sevExpr=null;this._sevMsg=null;this._sevLib=null;this._sevInvArray=null;this.CRITICAL=CRITICAL;this.BLOCKER=BLOCKER;this.MAJOR=MAJOR;this.MINOR=MINOR;this.INFO=INFO;this.ALL=ALL;this.DEFAULT=DEFAULT};Severity.prototype.isValidRuleSeverity=function(sev){return SEVERITY.indexOf(sev)>=0};Severity.prototype.isValidConfigSeverity=function(sev){var o=this.getSevExpr(sev);if(o.op&&o.sev==="all")return false;return SEVERITY.indexOf(o.sev)>=0||o.sev===ALL||this.isMapped(o.sev)};Severity.prototype.matchSeverityLevel=function(sev,sevExpr){var level,op,ret;sevExpr=sevExpr||this._sevExpr;if(!sevExpr){return}op=sevExpr.op;if(op){level=this._assignLevel(sev);if(op===">")ret=level>sevExpr.lev;else if(op===">=")ret=level>=sevExpr.lev;else if(op==="<")ret=level<sevExpr.lev;else if(op==="<=")ret=level<=sevExpr.lev;else if(op==="=")ret=level===sevExpr.lev}else{if(sevExpr.sev==="all")ret=true;else ret=sev===sevExpr.sev}return ret};Severity.prototype.isBlocker=function(sev){return this.unmap(sev)===BLOCKER};Severity.prototype.isCritical=function(sev){return this.unmap(sev)===CRITICAL};Severity.prototype.isMajor=function(sev){return this.unmap(sev)===MAJOR};Severity.prototype.isMinor=function(sev){return this.unmap(sev)===MINOR};Severity.prototype.isInfo=function(sev){return this.unmap(sev)===INFO};Severity.prototype.map=function(sev){var ret;if(this._sevSet){ret=this._sevSet[sev]}return ret||sev};Severity.prototype.unmap=function(sev){var ret;if(this._sevInv){ret=this._sevInv[sev]}return ret||sev};Severity.prototype.isMapped=function(sev){return!!(this._sevInv&&this._sevInv[sev])};Severity.prototype.isSev=function(sev){return SEVERITY.includes(sev)||this.isMapped(sev)};Severity.prototype.getSevExpr=function(sev){var origSev,ret={};ret.sev=origSev=_stripOp(sev);ret.sev=this.unmap(ret.sev);ret.op=sev.replace(origSev,"");ret.op=ret.op.length?ret.op:null;if(ret.op&&ret.op==="=="){ret.op="="}ret.lev=this._assignLevel(ret.sev);return ret};Severity.prototype.getMsgSev=function(msgId){return this._sevMsg?this._sevMsg[msgId]:null};Severity.prototype.getConfigSevExpr=function(){return this._sevExpr};Severity.prototype.getSevList=function(){return SEVERITY};Severity.prototype.getSevMap=function(){return this._sevSet};Severity.prototype.getInvertedSevMap=function(){return this._sevInv};Severity.prototype.getMappedArray=function(){if(!this._sevInvArray&&this._sevInv){let a,i;this._sevInvArray=a=[];for(i=0;i<SEVERITY.length;i++){a.push(this.map(SEVERITY[i]))}}return this._sevInvArray};Severity.prototype.setSevExpr=function(expr){this._sevExpr=expr};Severity.prototype.setSevMap=function(sevMap,sevInv){this._sevMap=sevMap;this._sevSet=sevMap.sevSet;this._sevInv=sevInv};Severity.prototype.setSevMsg=function(sevMsg){this._sevMsg=sevMsg};Severity.prototype.getSevLib=function(){if(!this._sevLib){this._sevLib=new SevLib(this)}return this._sevLib};Severity.prototype._assignLevel=function(sev){var lev=SEVERITY.indexOf(sev);if(lev<0&&sev!=="all"){sev=this.unmap(sev);lev=SEVERITY.indexOf(sev)}return lev};function _stripOp(sev){var c,x,s=sev;if(sev){c=sev.charAt(0);if(c===">"||c==="<"||c==="="){x=1;if(sev.charAt(1)==="="){x=2}s=sev.substr(x)}}return s};module.exports=Severity;