/*
 * 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 DN_PATTERN=/^([A-Z][a-z0-9\-]*\s?)(([A-Z][a-z0-9\-]*\s?)|([0-9]*\s?))*[^\.]/gm;const FORBIDDEN=[/spectra[\s\.,]/gmi,/vb[\s\.,]/gmi,/fa[\s\.,]/gmi];SCOPE_MAP={property:"properties",propertyEditorValues:"properties",slot:"slots",method:"methods",event:"events"};const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwco-displayname-standards"};ruleDef.prototype.getShortDescription=function(){return"Verifies the displayName attributes of a component meet the documented standards"};ruleDef.prototype.getDescription=function(){return"Internal Oracle standards for displayName dictate the use of init-capped words and no punctuation. 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._checkDisplayNames(context,fullName,meta)}}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkDisplayNames(context,componentInfo.fullName,componentInfo)}};ruleDef.prototype._checkDisplayNames=function(context,fullName,metadata){if(metadata){this._checkInScope(context,fullName,metadata.displayName);if(metadata.properties){const flattenedProps=Utils.flattenComponentProperties(metadata,"displayName");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.displayName,"propertyEditorValues",prop.key,pevKey)}}})}if(metadata.events){for(const[key,value]of Object.entries(metadata.events)){this._checkInScope(context,fullName,value.displayName,"event",key)}}if(metadata.methods){for(const[key,value]of Object.entries(metadata.methods)){this._checkInScope(context,fullName,value.displayName,"method",key)}}if(metadata.slots){for(const[key,value]of Object.entries(metadata.slots)){this._checkInScope(context,fullName,value.displayName,"slot",key)}}}};ruleDef.prototype._checkInScope=function(context,fullName,displayName,scopeType,scope,scopeDetail){const ignore=Utils.shouldIgnoreCheck(context,context.ruleName,fullName,scopeType?SCOPE_MAP[scopeType]:null,scope);if(!ignore&&displayName&&displayName.length>0){const matches=displayName.match(DN_PATTERN);if(!matches||matches[0]!==displayName){const issue=new context.Issue(`Component ${fullName} ${scopeType?scopeType+" > "+scope+" ":""}${scopeDetail?"("+scopeDetail+") ":""}displayName does not meet the Oracle standards of init-caps and no punctuation`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}FORBIDDEN.forEach(pattern=>{if(displayName.match(pattern)){const issue=new context.Issue(`Component ${fullName} ${scopeType?scopeType+" > "+scope+" ":""}${scopeDetail?"("+scopeDetail+") ":""}displayName contains a forbidden term '${pattern.toString().substring(1,pattern.toString().length-11)}'`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context,"blocker")}})}};module.exports=ruleDef;