import{getConfig as e,getContext as t,store as o}from"@wordpress/interactivity";function n(e,t){if(i(e)&&i(t)){const o=parseInt(e),n=parseInt(t),i=s(o,n);return`${o/i}:${n/i}`}return`${e}:${t}`}function i(e){const t=Math.floor(Number(e));return t!==1/0&&String(t)===e&&t>0}function s(e,t){return t?s(t,e%t):e}const r=new DOMParser,a=document,c=a.querySelector.bind(a),l=c('dialog[data-wp-interactive="nextgenthemes_arve_dialog"]');!function(){const i=c('[data-wp-interactive^="nextgenthemes"]')?.dataset?.wpInteractive;if(!i)return;const s=e(i),{state:a,actions:d,callbacks:p,helpers:g}=o(i,{state:{isValidLicenseKey:()=>{const e=t();return"valid"===a.options[e.option_key+"_status"]},is32charactersLong:()=>{const e=t();return 32===a.options[e.option_key].length},get isActiveTab(){const e=t();return!e.activeTabs||!0===e?.activeTabs[e.tab]}},actions:{toggleHelp:()=>{a.help=!a.help},openShortcodeDialog:e=>{const t=e.target instanceof HTMLElement&&e.target.dataset.editor;l&&t?(l.dataset.editor=t,l.showModal()):console.error("Dialog or editorId not found")},insertShortcode:()=>{const e=l?.dataset.editor;if(e)if("content"===e)window.wp.media.editor.insert(a.shortcode);else{if(void 0===window.tinymce||!window.tinymce.get(e))return void console.error("TinyMCE not initialized for field: "+e);window.tinymce.get(e).insertContent(a.shortcode)}else console.error("Editor ID not found");d.closeShortcodeDialog()},closeShortcodeDialog:()=>{l&&l.close()},changeTab:()=>{const e=t();for(const t in e.activeTabs)e.activeTabs[t]=!1;e.activeTabs[e.tab]=!0},inputChange:e=>{const o=t(),n=e?.target instanceof HTMLInputElement,i=e?.target instanceof HTMLSelectElement;if(!n&&!i)throw new Error("event.target is not HTMLInputElement or HTMLSelectElement");"arveUrl"in e.target.dataset?g.extractFromEmbedCode(e.target.value):a.options[o.option_key]=e.target.value,d.saveOptions()},checkboxChange:e=>{const o=t();if(!(e?.target instanceof HTMLInputElement&&"checkbox"===e?.target?.type))throw new Error("event.target is not HTMLInputElement type=checkbox");a.options[o.option_key]=e?.target.checked,d.saveOptions()},selectImage:()=>{l&&l.close();const e=t(),o=window.wp.media({title:"Upload Image",multiple:!1}).open().on("select",function(){const t=o.state().get("selection").first().toJSON().id;a.options[e.option_key]=t,l&&l.showModal()}).on("close",function(){l&&l.showModal()})},deleteCaches:()=>{const e=t();d.restCall(s.restUrl+"/delete-caches",{type:e.type,prefix:e.prefix,like:e.like,not_like:e.type,delete_option:e.delete_option})},saveOptionsReal:()=>{d.restCall(s.restSettingsUrl,{[i]:a.options})},restCall:(e,t,o=!1)=>{if(a.isSaving)return void(a.message="trying to save too fast");a.isSaving=!0,a.message="Saving…";let n=!1;fetch(e,{method:"POST",body:JSON.stringify(t),headers:{"Content-Type":"application/json","X-WP-Nonce":s.nonce}}).then(e=>(e.ok||(n=!0),e.json())).then(e=>{n&&(a.message="Error:",a.debug=JSON.stringify(e,null,2)),n||setTimeout(()=>a.message="",666)}).catch(e=>{a.message=e.message}).finally(()=>{a.isSaving=!1,o&&window.location.reload()})},eddLicenseAction(){const e=t();d.restCall(s.restUrl+"/edd-license-action",{option_key:e.option_key,edd_store_url:e.edd_store_url,edd_action:e.edd_action,item_id:e.edd_item_id,license:a.options[e.option_key]},!0)},resetOptionsSection(){const e=t().tab;Object.entries(s.defaultOptions).forEach(([t,o])=>{"all"===e?Object.entries(o).forEach(([e,t])=>{a.options[e]=t}):Object.entries(o).forEach(([o,n])=>{t===e&&(a.options[o]=n)})}),d.saveOptionsReal()}},callbacks:{updateShortcode(){let e="";for(const[t,o]of Object.entries(a.options))"credentialless"===t?!1===o&&(e+=`${t}="false" `):!0===o?e+=`${t}="true" `:o&&(e+=`${t}="${o}" `);a.shortcode="[arve "+e+"/]"}},helpers:{debugJson:e=>{a.debug=JSON.stringify(e,null,2)},extractFromEmbedCode:e=>{const t=r.parseFromString(e,"text/html").querySelector("iframe"),o=t&&t.getAttribute("src");if(o&&(e=o,t.width&&t.height)){const e=n(t.width,t.height);"16:9"!==e&&(a.options.aspect_ratio=e)}a.options.url=e}}});d.saveOptions=function(e){let t;return function(...o){const n=this;clearTimeout(t),t=window.setTimeout(()=>{t=void 0,e.apply(n,o)},1111)}}(d.saveOptionsReal),"nextgenthemes_arve_dialog"===i&&(d.saveOptions=()=>{})}(),function(){const e=window.getComputedStyle(a.body).backgroundColor,t=c(".wrap--nextgenthemes");t&&t.setAttribute("style",`--ngt-wp-body-bg: ${e};`)}(),window.jQuery(document).on("click",'.arve-btn:not([data-editor="content"])',e=>{e.preventDefault();const t=c('[data-wp-on--click="actions.openShortcodeDialog"][data-editor="content"]'),o=c('[data-wp-on--click="actions.insertShortcode"]');t&&o&&l?t.dispatchEvent(new Event("click")):console.error("Open btn, insert btn or dialog not found")});