/*
 * 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 helperPos=require("../../rules/jet/helpers/json-propname-pos");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-property-translatable"};ruleDef.prototype.getShortDescription=function(){return"Verify that properties with a given name have the translatable flag set "};ruleDef.prototype.getDescription=function(){return"To promote consistency amongst a set of components this audit will verify that properties or sub-properties with a given name e.g. pageTitle have the translatable flag set. This flag is primarily used in Visual Builder to guide users to set these properties to resource bundle references rather than hardcoded strings"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){this._checkForTranslatable(context,Utils.getComponentFullName(context.suppData.obj),context.suppData.obj)}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._checkForTranslatable(context,componentInfo.fullName,componentInfo)}};ruleDef.prototype._checkForTranslatable=function(context,componentName,componentInfo){let issue;const customOpts=context.rulePack.getRuleCustomOptions();if(customOpts.translatableProperties&&Array.isArray(customOpts.translatableProperties)&&customOpts.translatableProperties.length>0){let matchExpressions;if(customOpts.expressionMatch){matchExpressions=[];customOpts.translatableProperties.forEach(expString=>{matchExpressions.push(new RegExp(expString,"i"))})}const propertyList=Utils.flattenComponentProperties(componentInfo,"translatable",this._propertyFilter);propertyList.forEach(prop=>{let needsTranslatableFlagSet=false;let matchKey=prop.key;if(matchKey.includes(".")){matchKey=matchKey.split(".").reverse()[0]}if(!matchExpressions&&customOpts.translatableProperties.includes(matchKey)&&!prop.value){needsTranslatableFlagSet=true}else if(matchExpressions?.length>0){matchExpressions.forEach(matchExpression=>{if(matchKey.match(matchExpression)&&!prop.value){needsTranslatableFlagSet=true}})}if(needsTranslatableFlagSet){issue=new context.Issue(`translatable flag should be set for property '${prop.key}'`);context.reporter.addIssue(issue,context)}})}};ruleDef.prototype._propertyFilter=function(propertyName,propertyMetadata){if(propertyMetadata.type==="string"&&!propertyMetadata.enumValues){return true}return false};module.exports=ruleDef;