/*! Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one or more contributor license agreements. * Licensed under the Elastic License 2.0; you may not use this file except in compliance with the Elastic License 2.0. */!function(t){function e(e){for(var a,n,r=e[0],s=e[1],o=0,c=[];oconsole.warn(t)))}}class metadata_service_MetadataService{constructor(t){n()(this,"_userMetadata$",new o.BehaviorSubject(void 0)),n()(this,"stop$",new o.Subject),this.config=t}setup(t){this._userMetadata$.next(t);const e=t.trialEndDate;e&&this.scheduleUntil((()=>({inTrial:Date.now()<=new Date(e).getTime()})),(t=>!1===t.inTrial))}get userMetadata$(){return this._userMetadata$.pipe(Object(o.filter)(Boolean),Object(o.debounceTime)(100),Object(o.distinct)((t=>[t.inTrial,t.hasData].join("-"))),Object(o.shareReplay)(1))}start({hasDataFetcher:t}){this._userMetadata$.value&&this.scheduleUntil((async()=>t()),(t=>!0===t.hasData))}stop(){this.stop$.next(),this._userMetadata$.complete()}scheduleUntil(t,e){Object(o.timer)(0,this.config.metadata_refresh_interval.asMilliseconds()).pipe(Object(o.takeUntil)(this.stop$),Object(o.exhaustMap)((async()=>{this._userMetadata$.next({...this._userMetadata$.value,...await t()})})),Object(o.takeWhile)((()=>!e(this._userMetadata$.value)))).subscribe()}}let l,c;!function(t){t["security-solutions.add-integrations-url"]="security-solutions.add-integrations-url"}(l||(l={})),c||(c={});class plugin_CloudExperimentsPlugin{constructor(t){n()(this,"metadataService",void 0),n()(this,"launchDarklyClient",void 0),n()(this,"kibanaVersion",void 0),n()(this,"flagOverrides",void 0),n()(this,"isDev",void 0),n()(this,"getVariation",(async(t,e)=>{const a=l[t];return this.flagOverrides&&Object(r.has)(this.flagOverrides,a)?Object(r.get)(this.flagOverrides,a,e):this.launchDarklyClient?await this.launchDarklyClient.getVariation(a,e):e})),n()(this,"reportMetric",(({name:t,meta:e,value:a})=>{var i;const n=c[t];null===(i=this.launchDarklyClient)||void 0===i||i.reportMetric(n,e,a),this.isDev&&console.debug(`Reported experimentation metric ${n}`,{experimentationMetric:{name:t,meta:e,value:a}})})),this.isDev=t.env.mode.dev,this.kibanaVersion=t.env.packageInfo.version;const e=t.config.get();this.metadataService=new metadata_service_MetadataService({metadata_refresh_interval:Object(s.duration)(e.metadata_refresh_interval)}),e.flag_overrides&&(this.flagOverrides=e.flag_overrides);const a=e.launch_darkly;if(!(null!=a&&a.client_id||t.env.mode.dev))throw new Error("xpack.cloud_integrations.experiments.launch_darkly configuration should exist");null!=a&&a.client_id&&(this.launchDarklyClient=new launch_darkly_client_LaunchDarklyClient(a,this.kibanaVersion))}setup(t,e){var a;e.cloud.isCloudEnabled&&e.cloud.deploymentId&&this.launchDarklyClient&&this.metadataService.setup({userId:e.cloud.deploymentId,kibanaVersion:this.kibanaVersion,trialEndDate:null===(a=e.cloud.trialEndDate)||void 0===a?void 0:a.toISOString(),isElasticStaff:e.cloud.isElasticStaffOwned})}start(t,{cloud:e,dataViews:a}){return e.isCloudEnabled&&(this.metadataService.start({hasDataFetcher:async()=>({hasData:await a.hasData.hasUserDataView()})}),this.metadataService.userMetadata$.pipe(Object(o.concatMap)((async t=>await this.launchDarklyClient.updateUserMetadata(t)))).subscribe()),{getVariation:this.getVariation,reportMetric:this.reportMetric}}stop(){var t;null===(t=this.launchDarklyClient)||void 0===t||t.stop(),this.metadataService.stop()}}function u(t){return new plugin_CloudExperimentsPlugin(t)}}]);