"use strict";(()=>{const{createBlock:createBlock}=wp.blocks,{dispatch:dispatch,select:select}=wp.data;class atfpCreateNewBlock{constructor(){this.updateBlockStore={},this.loaderRemove=null,this.loader=null,this.replaceAttributes=null,this.updateBlockId=new Array}copyTranslateText=()=>{const selection=window.getSelection(),range=document.createRange();range.selectNodeContents(document.getElementById("atfp-copy-text")),selection.removeAllRanges(),selection.addRange(range),document.execCommand("copy"),selection.removeAllRanges()};noticeInitialize=()=>{dispatch("core/notices").createNotice("info",'To enable translation, please include the Make This Content Available for Translation text in your block content. For help, watch the video and click "Copy Text" to use. Then, paste it into the section of your block you want automatically translated',{isDismissible:!1,id:"atfp-notice-id",actions:[{label:"Watch Video.",url:`${atfpAddBlockVars.atfp_demo_page_url}#custom-block-translate`}],__unstableHTML:!0}).then(()=>{const targetAnchor=document.querySelector(`a[href^="${atfpAddBlockVars.atfp_demo_page_url}#custom-block-translate"]`);targetAnchor&&targetAnchor.addEventListener("click",e=>{e.preventDefault(),window.open(targetAnchor.href,"_blank")})})};copyBtnInitialize=()=>{const copyBtn=document.createElement("div");copyBtn.id="atfp-copy-btn",copyBtn.innerHTML="Copy Text",copyBtn.addEventListener("click",this.copyTranslateText),copyBtn.ariaLabel="Copy Text",copyBtn.title='Click to copy the text "Make This Content Available for Translation"';const copyText=document.createElement("div");copyText.id="atfp-copy-text",copyText.innerHTML="Make This Content Available for Translation",document.body.appendChild(copyBtn),document.body.appendChild(copyText)};addBlockInitialize=newBlock=>{this.newBlock=newBlock,this.creteNewBlock(),this.skeletonLoader()};removeLoader=()=>{clearTimeout(this.loaderRemove),this.loaderRemove=setTimeout(()=>{this.loader&&this.loader.remove()},2e3)};creteNewBlock=()=>{const newBlock=createBlock(this.newBlock);this.updateBlockData(newBlock)};updateBlockData=async Block=>{await dispatch("core/block-editor").insertBlocks([Block]),setTimeout(()=>{const blockWrp=document.getElementById(`block-${Block.clientId}`);blockWrp&&blockWrp.appendChild(this.loader)},100),setTimeout(()=>{this.updateBlockContent(Block)},400)};updateBlockContent=Block=>{const newBlock=document.getElementById(`block-${Block.clientId}`);newBlock&&this.updateContent(newBlock)};updateContent=async ele=>{const element=ele;let i=1;if(this.removeLoader(),element)if("true"==element.contentEditable&&element.children.length<2)element.innerHTML="Make This Content Available for Translation";else{const innerElements=element.getElementsByTagName("*");for(let innerElement of innerElements)i===innerElements.length&&(this.removeLoader(),setTimeout(()=>{this.updateBlockFromStore()},500)),i++,["SCRIPT","STYLE","META","LINK","TITLE","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT"].includes(innerElement.tagName)||innerElement.childNodes.length>0&&innerElement.childNodes.forEach(child=>{child.nodeType===Node.TEXT_NODE&&this.updateBlockAttr(innerElement,child)})}};updateBlockAttr=(innerElement,child)=>{let blockId=!1;if(innerElement.classList.contains("wp-block"))blockId=innerElement.dataset.block;else{const parentBlock=innerElement.closest(".wp-block");parentBlock&&(blockId=parentBlock.dataset.block)}const blockAttributes=select("core/block-editor").getBlockAttributes(blockId);let index=0;if(!this.updateBlockStore[blockId]){let attributes=JSON.parse(JSON.stringify(blockAttributes));this.updateBlockStore[blockId]={attributes:attributes},this.updateBlockStore[blockId].updateBlockData={}}const updateNestedAttributes=async(attributes,child)=>{const updateAttributes=async key=>{if(index++,"string"==typeof attributes[key]&&""!==attributes[key].trim()&&(attributes[key].trim()===child.textContent.trim()||attributes[key]===child.textContent.trim())){const originalValue=attributes[key],newValue="Make This Content Available for Translation "+index;this.updateBlockStore[blockId].updateBlockData[newValue.replace(/\s+/g,"-")]=originalValue,attributes[key]=newValue}};if("object"==typeof attributes&&null!==attributes)for(const key of Object.keys(attributes))await updateAttributes(key),"object"==typeof attributes[key]&&null!==attributes[key]&&await updateNestedAttributes(attributes[key],child)},blockStoreAttributes=this.updateBlockStore[blockId].attributes;updateNestedAttributes(blockStoreAttributes,child)};updateBlockFromStore=()=>{const blockStoreAttributes=this.updateBlockStore;Object.keys(blockStoreAttributes).forEach(blockId=>{const blockAttributes=blockStoreAttributes[blockId].attributes;this.removeLoader(),dispatch("core/block-editor").updateBlockAttributes(blockId,blockAttributes).then(()=>{clearTimeout(this.replaceAttributes),this.replaceAttributes=setTimeout(()=>{this.removeLoader();const blockIds=Object.keys(this.updateBlockStore);this.replaceBlockContent(blockIds[0])},500)})})};replaceBlockContent=blockId=>{const blockStoreAttributes=this.updateBlockStore,checkValidAttributes=(value=!1,blockId)=>{const blockElement=document.querySelector(`#block-${blockId}`),regex=new RegExp(value,"g"),matchFound=regex.test(blockElement.innerText);return matchFound},blockAttributes=blockStoreAttributes[blockId].attributes,upateNestedAttributes=async attributes=>{const updateAttributes=async key=>{if("string"==typeof attributes[key]&&attributes[key].includes("Make This Content Available for Translation"))try{const keyWithDashes=attributes[key].replace(/\s+/g,"-"),originalValue=this.updateBlockStore[blockId].updateBlockData[keyWithDashes],status=checkValidAttributes(attributes[key],blockId);attributes[key]=status?"Make This Content Available for Translation":originalValue}catch(e){console.log(`${attributes[key]} is not valid JSON.`)}};if("object"==typeof attributes&&null!==attributes)for(const key of Object.keys(attributes))await updateAttributes(key),"object"==typeof attributes[key]&&null!==attributes[key]&&await upateNestedAttributes(attributes[key])};upateNestedAttributes(blockAttributes),setTimeout(()=>{dispatch("core/block-editor").updateBlockAttributes(blockId,blockAttributes).then(()=>{const blockIds=Object.keys(this.updateBlockStore);if(blockIds.length>0){this.updateBlockId.push(blockId),dispatch("core/block-editor").selectBlock(null),this.removeLoader();const firstBlockId=blockIds.find(id=>!this.updateBlockId.includes(id));firstBlockId&&this.replaceBlockContent(firstBlockId)}})},500)};skeletonLoader=()=>{const loader=document.createElement("div"),loaderContainer=()=>{const container='';return'
'+container+'
'};loader.className="atfp-loader-wrapper",loader.innerHTML=loaderContainer(),this.loader=loader}}window.addEventListener("load",()=>{const atfpCreateBlockObj=new atfpCreateNewBlock;atfpCreateBlockObj.copyBtnInitialize(),atfpCreateBlockObj.noticeInitialize();const urlParams=new URLSearchParams(window.location.search);let newBlock="";urlParams.has("atfp_new_block")&&""!==urlParams.get("atfp_new_block").trim()&&(newBlock=urlParams.get("atfp_new_block"),atfpCreateBlockObj.addBlockInitialize(newBlock))})})();