/*
 * 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const SENTENCE_PATTERN=/^([A-Z])/gm;const FORBIDDEN=[/spectra[\s\.,]/gmi,/vb[\s\.,]/gmi,/fa[\s\.,]/gmi];const INTERNAL_URLS=[/http(s?):\/\/(\w)*.(\w){2,}.oracle.com/gmi,/http(s?):\/\/(\w)*.oraclecorp.com/gmi];SCOPE_MAP={property:"properties",propertyEditorValues:"properties",slot:"slots",method:"methods",event:"events"};const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-description-standards"};ruleDef.prototype.getShortDescription=function(){return"Verifies the description attributes of a component meet the documented standards"};ruleDef.prototype.getDescription=function(){return"Internal Oracle standards for description dictate the use of sentence case with normal punctuation, but no trailing period on the final sentence. This audit does a basic scan for this."};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);const componentType=Utils.getComponentType(meta);if(componentType==="composite"){this._checkDescriptions(context,fullName,meta)}}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkDescriptions(context,componentInfo.fullName,componentInfo)}};ruleDef.prototype._checkDescriptions=function(context,fullName,metadata){if(metadata){this._checkInScope(context,fullName,metadata.description);if(metadata.properties){const flattenedProps=Utils.flattenComponentProperties(metadata,"description");flattenedProps.forEach(prop=>{this._checkInScope(context,fullName,prop.value,"property",prop.key)});const flattenedPEV=Utils.flattenComponentProperties(metadata,"propertyEditorValues");flattenedPEV.forEach(prop=>{if(prop.value){for([pevKey,pevDef]of Object.entries(prop.value)){this._checkInScope(context,fullName,pevDef.description,"propertyEditorValues",prop.key,pevKey)}}})}if(metadata.events){for(const[key,value]of Object.entries(metadata.events)){this._checkInScope(context,fullName,value.description,"event",key)}}if(metadata.methods){for(const[key,value]of Object.entries(metadata.methods)){if(!["setProperty","getProperty","setProperties"].includes(key)){this._checkInScope(context,fullName,value.description,"method",key)}}}if(metadata.slots){for(const[key,value]of Object.entries(metadata.slots)){this._checkInScope(context,fullName,value.description,"slot",key)}}}};ruleDef.prototype._checkInScope=function(context,fullName,checkDescription,scopeType,scope,scopeDetail){const ignore=Utils.shouldIgnoreCheck(context,context.ruleName,fullName,scopeType?SCOPE_MAP[scopeType]:null,scope);let description=new String(checkDescription);if(!ignore&&description&&description.length>0){description=description.replace("e.g.","");const endSent=/\.[^A-Z^a-z]/;const sentences=description.split(endSent);sentences.forEach(sentence=>{const cleaned=sentence.trim();if(cleaned&&cleaned.length>0){const ucMatches=cleaned.match(SENTENCE_PATTERN);if(!ucMatches){const issue=new context.Issue(`Component ${fullName} ${scopeType?scopeType+" > "+scope+" ":""}${scopeDetail?"("+scopeDetail+") ":""}description does not meet the Oracle standards. Indication: sentences must begin with a capital letter`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}}});FORBIDDEN.forEach(pattern=>{if(description.match(pattern)){const issue=new context.Issue(`Component ${fullName} ${scopeType?scopeType+" > "+scope+" ":""}${scopeDetail?"("+scopeDetail+") ":""}description does not meet the Oracle standards. Indication: forbidden term '${pattern.toString().substring(1,pattern.toString().length-11)}'`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context,"blocker")}});INTERNAL_URLS.forEach(pattern=>{const detectedUrls=description.match(pattern);if(detectedUrls){detectedUrls.forEach(url=>{const issue=new context.Issue(`Component ${fullName} ${scopeType?scopeType+" > "+scope+" ":""}${scopeDetail?"("+scopeDetail+") ":""}description does not meet the Oracle standards. Indication: internal URL '${url}'`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context,"blocker")})}})}};module.exports=ruleDef;