/*
 * 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 ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-displayname"};ruleDef.prototype.getShortDescription=function(){return"Check component display name will work with Visual Builder"};ruleDef.prototype.getDescription=function(){return"This rule checks both the length and the number of words of the display name - configurable"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){let issue;const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);const componentType=Utils.getComponentType(meta);this._checkDisplayNames(context,fullName,componentType,meta.displayName,meta.properties,meta.events,meta.slots)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkDisplayNames(context,componentInfo.fullName,componentInfo.type,componentInfo.displayName,componentInfo.properties,componentInfo.events,componentInfo.slots)}};ruleDef.prototype._checkDisplayNames=function(context,fullName,componentType,displayName,properties,events,slots){const ruleOptions=context.rulePack.getRuleCustomOptions();let limits;let elimits;let plimits;let slimits;switch(componentType){case"composite":elimits=ruleOptions.limits.event;plimits=ruleOptions.limits.property;slimits=ruleOptions.limits.slot;case"vbcs-pattern":limits=ruleOptions.limits.component;break;case"vbcs-template":limits=ruleOptions.limits.template;break}if(limits&&displayName){if(displayName.length>limits.length){this._lengthError(context,fullName,componentType,limits.length)}if(displayName.split(" ").length>limits.words){this._wordCountError(context,fullName,componentType,limits.words)}}if(plimits&&properties){this.checkByAttributeType(context,fullName,componentType,plimits,properties,"properties","property")}if(elimits&&events){this.checkByAttributeType(context,fullName,componentType,elimits,events,"events","event")}if(slimits&&slots){this.checkByAttributeType(context,fullName,componentType,slimits,slots,"slots","slot")}};ruleDef.prototype._lengthError=function(context,component,type,limit,scope){issue=new context.Issue(`displayName property for Component ${component}${scope?" ("+scope+")":""} exceeds the defined length limit of ${limit} for a ${type}`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)};ruleDef.prototype._wordCountError=function(context,component,type,limit,scope){issue=new context.Issue(`displayName property for Component ${component}${scope?" ("+scope+")":""} exceeds the defined word count of ${limit} for a ${type}`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)};ruleDef.prototype.checkByAttributeType=function(context,fullName,componentType,limits,meta,attributeType,typeName){Object.keys(meta).forEach(cName=>{const cDN=meta[cName].displayName;if(cDN){if(cDN.length>limits.length){this._lengthError(context,fullName,componentType+" "+typeName,limits.length,attributeType+"=>"+cName)}if(cDN.split(" ").length>limits.words){this._wordCountError(context,fullName,componentType+" "+typeName,limits.words,attributeType+"=>"+cName)}}})};module.exports=ruleDef;