/*
 * 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/
 */
function Plugin(msgCtx,runMode,utils){this._plugins={};this._pluginCtx={error:msgCtx.error,warn:msgCtx.warn,info:msgCtx.info,info:msgCtx.debug,assert:msgCtx.assert,isDebug:msgCtx.isDebug,isVerbose:msgCtx.isVerbose,runMode:runMode};this._utils=utils};Plugin.prototype.getPluginLib=function(){return{hasPlugin:this._hasPlugin.bind(this),getPlugin:this._getPlugin.bind(this),getPluginDescription:this._getPluginDescription.bind(this),getPluginCapability:this._getPluginCapability.bind(this),getPluginCapabilities:this._getPluginCapabilities.bind(this),getPluginVersion:this._getPluginVersion.bind(this)}};Plugin.prototype.getPluginList=function(){var ar=Object.keys(this._plugins);ar.forEach((p,i)=>{ar[i]=`'${p}'`});return ar};Plugin.prototype.setPlugins=function(ar){var bErrors=false;if(ar){if(!Array.isArray(ar)){this._msgCtx.error("Expected an array of objects for API plugin()");return false}let p,i;for(i=0;i<ar.length;i++){if(p=ar[i]){if(!this._utils.isObject(p)){this._msgCtx.error(`plugin() array position ${i} is not an object.`);bErrors=true;continue}if(!this._checkInterface(p,i)){bErrors=true;continue}}}}return!bErrors};Plugin.prototype._hasPlugin=function(name){return!!this._plugins[name]};Plugin.prototype._getPlugin=function(name){var o,ret=null;if(o=this._plugins[name]){if(!o.init){try{o.impl=o.plugin.init(this._pluginCtx)}catch(e){this._msgCtx.error(`Plugin '${name}' init() error - ${e}`)}}ret=o.impl}return ret};Plugin.prototype._getPluginVersion=function(name){var o=this._plugins[name];return o?o.plugin.version:null};Plugin.prototype._getPluginDescription=function(name){var o=this._plugins[name];return o?o.plugin.description:null};Plugin.prototype._getPluginCapability=function(name,capability){var o=this._plugins[name];return o?o.plugin.getCapability(capability):null};Plugin.prototype._getPluginCapabilities=function(name){var o=this._plugins[name];return o?o.plugin.getCapabilities():null};Plugin.prototype._checkInterface=function(p,i){var name;if(!p.name){this._msgCtx.error(`Plugin element ${i} has no 'name'`);return false}name=p.name.trim();if(this._plugins[name]){this._msgCtx.error(`Plugin '${name}' is not unique.`);return false}if(!p.version){this._msgCtx.error(`Plugin '${name}' has no 'version' property.`);return false}if(!p.init){this._msgCtx.error(`Plugin '${name}' has no 'init()' method.`);return false}if(!p.getCapability){this._msgCtx.error(`Plugin '${name}' has no 'getCapability()' method.`);return false}if(!p.getCapabilities){this._msgCtx.error(`Plugin '${name}' has no 'getCapabilities()' method.`);return false}this._plugins[name]={plugin:p,impl:null,init:false};return true};module.exports=Plugin;