(()=>{"use strict";var e={n:l=>{var a=l&&l.__esModule?()=>l.default:()=>l;return e.d(a,{a}),a},d:(l,a)=>{for(var o in a)e.o(a,o)&&!e.o(l,o)&&Object.defineProperty(l,o,{enumerable:!0,get:a[o]})},o:(e,l)=>Object.prototype.hasOwnProperty.call(e,l)};const l=window.wp.blocks,a=window.wp.data,o=window.wp.element,t=window.wp.components,s=window.wp.blockEditor,i=window.wp.i18n,r=window.wp.apiFetch;var n=e.n(r);const c=window.ReactJSXRuntime;(0,l.registerBlockType)("ultimate-faq-solution/block",{apiVersion:3,title:"FAQ",icon:"editor-help",category:"widgets",attributes:{group:{type:"string",default:""},exclude:{type:"array",default:[]},behaviour:{type:"string",default:""},elements_order:{type:"string",default:""},hideTitle:{type:"boolean",default:!1}},edit({attributes:e,setAttributes:l,isSelected:r,context:u,clientId:d}){const[h,p]=(0,o.useState)([]),[w,f]=(0,o.useState)(""),[g,_]=(0,o.useState)(!1),{selectBlock:v}=(0,a.useDispatch)("core/block-editor"),b=(0,s.useBlockProps)(),x=Array.isArray(e.behaviour)?"":e.behaviour||"",m=Array.isArray(e.elements_order)?"":e.elements_order||"";(0,o.useEffect)((()=>{n()({path:"/wp/v2/ufaqsw?per_page=100"}).then((e=>{p(e)}))}),[]),(0,o.useEffect)((()=>{if(e.group){_(!0);const l=`/ufaqsw-preview/?${new URLSearchParams({group:e.group,exclude:e.exclude.join(","),behaviour:x,elements_order:m,hideTitle:e.hideTitle?"1":"0"}).toString()}`;f(l)}else f(""),_(!1)}),[e.group,e.exclude,e.behaviour,e.elements_order,e.hideTitle]);const q=[{label:(0,i.__)("Choose a behaviour","ufaqsw"),value:""},{label:(0,i.__)("Accordion","ufaqsw"),value:"accordion"},{label:(0,i.__)("Toggle","ufaqsw"),value:"toggle"}],j=[{label:(0,i.__)("Choose an order","ufaqsw"),value:""},{label:(0,i.__)("ASC","ufaqsw"),value:"asc"},{label:(0,i.__)("DESC","ufaqsw"),value:"desc"}],y=e=>{e.preventDefault(),e.stopPropagation(),v(d)};return(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(s.InspectorControls,{children:(0,c.jsxs)(t.PanelBody,{title:(0,i.__)("FAQ Settings","ufaqsw"),children:[(0,c.jsx)(t.SelectControl,{label:(0,i.__)("Select FAQ Group","ufaqsw"),help:(0,i.__)("Choose a group to display FAQs from.","ufaqsw"),__next40pxDefaultSize:!0,value:e.group,options:h?[{label:(0,i.__)("Select a FAQ group","ufaqsw"),value:""},{label:(0,i.__)("All","ufaqsw"),value:"all"}].concat(h.map((e=>({label:e.title,value:String(e.id)})))):[{label:(0,i.__)("Loading...","ufaqsw"),value:""}],onChange:e=>l({group:e})}),(0,c.jsx)(t.CheckboxControl,{label:(0,i.__)("Hide group title","ufaqsw"),help:(0,i.__)("Check to hide the group title. This will override the faq group setting.","ufaqsw"),checked:e.hideTitle,onChange:e=>l({hideTitle:e})}),"all"===e.group&&(0,c.jsx)(t.SelectControl,{label:"Behaviour",help:"Choose a behaviour for the FAQ display. This will override the faq group settings.",__next40pxDefaultSize:!0,value:x,options:q,onChange:e=>l({behaviour:e})}),(0,c.jsx)(t.SelectControl,{label:"Order",__next40pxDefaultSize:!0,value:m,options:j,onChange:e=>l({elements_order:e})}),"all"===e.group&&(0,c.jsx)(t.PanelBody,{title:"Exclude Individual FAQ Groups",children:h&&h.map((a=>(0,c.jsx)(t.CheckboxControl,{label:a.title,checked:e.exclude.includes(a.id),onChange:o=>{const t=o?[...e.exclude,a.id]:e.exclude.filter((e=>e!==a.id));l({exclude:t})}},a.id)))})]})}),(0,c.jsx)("div",{...b,children:e.group?(0,c.jsxs)("div",{className:"ufaqsw-block-wrapper",children:[r&&(0,c.jsx)("div",{className:"ufaqsw-block-controls",children:(0,c.jsx)("div",{className:"ufaqsw-shortcode-display",children:(0,c.jsx)("label",{children:(0,i.__)("Configure the block from right panel - FAQ Settings","ufaqsw")})})}),(0,c.jsxs)("div",{className:"ufaqsw-iframe-container",children:[g&&(0,c.jsxs)("div",{className:"ufaqsw-loading",children:[(0,c.jsx)(t.Spinner,{}),(0,c.jsx)("span",{children:(0,i.__)("Loading FAQ content...","ufaqsw")})]}),w&&(0,c.jsx)("iframe",{src:w,className:"ufaqsw-content-preview-iframe "+(g?"loading":""),title:(0,i.__)("FAQ Preview","ufaqsw"),frameBorder:"0",width:"100%",height:"400",onLoad:()=>{_(!1);const e=document.querySelector(".ufaqsw-content-preview-iframe");if(e)try{const l=e.contentDocument||e.contentWindow.document,a=Math.max(l.body.scrollHeight,300);e.style.height=a+"px"}catch(l){e.style.height="400px"}}}),!r&&!g&&(0,c.jsx)("div",{className:"ufaqsw-iframe-overlay",onMouseDown:y,onClick:y})]})]}):(0,c.jsxs)("div",{className:"components-placeholder is-large",children:[(0,c.jsxs)("div",{className:"components-placeholder__label",children:[(0,c.jsx)("span",{className:"faq-block-icon dashicons dashicons-editor-help",role:"img","aria-label":"FAQ Icon"})," FAQ Block"]}),(0,c.jsx)("div",{className:"components-placeholder__instructions",children:(0,i.__)("Configure the block from right panel - FAQ Settings","ufaqsw")})]})})]})},save:()=>null})})();