"use strict";var v=Object.defineProperty;var m=(g,e,t)=>e in g?v(g,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):g[e]=t;var s=(g,e,t)=>(m(g,typeof e!="symbol"?e+"":e,t),t);const p="@pandino/pandino/Logger",R="@pandino/pandino/evaluateFilter",c="service.pid",M="objectClass",C="@pandino/configuration-management/ConfigurationAdmin",S="@pandino/configuration-management/ManagedService",d="@pandino/persistence-manager/PersistenceManager";class L{constructor(e,t,i){s(this,"bundle");s(this,"logger");s(this,"configurationManager");this.configurationManager=e,this.bundle=t,this.logger=i}getConfiguration(e,t){let i=this.configurationManager.getConfiguration(e);return i||(i=this.configurationManager.createConfiguration(e,t)),i}listConfigurations(e){return this.configurationManager.listConfigurations(e)}}class f{constructor(e){s(this,"rawPid");s(this,"servicePid");s(this,"symbolicName");s(this,"version");s(this,"location");s(this,"bindingLevel");if(this.rawPid=e,e.indexOf("|")<0)this.servicePid=e,this.bindingLevel=0;else{let t=0,i=e.indexOf("|");this.servicePid=e.substring(t,i),t=i+1,i=e.indexOf("|",t),i>=0?(this.symbolicName=e.substring(t,i),t=i+1,i=e.indexOf("|",t),i>=0?(this.version=e.substring(t,i),this.location=e.substring(i+1),this.bindingLevel=3):(this.version=e.substring(t),this.bindingLevel=2)):(this.symbolicName=e.substring(t),this.bindingLevel=1)}}matchesTarget(e){let t=e.getBundle();return t?this.symbolicName?this.symbolicName!==t.getSymbolicName()?!1:this.version?t.getVersion()!=this.version?!1:!this.location||this.location===t.getLocation():!0:!0:!1}getRawPid(){return this.rawPid}getServicePid(){return this.servicePid}bindsStronger(e){return this.bindingLevel>e.bindingLevel}equals(e){return e==null?!1:e===this?!0:e instanceof f?this.rawPid===e.rawPid:!1}toString(){return this.rawPid}}class u{constructor(e,t,i,n){s(this,"configurationManager");s(this,"pid");s(this,"isDeleted",!1);s(this,"location");s(this,"properties");this.configurationManager=e,this.pid=new f(t),this.location=i,n&&(this.properties={...n})}delete(){this.checkDeleted(),this.configurationManager.deleteConfiguration(this.pid.toString()),this.isDeleted=!0}equals(e){return e==null?!1:e instanceof u?this.pid.toString()===e.getPid():!1}getBundleLocation(){return this.checkDeleted(),this.location}getPid(){return this.checkDeleted(),this.pid.toString()}getProcessedProperties(e){return this.getProperties()}getProperties(){return this.checkDeleted(),this.properties?{...this.properties,[c]:this.pid.toString()}:void 0}setBundleLocation(e){this.checkDeleted(),this.location=e}update(e){this.checkDeleted(),this.properties=e,this.configurationManager.updateConfiguration(this)}getServicePid(){return this.pid.getServicePid()}getTargetedPid(){return this.pid}checkDeleted(){if(this.isDeleted)throw new Error(`Configuration for PID: ${this.pid}already deleted!`)}}class P{constructor(e,t,i){s(this,"cache",new Map);s(this,"persistenceManager");s(this,"cm");s(this,"context");this.context=e,this.persistenceManager=t,this.cm=i,this.persistenceManager.getProperties().forEach(n=>{const a=new u(this.cm,n[c],this.context.getBundle().getLocation(),n);this.cache.set(n[c],a)})}has(e){if(this.cache.has(e))return!0;const t=this.persistenceManager.load(e);return t?(this.cache.set(e,new u(this.cm,e,this.context.getBundle().getLocation(),t)),!0):!1}get(e){if(this.has(e))return this.cache.get(e)}set(e,t){this.persistenceManager.store(e,t.getProperties()??{}),this.cache.set(e,t)}values(){return Array.from(this.cache.values())}delete(e){this.persistenceManager.exists(e)&&this.persistenceManager.delete(e),this.cache.has(e)&&this.cache.delete(e)}}class E{constructor(e,t,i,n){s(this,"isSync",!0);s(this,"context");s(this,"logger");s(this,"evaluateFilter");s(this,"managedReferences",new Map);s(this,"eventListeners",new Map);s(this,"configurationCache");this.context=e,this.logger=t,this.evaluateFilter=i,this.configurationCache=new P(e,n,this)}initReferencesAddedBeforeManagerActivation(){const e=[],t=[],i=this.context.getServiceReferences(S).filter(n=>n.getProperty(c));for(const n of this.configurationCache.values()){const a=i.filter(r=>r.getProperty(c)===n.getPid());for(const r of a){const o=r.getProperty(c);if(o&&!this.managedReferences.has(o)){const h=new f(o),l=this.context.getService(r);this.initManagedService(o,r,n,h,l)}t.push(r)}}for(const n of i)t.includes(n)||e.push(n);for(const n of e){const a=this.context.getService(n);if(a){const r=n.getProperty(c);this.logger.debug(`Updating non-configured Service for PID: ${r}`),a.updated(void 0),this.managedReferences.has(r)||this.managedReferences.set(r,[]);const o=this.managedReferences.get(r);Array.isArray(o)&&!o.includes(n)&&o.push(n)}}}serviceChanged(e){var a;const t=e.getServiceReference(),i=t.getProperty(c),n=this.context.getService(t);if(n){if(i&&typeof n.updated=="function"){const r=this.configurationCache.has(i)?this.configurationCache.get(i):this.internalCreateConfiguration(i,(a=t.getBundle())==null?void 0:a.getLocation());this.handleManagedServiceEvent(e.getType(),i,t,n,r)}else if(typeof n.configurationEvent=="function"){const r=n;this.handleConfigurationEventListenerEvent(e.getType(),i,r)}}}handleManagedServiceEvent(e,t,i,n,a){var o;const r=new f(t);if(e==="REGISTERED")this.initManagedService(t,i,a,r,n);else if(e==="UNREGISTERING"){this.managedReferences.has(t)||this.managedReferences.set(t,[]);const h=(o=this.managedReferences.get(t))==null?void 0:o.findIndex(l=>l===i);h!==void 0&&h>-1&&this.managedReferences.get(t).splice(h,1),r.matchesTarget(i)&&(n.updated(void 0),this.fireConfigurationChangeEvent("DELETED",t,i))}}initManagedService(e,t,i,n,a){var r;this.managedReferences.has(e)||this.managedReferences.set(e,[]),this.managedReferences.get(e).includes(t)||this.managedReferences.get(e).push(t),i.getBundleLocation()||i.setBundleLocation((r=t.getBundle())==null?void 0:r.getLocation()),n.matchesTarget(t)&&(a==null||a.updated(i.getProperties()),this.fireConfigurationChangeEvent("UPDATED",e,t))}handleConfigurationEventListenerEvent(e,t,i){if(e==="REGISTERED"){this.eventListeners.has(t)||this.eventListeners.set(t,[]);const n=this.eventListeners.get(t);Array.isArray(n)&&!n.includes(i)&&n.push(i)}else if(e==="UNREGISTERING"&&this.eventListeners.has(t)){const n=this.eventListeners.get(t);if(Array.isArray(n)){const a=n.findIndex(r=>r===i);a>-1&&n.splice(a,1)}}}getConfiguration(e){let t=this.configurationCache.get(e);if(t)return this.logger.debug(`Found configuration ${e} bound to ${t.getBundleLocation()}`),t}createConfiguration(e,t){var r;let i=t,n=this.getConfiguration(e);if(n)return n;const a=this.managedReferences.get(e);return a&&a.length&&(i=(r=a[0].getBundle())==null?void 0:r.getLocation()),this.internalCreateConfiguration(e,i)}listConfigurations(e){return e?(this.logger.debug(`Listing configurations matching ${e}`),Array.from(this.configurationCache.values()).filter(t=>this.evaluateFilter(t.getProperties(),e))):[...this.configurationCache.values()]}deleteConfiguration(e){if(this.configurationCache.has(e)&&(this.configurationCache.delete(e),this.managedReferences.has(e))){const t=this.managedReferences.get(e);if(Array.isArray(t))for(const i of t){const n=this.context.getService(i);n&&n.updated(void 0),this.fireConfigurationChangeEvent("DELETED",e,i)}}this.logger.debug(`Attempted to delete already removed configuration for pid: ${e}, ignoring.`)}updateConfiguration(e){if(this.configurationCache.set(e.getPid(),e),this.managedReferences.has(e.getServicePid())){const t=this.managedReferences.get(e.getServicePid());if(Array.isArray(t)){for(const i of t)if(e.getTargetedPid().matchesTarget(i)){const n=this.context.getService(i);n&&(n.updated({...e.getProperties()}),this.fireConfigurationChangeEvent("UPDATED",e.getPid(),i))}}}}internalCreateConfiguration(e,t){return this.logger.debug(`createConfiguration(${e}, ${t})`),new u(this,e,t)}fireConfigurationChangeEvent(e,t,i){const n={getPid(){return t},getReference(){return i},getType(){return e}},a=this.eventListeners.get(t)||[];for(const r of a)r.configurationEvent(n)}}class y{constructor(){s(this,"context");s(this,"loggerReference");s(this,"logger");s(this,"configManager");s(this,"configAdmin");s(this,"configAdminRegistration");s(this,"evaluateFilter");s(this,"filterParserReference");s(this,"persistenceManagerReference");s(this,"persistenceManager");s(this,"pmUsed",!1);s(this,"pmListener")}async start(e){this.context=e,this.loggerReference=e.getServiceReference(p),this.logger=e.getService(this.loggerReference),this.filterParserReference=e.getServiceReference(R),this.evaluateFilter=e.getService(this.filterParserReference),this.persistenceManagerReference=e.getServiceReference(d),this.persistenceManagerReference?(this.logger.info(`Activating Configuration Management with immediate Persistence Manager Reference: ${this.persistenceManagerReference.getProperty(M)}`),this.persistenceManager=e.getService(this.persistenceManagerReference),this.persistenceManager&&(this.init(this.persistenceManager),this.pmUsed=!0)):(this.pmListener={isSync:!0,serviceChanged:t=>{t.getType()==="REGISTERED"&&!this.pmUsed?(this.persistenceManagerReference=t.getServiceReference(),this.persistenceManager=e.getService(this.persistenceManagerReference),this.persistenceManager&&(this.init(this.persistenceManager),this.pmUsed=!0)):t.getType()==="UNREGISTERING"&&this.pmUsed&&(this.persistenceManagerReference&&e.ungetService(this.persistenceManagerReference),this.configManager&&e.removeServiceListener(this.configManager),this.configAdminRegistration&&this.configAdminRegistration.unregister())}},this.logger.info("Configuration Management activation delayed, waiting for a Persistence Manager Reference..."),this.context.addServiceListener(this.pmListener,`(objectClass=${d})`))}async stop(e){this.loggerReference&&e.ungetService(this.loggerReference),this.filterParserReference&&e.ungetService(this.filterParserReference),this.persistenceManagerReference&&e.ungetService(this.persistenceManagerReference),this.configManager&&e.removeServiceListener(this.configManager),this.configAdminRegistration&&this.configAdminRegistration.unregister(),this.pmListener&&e.removeServiceListener(this.pmListener)}init(e){this.logger.info("Initializing Configuration Management..."),!this.pmUsed&&this.context?(this.configManager=new E(this.context,this.logger,this.evaluateFilter,e),this.configAdmin=new L(this.configManager,this.context.getBundle(),this.logger),this.configAdminRegistration=this.context.registerService(C,this.configAdmin),this.configManager.initReferencesAddedBeforeManagerActivation(),this.context.addServiceListener(this.configManager)):this.logger.warn("Tried to re-start Configuration Admin while is already in use. Ignoring")}}module.exports=y;