// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Microsoft Software License Terms for the Azure Communications Services, Azure CPaaS, MICROSOFT AZURE COMMUNICATION CALLING EFFECTS
// See LICENSE file for license information.

import{createClientLogger as e}from"@azure/logger";import{LoggerImpl as t}from"./de977d.js";import{stringifyObject as n}from"./cd7b0b.js";import{VideoEffectInternalBase as o}from"./0b7c50.js";import{ImageLoader as i}from"./a767f2.js";var __awaiter=function(e,t,n,o){function adopt(e){return e instanceof n?e:new n((function(t){t(e)}))}return new(n||(n=Promise))((function(n,i){function fulfilled(e){try{step(o.next(e))}catch(e){i(e)}}function rejected(e){try{step(o.throw(e))}catch(e){i(e)}}function step(e){e.done?n(e.value):adopt(e.value).then(fulfilled,rejected)}step((o=o.apply(e,t||[])).next())}))};class BackgroundReplacementEffectInternalImpl extends o{constructor(e,t,n){super(t,n),this.imageLoader=new i(n,this.configProvider)}get effectConfig(){return this.configProvider.config.videoEffects.effectConfig}getEffectProvider(){var e,t;return __awaiter(this,void 0,void 0,(function*(){let o="";try{o=yield this.imageLoader.loadImage(this.effectConfig.backgroundImageUrl);const e=Object.assign({},this.configProvider.config);e.videoEffects.effectConfig=Object.assign(Object.assign({},this.configProvider.config.videoEffects.effectConfig),{backgroundImageUrl:o}),this.configProvider.updateConfig(e)}catch(o){const i={code:null!==(e=null==o?void 0:o.code)&&void 0!==e?e:"FailedToProcess",message:`Error fetching effect provider - ${n(null!==(t=null==o?void 0:o.message)&&void 0!==t?t:o)}`};return this.handleProcessingError(i),Promise.reject(i)}return this.webcvProvider}))}}let r=0;class BackgroundReplacementEffect{constructor(n){this.name="BackgroundReplacement",this._logger=new t(e("ACS-calling-video-effects"),[()=>"BackgroundReplacementEffect"+r++]),this._backgroundReplacementInternal=new BackgroundReplacementEffectInternalImpl(n,this.name,this._logger),this._updateEffectConfig(n),this._logger.info(`v${this._backgroundReplacementInternal.getVersion()} BackgroundReplacementEffect ctor success`)}get _internalHandle(){return this._backgroundReplacementInternal}configure(e){var t,o;return __awaiter(this,void 0,void 0,(function*(){const i=Object.assign({},e);let r="";try{r=yield this._internalHandle.imageLoader.loadImage(i.backgroundImageUrl)}catch(e){const i={code:null!==(t=null==e?void 0:e.code)&&void 0!==t?t:"FailedToProcess",message:`Error updating config - ${n(null!==(o=null==e?void 0:e.message)&&void 0!==o?o:e)}`};return this._internalHandle.handleProcessingError(i),Promise.reject(i)}i.backgroundImageUrl=r,this._updateEffectConfig(i)}))}isSupported(){return __awaiter(this,void 0,void 0,(function*(){return yield this._internalHandle.capabilityChecker.videoEffectsSupported()}))}_updateEffectConfig(e){var t,o;const i=this._internalHandle.configProvider,r=Object.assign({},i.config);r.videoEffects.effectConfig=Object.assign(Object.assign({},null===(o=null===(t=i.config)||void 0===t?void 0:t.videoEffects)||void 0===o?void 0:o.effectConfig),e),i.updateConfig(r),this._logger.info(`Effect config updated with new values: ${n(e)}`)}}export{BackgroundReplacementEffect};
