/*
 * Copyright (c) 2018, 2026, 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 path=require("path");const Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-vb-icon"};ruleDef.prototype.getShortDescription=function(){return"Verify components have icons for Visual Builder"};ruleDef.prototype.getDescription=function(){return"This rule checks that the correct icon variants are supplied for the visual builder component palette"};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);this._checkIcon(context,fullName,componentType,meta.icon)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkIcon(context,componentInfo.fullName,componentInfo.type,componentInfo.icon)}};ruleDef.prototype._checkIcon=function(context,fullName,componentType,iconDef){let issue;const inRedwood=context.utils.jafLib.getConfig().getTheme().toLowerCase()==="redwood";switch(componentType){case"composite":case"vbcs-pattern":case"vbcs-action":if(!iconDef){issue=new context.Issue(`Component ${fullName} does not have a Visual Builder icon assigned`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context,"info")}else{if(!iconDef.iconPath){issue=new context.Issue(`Component ${fullName} does not have a base Visual Builder icon assigned via iconPath`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context,"info")}else{this._checkIconLocationAndFormat(context,"iconPath",iconDef.iconPath,fullName)}if(inRedwood){if(iconDef.hoverIconPath){this._checkIconLocationAndFormat(context,"hoverIconPath",iconDef.hoverIconPath,fullName)}if(iconDef.iconPath&&iconDef.hoverIconPath&&iconDef.iconPath!==iconDef.hoverIconPath){issue=new context.Issue(`Component ${fullName} should use the same icon for the base and hover state in the Visual Builder Redwood component palette`);issue.setMsgKey(context.ruleName+"_9");context.reporter.addIssue(issue,context,"info")}}else{if(!iconDef.hoverIconPath){issue=new context.Issue(`Component ${fullName} does not have a Visual Builder icon assigned for hover (hoverIconPath)`);issue.setMsgKey(context.ruleName+"_3");context.reporter.addIssue(issue,context,"info")}else{this._checkIconLocationAndFormat(context,"hoverIconPath",iconDef.hoverIconPath,fullName)}if(iconDef.iconPath&&iconDef.hoverIconPath&&iconDef.iconPath===iconDef.hoverIconPath){issue=new context.Issue(`Component ${fullName} uses identical icons for the base and hover state in the Visual Builder component palette`);issue.setMsgKey(context.ruleName+"_4");context.reporter.addIssue(issue,context,"info")}}}break;case"vbcs-template":if(!iconDef||!iconDef.iconPath){issue=new context.Issue(`Template ${fullName} does not have a Visual Builder icon assigned`);issue.setMsgKey(context.ruleName+"_8");context.reporter.addIssue(issue,context,"info")}break}};ruleDef.prototype._checkIconLocationAndFormat=function(context,use,iconFile,componentName){const pathSegs=iconFile.split("/");if(pathSegs[0]!=="extension"){issue=new context.Issue(`Design time ${use} icon ${iconFile} for component ${componentName} should be located under the /extension folder`);issue.setMsgKey(context.ruleName+"_5");context.reporter.addIssue(issue,context,"info")}if(path.extname(iconFile).toLowerCase()!==".svg"){issue=new context.Issue(`Design time ${use} icon ${iconFile} for component ${componentName} should ideally be in SVG format`);issue.setMsgKey(context.ruleName+"_6");context.reporter.addIssue(issue,context,"info")}if(context.rulePack.getExtension().allComponents.has(componentName)){const componentRoot=context.rulePack.getExtension().allComponents.get(componentName).rootFolder;if(!context.utils.fsUtils.fileExists(path.join(componentRoot,iconFile))){issue=new context.Issue(`Design time ${use} icon ${iconFile} for component ${componentName} does not exist or is outside of the component distribution`);issue.setMsgKey(context.ruleName+"_7");context.reporter.addIssue(issue,context)}}};module.exports=ruleDef;