/*
 * 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 Histo=require("./Histo");class cliStats{constructor(nd,stats,severity,oConsole,info,isUnitTest,appCtx,useStdErr){this._nd=nd;this._severity=severity;this._clr=appCtx.colors;this._isColor=this._clr.isColor();this._console=oConsole;this._info=info;this._isUnitTest=isUnitTest;this._useStdErr=useStdErr;this._stats=stats}displayStatsAndHisto(elapsed){var clr=this._clr;var msg,msg2,sevs,n,m,l;var stats=this._stats;n=stats.errorFiles||stats.parseErrors;m=stats.totalIssues;msg="JET Audit: "+m+" issue"+(m!==1?"s":"")+(m!==1?" were":" was")+" found"+(m?" in "+stats.totalIssueFiles+" file"+(stats.totalIssueFiles!==1?"s":""):"")+".";msg="\n"+(m?clr.BLACKONRED(msg):clr.GREEN(msg));if(n){n=stats.errorFiles;m=stats.parseErrors;msg2=(n?" "+n+" file"+(n!==1?"s":"")+" could not be read.":"")+(m?" "+m+" parse error"+(m!==1?"s":"")+" "+(m===1?"was":"were")+" found.":"");msg+=clr.BLACKONRED(msg2)}this._console(msg);if(this._useStdErr){process.stderr.write(msg+"\n")}n=stats.totalFilesSelected;msg=_right(n,6)+" file"+(n===1?" ":"s ")+(n===1?"was":"were")+" selected.";n=stats.totalFilesAudited;msg+="\n"+_right(n,6)+" file"+(n===1?" ":"s ")+(n===1?"was":"were")+" audited.";n=stats.totalUrlsAudited;msg+="\n"+_right(n,6)+" URL"+(n===1?" ":"s ")+(n===1?"was":"were")+" audited.";n=stats.rulesFired;msg+="\n"+_right(n,6)+" rule"+(n===1?" ":"s ")+(n===1?"was":"were")+" fired.";n=stats.hooksFired;msg+="\n"+_right(n,6)+" hook rule"+(n===1?" ":"s ")+(n===1?"was":"were")+" fired.";sevs=this._severity.getMappedArray()||this._severity.getSevList();sevs=sevs.filter((val,x)=>sevs.indexOf(val)===x);l=0;sevs.forEach(v=>{n=v.length;l=n>l?n:l});l+=2;let sev,sevTotals;sevTotals=this._getSevTotals(stats.totalSevStats);var histo=new Histo(sevTotals,40,clr);msg+="\n\n"+"-".repeat(10)+"Severities"+"-".repeat(10);for(sev in sevTotals){n=sevTotals[sev];msg+="\n"+_left(sev,l)+": "+_right(n,6)+"  |"+histo.bar(n)}msg+="\n"+"-".repeat(30);if(elapsed){msg+="\n[Elapsed: "+elapsed[0]+(elapsed[1]/1000000000).toString().substr(1,3)+"s]"}this._console(msg)}displayErrorsAndAsserts(){var clr=this._clr;var stats=this._stats;var msg="";if(stats.errors){msg+="[Error]: "+stats.errors+" error";msg+=(stats.errors===1?" was":"s were")+" found."}if(stats.warnings){if(msg.length){msg+="\n"}msg+="[Warn]: "+stats.warnings+" warning";msg+=(stats.warnings===1?" was":"s were")+" found."}if(stats.asserts){if(msg.length){msg+="\n"}msg+="[Warn]: "+stats.asserts+" assert";msg+=(stats.asserts===1?" was":"s were")+" found."}msg+="\n";if(!this._isUnitTest){this._info("\n"+clr.BLACKONRED(msg))}else{this._nd.fs.writeSync(process.stdout.fd,"\n"+clr.BLACKONRED(msg))}if(this._useStderr){process.stderr.write(msg+"\n")}}_getSevTotals(totalSevStats){var sevMap,sevTotals,sev,val,n=0;var sevTotals={};if(sevMap=this._severity.getInvertedSevMap()){for(sev in sevMap){val=sevMap[sev];if(!Array.isArray(val)){n+=totalSevStats[val]}else{val.forEach(v=>{n+=totalSevStats[v]})}sevTotals[sev]=n;n=0}}else{sevTotals=totalSevStats}return sevTotals}};function _right(num,len){var s=""+num;return len>s.length?" ".repeat(len-s.length)+s:s};function _left(num,len){var s=""+num;return len>s.length?s+" ".repeat(len-s.length):s};module.exports=cliStats;