(()=>{"use strict";var e,a={252:()=>{const e=window.wp.blocks,a=window.wp.components,o=window.ReactJSXRuntime,n=(0,o.jsx)(a.SVG,{xmlns:"http://www.w3.org/2000/svg",width:"200",height:"200",viewBox:"0 0 256 256",children:(0,o.jsx)(a.Path,{fill:"currentColor",d:"M208 48a87.48 87.48 0 0 0-35.36 7.43c-15.1-25.37-39.92-38-41.06-38.59a8 8 0 0 0-7.16 0c-1.14.58-26 13.22-41.06 38.59A87.48 87.48 0 0 0 48 48a8 8 0 0 0-8 8v40a88.11 88.11 0 0 0 80 87.63v35.43l-36.42-18.22a8 8 0 1 0-7.16 14.32l48 24a8 8 0 0 0 7.16 0l48-24a8 8 0 0 0-7.16-14.32L136 219.06v-35.43A88.11 88.11 0 0 0 216 96V56a8 8 0 0 0-8-8ZM56 96V64.44A72.1 72.1 0 0 1 120 136v31.56A72.1 72.1 0 0 1 56 96Zm144 0a72.1 72.1 0 0 1-64 71.56V136a72.1 72.1 0 0 1 64-71.56Z"})}),t=window.wp.i18n,i=window.wp.blockEditor,r=window.wp.element,l=window.wp.data,s=JSON.parse('{"UU":"create-block/image-block-for-pollinations-ai"}');(0,e.registerBlockType)(s.UU,{edit:function({setAttributes:n,clientId:s}){const[c,d]=(0,r.useState)(!1),[p,g]=(0,r.useState)(""),[m,h]=(0,r.useState)(""),[u,w]=(0,r.useState)(""),{insertBlock:b}=(0,l.useDispatch)(i.store),{innerBlocks:f}=(0,l.useSelect)((e=>({innerBlocks:e(i.store).getBlocks(s)})),[s]),k=()=>{g(""),h(""),w(""),d(!1)};return(0,o.jsxs)("div",{...(0,i.useBlockProps)(),children:[!f.length&&(0,o.jsxs)("div",{className:"form-container",children:[(0,o.jsx)(a.TextareaControl,{rows:3,label:(0,t.__)("Prompt Text","image-block-for-pollinations-ai"),value:p||"",help:`${(0,t.__)("Characters left:","image-block-for-pollinations-ai")} ${475-p.length}`,disabled:!!c,onChange:e=>{(e=>{e.length<=475&&g(e)})(e)}}),!m&&(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(a.Button,{className:"is-primary",disabled:!!c,onClick:()=>(async()=>{if(d(!0),p)try{const e=await fetch(`https://image.pollinations.ai/prompt/${encodeURIComponent(p)}?nologo=true`,{method:"GET"}),a=await e.blob(),o=URL.createObjectURL(a);h(o)}catch(e){w(`<strong>${(0,t.__)("Error generating image:","image-block-for-pollinations-ai")}</strong><br/> ${e}`)}finally{d(!1)}else d(!1)})(),children:c?(0,t.__)("Loading…","image-block-for-pollinations-ai"):(0,t.__)("Generate Image","image-block-for-pollinations-ai")}),c&&(0,o.jsx)(a.Spinner,{})]}),""!==m&&(0,o.jsxs)("div",{className:"buttons-row",children:[(0,o.jsx)(a.Button,{className:"is-primary",onClick:()=>{d(!0),fetch(m).then((e=>e.blob())).then((a=>{const o=new window.FileReader;o.onloadend=function(){const a=o.result.split(",")[1];wp.ajax.send("ibpai_save_image",{data:{prompt_text:p,image_data:a,nonce:ibpaiMediaGenerator.nonce},success(a){n({generatedImageId:a.attachment_id,generatedImageURL:a.url,promptText:p});const o=(0,e.createBlock)("core/image",{id:a.attachment_id,sizeSlug:"large",url:a.url});b(o,f.length,s),d(!1),k()},error(e){w(`<strong>${(0,t.__)("Error saving image:","image-block-for-pollinations-ai")}</strong><br/> ${e}`)},finally(){d(!1)}})},o.readAsDataURL(a)}))},children:(0,t.__)("Save and insert","image-block-for-pollinations-ai")}),(0,o.jsx)(a.Button,{className:"is-secondary",onClick:()=>{k()},children:(0,t.__)("Cancel","image-block-for-pollinations-ai")})]}),u&&(0,o.jsx)("div",{className:"has-error",dangerouslySetInnerHTML:{__html:u}}),""!==m&&(0,o.jsx)("img",{src:m,width:"100%",height:"auto",alt:"",className:"generated-image"})]}),(0,o.jsx)(i.InnerBlocks,{allowedBlocks:["core/image"],renderAppender:!1})]})},save:function(){return(0,o.jsx)("div",{...i.useBlockProps.save(),children:(0,o.jsx)(i.InnerBlocks.Content,{templateLock:"all"})})},icon:n})}},o={};function n(e){var t=o[e];if(void 0!==t)return t.exports;var i=o[e]={exports:{}};return a[e](i,i.exports,n),i.exports}n.m=a,e=[],n.O=(a,o,t,i)=>{if(!o){var r=1/0;for(d=0;d<e.length;d++){for(var[o,t,i]=e[d],l=!0,s=0;s<o.length;s++)(!1&i||r>=i)&&Object.keys(n.O).every((e=>n.O[e](o[s])))?o.splice(s--,1):(l=!1,i<r&&(r=i));if(l){e.splice(d--,1);var c=t();void 0!==c&&(a=c)}}return a}i=i||0;for(var d=e.length;d>0&&e[d-1][2]>i;d--)e[d]=e[d-1];e[d]=[o,t,i]},n.o=(e,a)=>Object.prototype.hasOwnProperty.call(e,a),(()=>{var e={57:0,350:0};n.O.j=a=>0===e[a];var a=(a,o)=>{var t,i,[r,l,s]=o,c=0;if(r.some((a=>0!==e[a]))){for(t in l)n.o(l,t)&&(n.m[t]=l[t]);if(s)var d=s(n)}for(a&&a(o);c<r.length;c++)i=r[c],n.o(e,i)&&e[i]&&e[i][0](),e[i]=0;return n.O(d)},o=globalThis.webpackChunkimage_block_for_pollinations_ai=globalThis.webpackChunkimage_block_for_pollinations_ai||[];o.forEach(a.bind(null,0)),o.push=a.bind(null,o.push.bind(o))})();var t=n.O(void 0,[350],(()=>n(252)));t=n.O(t)})();