/*
 * 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 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-exchange-images"};ruleDef.prototype.getShortDescription=function(){return"Validates the existence of images specified for Exchange in the component metadata"};ruleDef.prototype.getDescription=function(){return"This audit checks for the declaration of cover image or screen shot information in a component definition and ensures that the specified files actually exist within the packaged component. Such missing images would prevent the component from being shared via the Exchange"};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);this._checkImages(context,fullName,meta)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkImages(context,componentInfo.fullName,componentInfo)}};ruleDef.prototype._checkImages=function(context,fullName,componentMeta){if(!Utils.shouldIgnoreCheck(context,"jetwc-exchange-images",fullName)){const compInfo=context.rulePack.getExtension().allComponents.get(fullName);if(componentMeta.extension&&componentMeta.extension.catalog&&compInfo){const componentRoot=compInfo.rootFolder;if(componentMeta.extension.catalog.coverImage&&componentMeta.extension.catalog.coverImage.length>0){this._verfiyImage(context,componentRoot,componentMeta.extension.catalog.coverImage,"Cover Image",fullName)}if(componentMeta.extension.catalog.screenshots&&Object.keys(componentMeta.extension.catalog.screenshots).length>0){Object.keys(componentMeta.extension.catalog.screenshots).forEach(screenShot=>{this._verfiyImage(context,componentRoot,componentMeta.extension.catalog.screenshots[screenShot],"Screenshot",fullName)})}}}};ruleDef.prototype._verfiyImage=function(context,componentRoot,imageFile,use,componentName){let issue;if(!context.utils.fsUtils.fileExists(path.join(componentRoot,imageFile))){issue=new context.Issue(`Image file used for Exchange does not exist or is outside of the component distribution: Image ${imageFile} used as ${use} for component ${componentName}`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}else{const pathSegs=imageFile.split("/");if(pathSegs[0]!=="extension"){issue=new context.Issue(`Images used for Exchange should ideally be located in the /extension folder: Image ${imageFile} used as ${use} for component ${componentName}`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context,"info")}}};module.exports=ruleDef;