import*as e from"react";import*as t from"useblocks";var s={d:(e,t)=>{for(var n in t)s.o(t,n)&&!s.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},n={};s.d(n,{A:()=>u});const i=(r={default:()=>e.default},a={},s.d(a,r),a);var r,a;const o=(e=>{var t={};return s.d(t,e),t})({init:()=>t.init}),l=new Set(["container","id","className","style","getAuthToken","handleHtmlChanged","handleEmailChanged","handleSaveEmail","handleReadBlocks","handleSaveBlock","handleRemoveBlock","handleTranslate","handleLoad","handleDestroy","handleChunkLoaded","handleLoadImage","handleSaveImage","handleRemoveImage","handleReadImages","handleNotification","handleReadNotifications","handleMarkNotificationRead","handleMarkAllNotificationsRead"]);function c(e,t){if(Object.is(e,t))return!0;if(typeof e!=typeof t)return!1;if(!e||!t||"object"!=typeof e)return!1;if(Array.isArray(e)!==Array.isArray(t))return!1;const s=e,n=t,i=Object.keys(s),r=Object.keys(n);return i.length===r.length&&i.every(e=>Object.prototype.hasOwnProperty.call(n,e)&&c(s[e],n[e]))}function h(e){const t={};for(const[s,n]of Object.entries(e))l.has(s)||"function"==typeof n||(t[s]=n);return t}function d(e,t){return!c(h(e),h(t))}class u extends i.default.Component{constructor(...e){super(...e),this._instance=void 0,this._element=null,this._runId=0,this._resetChain=Promise.resolve(),this._destroyTimer=null,this._resetDebounceTimer=null,this.state={initError:null},this.setElement=e=>{if(this._element!==e)if(this._cancelScheduledDestroy(),this._element=e,e)this._enqueueReset();else if(this._runId+=1,this._instance){const e=this._instance;this._instance=void 0,this._scheduleDestroy(e)}}}_cancelScheduledDestroy(){null!==this._destroyTimer&&(clearTimeout(this._destroyTimer),this._destroyTimer=null)}_scheduleDestroy(e){this._cancelScheduledDestroy(),this._destroyTimer=setTimeout(()=>{if(this._destroyTimer=null,null===this._element)try{e.destroy()}catch(e){console.error("[EmailMaker] destroy after embed unmount failed",e)}},120)}_enqueueReset(){null!==this._resetDebounceTimer&&clearTimeout(this._resetDebounceTimer),this._resetDebounceTimer=setTimeout(()=>{this._resetDebounceTimer=null,this._runResetNow()},80)}_runResetNow(){const{props:e,_element:t}=this,s=++this._runId;if(t)this._resetChain=this._resetChain.then(async()=>{if(s!==this._runId||t!==this._element)return;const{id:n,className:i,style:r,container:a,...l}=e,c={...l,element:t},h=this._instance?await this._instance.reset(c):await(0,o.init)(c);if(s===this._runId&&t===this._element)this._instance=h,this.setState({initError:null});else if(h!==this._instance)try{h.destroy()}catch(e){console.error("[EmailMaker] discard stale init instance failed",e)}}).catch(e=>{if(s!==this._runId||t!==this._element)return;const n=e instanceof Error?e:new Error(String(e));console.error("[EmailMaker] init/reset failed",n),this.setState({initError:n})});else if(this._instance){const e=this._instance;this._instance=void 0,this._scheduleDestroy(e)}}componentDidUpdate(e){d(e,this.props)&&this._enqueueReset()}componentWillUnmount(){if(this._runId+=1,null!==this._resetDebounceTimer&&(clearTimeout(this._resetDebounceTimer),this._resetDebounceTimer=null),this._instance){const e=this._instance;this._instance=void 0,this._scheduleDestroy(e)}}render(){const{id:e,className:t,style:s,container:n}=this.props,{initError:r}=this.state,a={id:e,className:t,style:s,...n};return r?i.default.createElement("div",Object.assign({},a,{ref:this.setElement,style:{...a.style,display:"flex",alignItems:"center",justifyContent:"center",padding:16,color:"#b42318",fontSize:14,textAlign:"center"}}),r.message):i.default.createElement("div",Object.assign({},a,{ref:this.setElement}))}}var m=n.A;export{m as default};