const{parse:parse}=wp.blocks,{select:select,subscribe:subscribe}=wp.data;class blockDataReterive{constructor(){this.blockLists=[],this.customBlockTranslateData={},this.customBlocksData=[],this.loaderContainer=null,this.init()}init=()=>{this.fetchCustomBlocks(),this.loaderContainer=document.createElement("div"),this.loaderContainer.className="atfp-overlay",this.loaderContainer.setAttribute("role","status"),this.loaderContainer.setAttribute("aria-live","polite"),this.loaderContainer.innerHTML=this.getOverlayTemplate(),document.body.appendChild(this.loaderContainer),document.body.classList.add("atfp-overlay-open")};getBlocks=blocks=>{const innerBlocks=block=>{const innerBlock=block.innerBlocks;innerBlock.length>0&&innerBlock.forEach(innerBlock=>{this.customBlocksData.push(innerBlock),innerBlocks(innerBlock)})},blockLists=blocks;blockLists.forEach(block=>{innerBlocks(block)}),this.customBlocksData=[...this.customBlocksData,...blockLists],this.getBlockData()};fetchCustomBlocks=()=>{const apiSendData={atfp_nonce:atfp_block_update_object.ajax_nonce,action:atfp_block_update_object.action_get_content},apiUrl=atfp_block_update_object.ajax_url;fetch(apiUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded; charset=UTF-8"},body:new URLSearchParams(apiSendData)}).then(response=>response.json()).then(data=>{if("No custom blocks found."===data.message)return void(this.loaderContainer&&this.loaderContainer.remove());const customBlocks=parse(data.data.block_data);this.getBlocks(customBlocks),this.saveBlockData()}).catch(error=>{this.loaderContainer&&this.loaderContainer.remove(),console.error("Error fetching block rules:",error)})};saveBlockData=()=>{if(Object.keys(this.customBlockTranslateData).length<1)return void(this.loaderContainer&&this.loaderContainer.remove());const apiSendData={atfp_nonce:atfp_block_update_object.ajax_nonce,action:atfp_block_update_object.action_update_content,save_block_data:JSON.stringify(this.customBlockTranslateData)},apiUrl=atfp_block_update_object.ajax_url;fetch(apiUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded; charset=UTF-8"},body:new URLSearchParams(apiSendData)}).then(response=>response.json()).then(data=>{this.setOverlayState("success"),this.teardownOverlay(),data.success&&data.data.message&&console.log(data.data.message)}).catch(error=>{this.setOverlayState("error"),this.teardownOverlay(),console.error("Error fetching block rules:",error)})};nestedAttrValue=idsArr=>{const convertToArrays=obj=>{if("object"!=typeof obj||null===obj)return obj;for(let key in obj)obj.hasOwnProperty(key)&&("object"==typeof obj[key]&&null!==obj[key]&&obj[key].hasOwnProperty("atfp_array_key_replace")?(obj[key]=Object.values(obj[key]),obj[key]=convertToArrays(obj[key])):obj[key]=convertToArrays(obj[key]));return obj},deepMerge=(target,source)=>{for(const key in source)source[key]instanceof Object&&key in target&&Object.assign(source[key],deepMerge(target[key],source[key]));return Object.assign(target||{},source),target};let currentElement={},tempObj=currentElement,lastKey=idsArr[idsArr.length-1];idsArr.slice(0,-1).forEach(key=>{tempObj[key]=tempObj[key]||{},tempObj=tempObj[key]}),tempObj[lastKey]=!0;const obj=convertToArrays(currentElement);deepMerge(this.customBlockTranslateData,obj)};filterAttr=(idsArray,value)=>{null!=value&&(Object.getPrototypeOf(value)===Array.prototype?this.filterBlockArrayAttr(idsArray,value):Object.getPrototypeOf(value)===Object.prototype?this.filterBlockObjectAttr(idsArray,value):"string"==typeof value&&/Make This Content Available for Translation/i.test(value)?this.nestedAttrValue(idsArray,value):value instanceof wp.richText.RichTextData&&/Make This Content Available for Translation/i.test(value.originalHTML)&&this.nestedAttrValue(idsArray,value.originalHTML))};filterBlockArrayAttr=(idsArr,blockData)=>{const newIdArr=new Array(...idsArr);newIdArr.push("atfp_array_key_replace"),blockData.forEach((value,key)=>{("string"==typeof value&&/Make This Content Available for Translation/i.test(value)||![null,void 0].includes(value)&&[Array.prototype,Object.prototype].includes(Object.getPrototypeOf(value)))&&this.filterAttr(newIdArr,value)})};filterBlockObjectAttr=(idsArr,blockData)=>{Object.keys(blockData).forEach(key=>{const newIdArr=new Array(...idsArr),value=blockData[key];null!=value&&("string"==typeof value&&/Make This Content Available for Translation/i.test(value)||[Array.prototype,Object.prototype].includes(Object.getPrototypeOf(value)))&&(newIdArr.push(key),this.filterAttr(newIdArr,blockData[key]))})};filterBlockAttribute=blockData=>{Object.keys(blockData).map(clientId=>{const blockName=Object.keys(blockData[clientId])[0],attributes=blockData[clientId][blockName];Object.keys(attributes).forEach(keytwo=>{const value=attributes[keytwo],idsArray=new Array(blockName,"attributes",keytwo);this.filterAttr(idsArray,value)})})};getBlockData=()=>{if("object"!=typeof this.customBlocksData||0===Object.keys(this.customBlocksData).length)return;const blockData=this.customBlocksData,blockAttributes={};Object.values(blockData).forEach(block=>{Object.values(block.attributes).length>0&&(blockAttributes[block.clientId]={},blockAttributes[block.clientId][block.name]=block.attributes)}),Object.values(blockAttributes).length>0&&this.filterBlockAttribute(blockAttributes)};setOverlayState=state=>{if(!this.loaderContainer)return;const panel=this.loaderContainer.querySelector(".atfp-overlay .atfp-box");panel&&panel.setAttribute("data-state",state)};teardownOverlay=(delayMs=3e3)=>{this.loaderContainer&&setTimeout(()=>{this.loaderContainer.classList.add("atfp-overlay--closing"),setTimeout(()=>{this.loaderContainer.remove(),this.loaderContainer=null,document.body.classList.remove("atfp-overlay-open")},300)},delayMs)};getOverlayTemplate=()=>'\n
\n
\n
\n
\n \n \n \n\n
\n
Saving block content
\n
Supported block content has been updated
\n
Update failed
\n\n
\n Please don’t close or refresh this window until the update is complete.\n
\n
\n Supported block content has been updated. You may continue.\n
\n
\n Something went wrong. You can retry without closing this window.\n
\n
\n
\n\n
\n
\n
\n '}const debounce=(func,delay)=>{let timeoutId;return function(...args){clearTimeout(timeoutId),timeoutId=setTimeout(()=>func.apply(this,args),delay)}};let isBlockContentUpdating=!1;const saveBlockContent=debounce(()=>{new blockDataReterive,isBlockContentUpdating=!1},300);select&&select("core/editor")&&subscribe&&subscribe(()=>{const{isCurrentPostPublished:isCurrentPostPublished,isSavingPost:isSavingPost,isPublishingPost:isPublishingPost,isAutosavingPost:isAutosavingPost}=select("core/editor"),isAutoSaving=isAutosavingPost(),isPublishing=isPublishingPost(),isSaving=isSavingPost(),postPublished=isCurrentPostPublished();!(isPublishing||postPublished&&isSaving)||isAutoSaving||isBlockContentUpdating||(isBlockContentUpdating=!0,saveBlockContent())});