/*
 * 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 RULENAME="oj-js-public-api";const DESCRIPTION="Modules declared in the array passed as the first argument of a define() or require() "+"function of a .js file that are sourced from a component or component pack, should be :<br>"+"a) representative as being a loader for a given component, or<br>"+"b) defined as a public module for a resource component that is not marked as "+"subcomponentType: packPrivate";const SHORT_DESCRIPTION="Modules declared in the first argument of a define() or require() must not be private";const path=require("path");const IGN_MODULES=["ojs/","ojcss!","ojL10n!","text!","css!"];class Rule{getName(){return RULENAME}getDescription(){return DESCRIPTION}getShortDescription(){return SHORT_DESCRIPTION}register(regCtx){this._metaLib=regCtx.utils.metaLib;this._fsUtils=regCtx.utils.fsUtils;return{CallExpression:this._onCall}}_onCall(ruleCtx,node){var name;this._fileBase=path.dirname(ruleCtx.filepath);if(name=node?.callee.name){if(name==="define"||name==="require"){this._onRequireOrDefine(ruleCtx,node,name)}}}_onRequireOrDefine(ruleCtx,node,origin){var args,arg,arrayExpr,elems;if((args=node.arguments)&&args.length){if((elems=args[0])&&elems.type==="ArrayExpression"){if(elems=elems.elements){elems.forEach(arg=>{if(arg.type==="Literal"&&arg.value.includes("/")){if(!_isIgnoreEntry(arg.value)){this._checkModule(ruleCtx,arg,origin)}}})}}}}_checkModule(ruleCtx,module,origin){var compName,raw,isLoader,packMeta,meta,x;x=module.value.indexOf("/");if(!(packMeta=this._metaLib.getPackMetadata(module.value.substring(0,x)))){return}compName=raw=module.value.substring(0,x)+"-"+module.value.substring(x+1);if((x=compName.lastIndexOf("/loader"))>=0){compName=compName.substring(0,x);isLoader=true}if(isLoader){if(meta=this._metaLib.getTagMetadata(compName)){if(meta.subcomponentType==="packPrivate"){this._emitIssue(ruleCtx,1,origin,compName,module)}if(meta.type!=="composite"){this._emitIssue(ruleCtx,5,origin,compName,module)}}return}if(compName.includes("/")){this._checkMultiPath(ruleCtx,module,packMeta,origin);return}if(this._metaLib.isWCTag(compName)){if(this._metaLib.isTagPackPrivate(compName)){this._emitIssue(ruleCtx,1,origin,compName,module)}}}_checkMultiPath(ruleCtx,module,packMeta,origin){var compName,elems,path,rest,dependency,resMeta;compName=module.value;elems=compName.split("/");path=elems[0]+"/"+elems[1];dependency=path.replace("/","-");rest=compName.substring(path.length+1);if(!packMeta.dependencies[dependency]){this._emitIssue(ruleCtx,1,origin,compName,module);return}if(!(resMeta=this._metaLib.getResourceMetadata(path))){this._emitIssue(ruleCtx,3,origin,compName,module);return}if(resMeta.subcomponentType==="packPrivate"){this._emitIssue(ruleCtx,4,origin,compName,module);return}if(!resMeta?.publicModules.includes(rest)){this._emitIssue(ruleCtx,2,origin,compName,module);return}}_emitIssue(ruleCtx,msgNum,from,compName,module){var issue;if(msgNum>=1&&msgNum<=4){issue=new ruleCtx.Issue(`'${from}' '${module.value}' is private and not intended for public use.`)}else if(msgNum===5){issue=new ruleCtx.Issue(`'${from}' '${compName}' is not a composite component.`)}else{return}issue.setPosition(null,null,module.start,module.end);if(msgNum>1)issue.setMsgKey(RULENAME+"_"+(msgNum-1));ruleCtx.reporter.addIssue(issue,ruleCtx)}}function _isIgnoreEntry(name){var i;for(i=0;i<IGN_MODULES.length;i++){if(name.startsWith(IGN_MODULES[i])){break};}return i<IGN_MODULES.length};module.exports=Rule;