(()=>{"use strict";var e,t={43:(e,t,n)=>{var r={};n.r(r),n.d(r,{getSettingsPageURL:()=>p,isConnected:()=>u});var o={};n.r(o),n.d(o,{getHighlightBlocks:()=>h});var s={};n.r(s),n.d(s,{removeHighlightBlocks:()=>f,setHighlightBlocks:()=>g});var i={};n.r(i),n.d(i,{close:()=>N,open:()=>B,requestAI:()=>F,reset:()=>D,setContext:()=>x,setError:()=>A,setInput:()=>j,setInsertionPlace:()=>M,setLoading:()=>R,setResponse:()=>L,setScreen:()=>T,toggle:()=>P});var a={};n.r(a),n.d(a,{getContext:()=>U,getError:()=>W,getInput:()=>G,getInsertionPlace:()=>q,getLoading:()=>K,getProgress:()=>Z,getResponse:()=>V,getScreen:()=>$,isOpen:()=>H});const{connected:c,settingsPageURL:l}=window.mindData;function u(){return"1"===c}function p(){return l}const d=window.wp.data,m=(0,d.createReduxStore)("mind",{selectors:r,reducer:e=>e});(0,d.register)(m);function h(e){return e?.highlightBlocks||[]}function g(e){return{type:"SET_HIGHLIGHT_BLOCKS",highlightBlocks:e}}function f(e){return{type:"REMOVE_HIGHLIGHT_BLOCKS",removeBlocks:e}}const E=(0,d.createReduxStore)("mind/blocks",{reducer:function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{highlightBlocks:[]},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};switch(t.type){case"SET_HIGHLIGHT_BLOCKS":if(t.highlightBlocks&&t.highlightBlocks.length)return{...e,highlightBlocks:[...e.highlightBlocks,...t.highlightBlocks]};break;case"REMOVE_HIGHLIGHT_BLOCKS":if(e.highlightBlocks&&e.highlightBlocks.length&&t.removeBlocks&&t.removeBlocks.length)return{...e,highlightBlocks:e.highlightBlocks.filter((e=>!t.removeBlocks.includes(e)))}}return e},selectors:o,actions:s});(0,d.register)(E);const _={isOpen:!1,input:"",context:["selected-blocks","page"],insertionPlace:"",screen:"",loading:!1,response:"",error:null,progress:{charsProcessed:0,queueSize:0,isComplete:!1}},b=window.wp.i18n,k=window.wp.apiFetch;var v=n.n(k);function w(e){return" \r\n\t".indexOf(e)>=0}const y=window.wp.blocks;class O{constructor(e){this.dispatch=e,this.contentBuffer="",this.decoder=new TextDecoder,this.isJsonStarted=!1,this.jsonBuffer="",this.throttledDispatch=this.throttle(this.performDispatch.bind(this),200)}throttle(e,t){let n;return function(...r){n||(e.apply(this,r),n=!0,setTimeout((()=>n=!1),t))}}async processStream(e){try{for(;;){const{value:t,done:n}=await e.read();if(n)break;await this.processChunk(t)}}catch(e){this.handleError(e)}}async processChunk(e){const t=this.decoder.decode(e,{stream:!0}).split("\n");for(const e of t)if(e.startsWith("data: "))try{const t=e.slice(6),n=JSON.parse(t);if(n.error){this.handleError(n);break}if(!0===n.done)return void(this.jsonBuffer&&await this.parseAndDispatchBlocks(this.jsonBuffer,!0));if(!n.content)continue;await this.processContent(n.content)}catch(e){}}async processContent(e){if(this.contentBuffer+=e,this.isJsonStarted)if(e.includes("```")){const t=e.indexOf("```");this.jsonBuffer+=e.substring(0,t),await this.parseAndDispatchBlocks(this.jsonBuffer,!0),this.isJsonStarted=!1,this.jsonBuffer=""}else this.jsonBuffer+=e,await this.tryParseIncomplete(this.jsonBuffer);else if(this.contentBuffer.includes("```json")){this.isJsonStarted=!0;const[,e]=this.contentBuffer.split("```json");this.jsonBuffer=e||""}}async tryParseIncomplete(e){try{if(!e||!e.trim().startsWith("["))return;const t=function(e){for(var t,n,r,o=["topLevel"],s=0,i=function(e){return o.push(e)},a=function(e){return o[o.length-1]=e},c=function(e){null==t&&(t=s,n=o.length,r=e)},l=function(e){e===r&&(t=void 0,n=void 0,r=void 0)},u=function(){return o.pop()},p=function(){return s--},d=function(e){if("0"<=e&&e<="9")i("number");else switch(e){case'"':return void i("string");case"-":return void i("numberNeedsDigit");case"t":return void i("true");case"f":return void i("false");case"n":return void i("null");case"[":return void i("arrayNeedsValue");case"{":return void i("objectNeedsKey")}},m=e.length;s"9")&&(p(),u());break;case"numberNeedsDigit":a("number");break;case"numberNeedsExponent":a("+"===h||"-"===h?"numberNeedsDigit":"number");break;case"true":case"false":case"null":(h<"a"||h>"z")&&(p(),u());break;case"arrayNeedsValue":"]"===h?u():w(h)||(l("collectionItem"),a("arrayNeedsComma"),d(h));break;case"arrayNeedsComma":"]"===h?u():","===h&&(c("collectionItem"),a("arrayNeedsValue"));break;case"objectNeedsKey":"}"===h?u():'"'===h&&(c("collectionItem"),a("objectNeedsColon"),i("string"));break;case"objectNeedsColon":":"===h&&a("objectNeedsValue");break;case"objectNeedsValue":w(h)||(l("collectionItem"),a("objectNeedsComma"),d(h));break;case"objectNeedsComma":"}"===h?u():","===h&&(c("collectionItem"),a("objectNeedsKey"))}}null!=n&&(o.length=n);for(var g=[null!=t?e.slice(0,t):e],f=function(t){return g.push(t.slice(e.length-e.lastIndexOf(t[0])))},E=o.length-1;E>=0;E--)switch(o[E]){case"string":g.push('"');break;case"numberNeedsDigit":case"numberNeedsExponent":g.push("0");break;case"true":f("true");break;case"false":f("false");break;case"null":f("null");break;case"arrayNeedsValue":case"arrayNeedsComma":g.push("]");break;case"objectNeedsKey":case"objectNeedsColon":case"objectNeedsValue":case"objectNeedsComma":g.push("}")}return g.join("")}(e);if(!t)return;const n=JSON.parse(t);if(Array.isArray(n)&&n.length>0){const e=n.map((e=>this.transformToBlock(e))).filter(Boolean);e.length>0&&await this.dispatchBlocks(e,!1)}}catch(e){}}async parseAndDispatchBlocks(e,t=!1){try{const n=JSON.parse(e),r=Array.isArray(n)?n.map((e=>this.transformToBlock(e))).filter(Boolean):[this.transformToBlock(n)].filter(Boolean);r.length>0&&await this.dispatchBlocks(r,t)}catch(n){t||await this.tryParseIncomplete(e)}}transformToBlock(e){if(!e?.name)return null;try{const t=Array.isArray(e.innerBlocks)?e.innerBlocks.map((e=>this.transformToBlock(e))).filter(Boolean):[],n=e.attributes||{};return(0,y.createBlock)(e.name,n,t)}catch(e){return null}}performDispatch(e,t){this.dispatch({type:t?"REQUEST_AI_SUCCESS":"REQUEST_AI_CHUNK",payload:{response:e,progress:{charsProcessed:this.contentBuffer.length,blocksCount:e.length,isComplete:t}}})}async dispatchBlocks(e,t=!1){t?this.performDispatch(e,!0):this.throttledDispatch(e,!1)}handleError(e){this.dispatch({type:"REQUEST_AI_ERROR",payload:e.message})}}function C(e){e?.attributes?.metadata&&delete e.attributes.metadata,e?.attributes?.patternName&&delete e.attributes.patternName;const t={clientId:e.clientId,name:e.name,attributes:e.attributes,innerBlocks:[]};return e.innerBlocks.length&&e.innerBlocks.forEach((e=>{t.innerBlocks.push(C(e))})),t}function S(e){const{getBlock:t,getSelectedBlockClientIds:n}=wp.data.select("core/block-editor"),r=n(),o=[];return r.forEach((e=>{const n=t(e);n?.name&&n?.attributes&&o.push(C(n))})),e?JSON.stringify(o):o}function I(){const e=S();return!(!e||!e.length||1===e.length&&"core/paragraph"===e[0].name&&!e[0].attributes.content.trim())}function B(){return{type:"OPEN"}}function N(){return{type:"CLOSE"}}function P(){return{type:"TOGGLE"}}function j(e){return{type:"SET_INPUT",input:e}}function x(e){return{type:"SET_CONTEXT",context:e}}function M(e){return{type:"SET_INSERTION_PLACE",insertionPlace:e}}function T(e){return{type:"SET_SCREEN",screen:e}}function R(e){return{type:"SET_LOADING",loading:e}}function L(e){return{type:"SET_RESPONSE",response:e}}function A(e){return{type:"SET_ERROR",error:e}}function D(){return{type:"RESET"}}function F(){return async({dispatch:e,select:t})=>{if(u){if(!t.getLoading())try{e({type:"REQUEST_AI_PENDING"});const n={request:t.getInput()};t.getContext().includes("selected-blocks")&&I()&&(n.selected_blocks=S(!0)),t.getContext().includes("page")&&(n.page_blocks=function(){const{getBlocks:e}=wp.data.select("core/block-editor"),t=e(),n=[];return t.forEach((e=>{e?.name&&e?.attributes&&n.push(C(e))})),JSON.stringify(n)}(),n.page_context=function(){const{getCurrentPost:e}=wp.data.select("core/editor"),t=e(),n={title:t.title,type:t.type,slug:t.slug,link:t.link,status:t.status,date:t.date_gmt,modified:t.modified_gmt,excerpt:t.excerpt};return JSON.stringify(n)}());const r=new O(e),o=await v()({path:"/mind/v1/request_ai",method:"POST",data:n,parse:!1});if(!o.ok){const e=await o.json();throw new Error(e.message||(0,b.__)("Failed to fetch AI response","mind"))}await r.processStream(o.body.getReader())}catch(t){e({type:"REQUEST_AI_ERROR",payload:t.message||(0,b.__)("Failed to fetch AI response","mind")})}}else e(A((0,b.__)("Not connected","mind")))}}function H(e){return e?.isOpen||!1}function G(e){return e?.input||""}function U(e){return e?.context||[]}function q(e){return e?.insertionPlace||""}function $(e){return e?.screen||""}function K(e){return e?.loading||!1}function Z(e){return e?.progress||!1}function V(e){return e?.response||[]}function W(e){return e?.error||!1}const J=(0,d.createReduxStore)("mind/popup",{reducer:function(e=_,t={}){switch(t.type){case"CLOSE":if(e.isOpen)return{...e,isOpen:!1};break;case"OPEN":if(!e.isOpen){const t={...e,isOpen:!0};return t.context.includes("selected-blocks")||(t.context=[...t.context,"selected-blocks"]),t}break;case"TOGGLE":return{...e,isOpen:!e.isOpen};case"SET_INPUT":if(e.input!==t.input)return{...e,input:t.input};break;case"SET_CONTEXT":if(e.context!==t.context)return{...e,context:t.context};break;case"SET_INSERTION_PLACE":if(e.insertionPlace!==t.insertionPlace)return{...e,insertionPlace:t.insertionPlace};break;case"SET_SCREEN":if(e.screen!==t.screen)return{...e,screen:t.screen};break;case"SET_LOADING":if(e.loading!==t.loading)return{...e,loading:t.loading};break;case"SET_RESPONSE":if(e.response!==t.response)return{...e,response:t.response};break;case"SET_ERROR":if(e.error!==t.error)return{...e,error:t.error};break;case"REQUEST_AI_PENDING":return{...e,isOpen:!0,loading:!0,response:[],error:null,screen:"request",progress:_.progress};case"REQUEST_AI_CHUNK":return{...e,loading:!0,response:t.payload.response,progress:t.payload.progress};case"REQUEST_AI_SUCCESS":return{...e,loading:!1,response:t.payload.response,progress:{...t.payload.progress,isComplete:!0}};case"REQUEST_AI_ERROR":return{...e,loading:!1,response:[],error:t.payload||"",progress:_.progress};case"RESET":return{...e,input:"",insertionPlace:"",screen:"",response:!1,error:!1,loading:!1}}return e},actions:i,selectors:a});(0,d.register)(J);const Q=window.React,z=window.wp.element,X=window.wp.components,Y=window.wp.domReady;var ee=n.n(Y);function te(e){var t,n,r="";if("string"==typeof e||"number"==typeof e)r+=e;else if("object"==typeof e)if(Array.isArray(e))for(t=0;t{const{isOpen:t,getInput:n,getScreen:r,getLoading:o,getResponse:s}=e("mind/popup");return{isOpen:t(),input:n(),screen:r(),loading:o(),response:s()}})),E=f?.length>0;return(0,z.useEffect)((()=>{p&&!g&&r?.current&&r.current.focus()}),[p,g,r]),(0,z.useEffect)((()=>{""===h&&m&&l("request")}),[h,m,l]),(0,z.useEffect)((()=>{if(r?.current){r.current.style.height="0px";const e=r.current.scrollHeight;r.current.style.height=e+"px"}}),[r,m,g,E,s]),(0,z.useEffect)((()=>{const e=n&&!o.current;if(o.current=n,e){const e=[];return[100,200,300].forEach((t=>{const n=setTimeout((()=>{i((e=>e+1))}),t);e.push(n)})),()=>{e.forEach((e=>clearTimeout(e)))}}}),[n]),(0,Q.createElement)("div",{className:"mind-popup-input"},(0,Q.createElement)(se,null),(0,Q.createElement)("textarea",{ref:r,placeholder:(0,b.__)("Ask AI to build or change blocks…","mind"),value:m,onChange:e=>{c(e.target.value)},onKeyDown:function(e){""===h||"Backspace"!==e.key||e.target.value?f?.length>0&&"Enter"===e.key&&!e.shiftKey?t():"request"!==h||"Enter"!==e.key||e.shiftKey||(e.preventDefault(),m&&u()):a()},disabled:g,rows:1}))}function ae(){return(0,z.createElement)("div",{className:"mind-popup-loading-line"},(0,z.createElement)("span",null))}function ce(e){const{type:t,children:n}=e;return(0,z.createElement)("div",{className:"mind-popup-notice "+(t?`mind-popup-notice-${t}`:"")},n)}const le=window.lodash,ue=window.wp.blockEditor;function pe({response:e}){return(0,Q.createElement)("div",{className:"mind-popup-response__preview"},(0,Q.createElement)(ue.BlockPreview,{blocks:[{name:"core/group",clientId:"a9b75f7e-55c7-4f2b-93bb-00cf24181278",isValid:!0,attributes:{align:"full",layout:{type:"constrained"},className:"alignfull"},innerBlocks:e}],viewportWidth:0,additionalStyles:[{css:"\n\t\t\t\t\t\t\t.is-root-container > div {\n\t\t\t\t\t\t\t\tmargin-top: 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t"}]}))}const de=(0,z.memo)((function({response:e,loading:t}){const[n,r]=(0,z.useState)(1),[o,s]=(0,z.useState)([]),[i,a]=(0,z.useState)([]),c=(0,z.useRef)(null);return(0,z.useEffect)((()=>{if(e.length)return c.current&&clearTimeout(c.current),1===n?a(e):s(e),c.current=setTimeout((()=>{r(1===n?2:1)}),50),()=>{c.current&&clearTimeout(c.current)}}),[e]),e.length||t?(0,Q.createElement)("div",{className:ne("mind-popup-response",`mind-popup-response--${n}`)},(o.length>0||i.length>0)&&(0,Q.createElement)(Q.Fragment,null,(0,Q.createElement)(pe,{response:o}),(0,Q.createElement)(pe,{response:i}))):null}),((e,t)=>(0,le.isEqual)(e.response,t.response)&&e.loading===t.loading));function me(){const e=(0,z.useRef)(),{setScreen:t}=(0,d.useDispatch)("mind/popup"),{isOpen:n,input:r,screen:o,loading:s,response:i,progress:a,error:c}=(0,d.useSelect)((e=>{const{isOpen:t,getInput:n,getScreen:r,getLoading:o,getResponse:s,getProgress:i,getError:a}=e("mind/popup");return{isOpen:t(),input:n(),screen:r(),loading:o(),response:s(),progress:i(),error:a()}}));return(0,z.useEffect)((()=>{n&&!s&&e?.current&&function(){if(e?.current){const t=e.current.querySelector("input");t&&t.focus()}}()}),[n,s,e]),(0,z.useEffect)((()=>{""===o&&r&&t("request")}),[o,r,t]),(0,Q.createElement)("div",{className:"mind-popup-content"},"request"===o&&(0,Q.createElement)("div",{className:"mind-popup-request"},i?.length>0&&(0,Q.createElement)(de,{progress:a,loading:s,response:i}),!s&&c&&(0,Q.createElement)(ce,{type:"error"},c)))}function he(e){return(0,z.createElement)("span",{className:"mind-popup-loading-text"},e.children)}function ge(e){const{onInsert:t}=e,{close:n,reset:r,setContext:o,setError:s,requestAI:i}=(0,d.useDispatch)("mind/popup"),{input:a,context:c,loading:l,response:u,insertionPlace:p}=(0,d.useSelect)((e=>{const{getInput:t,getContext:n,getLoading:r,getResponse:o,getInsertionPlace:s}=e("mind/popup");return{input:t(),context:n(),loading:r(),response:o(),insertionPlace:s()}})),m=[{name:(0,b.__)("Page","mind"),tooltip:(0,b.__)("Provide page context","mind"),value:"page"},!!I()&&{name:(0,b.__)("Blocks","mind"),tooltip:(0,b.__)("Provide selected blocks context","mind"),value:"selected-blocks"}],h=!l&&!u?.length;return(0,Q.createElement)("div",{className:"mind-popup-footer"},(0,Q.createElement)("div",null,(0,Q.createElement)("div",{className:"mind-popup-footer-context"},m.map((e=>e&&(h||c.includes(e.value))?(0,Q.createElement)(X.Tooltip,{delay:500,placement:"top",key:e.value,text:e.tooltip},(0,Q.createElement)("button",{key:e.value,disabled:!h,className:c.includes(e.value)?"active":"",onClick:()=>{c.includes(e.value)?o(c.filter((t=>t!==e.value))):o([...c,e.value])}},e.name,(0,Q.createElement)("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"14",height:"14",fill:"currentColor"},(0,Q.createElement)("path",{d:"M19 11h-6V5h-2v6H5v2h6v6h2v-6h6z"})))):null)))),(0,Q.createElement)("div",{className:"mind-popup-footer-actions"},!l&&0===u?.length&&(0,Q.createElement)(X.Tooltip,{placement:"top",text:(0,b.__)("Ask AI and get answer","mind")},(0,Q.createElement)(X.Button,{className:"mind-popup-footer-actions-primary mind-popup-footer-actions-icon",onClick:()=>{i()},"aria-label":(0,b.__)("Get Answer","mind"),disabled:!a},"→")),l&&(0,Q.createElement)(X.Button,{className:"mind-popup-footer-actions-primary",disabled:!0},(0,Q.createElement)(he,null,(0,b.__)("Loading","mind"))),u?.length>0&&!l&&(0,Q.createElement)(Q.Fragment,null,(0,Q.createElement)(X.Button,{onClick:()=>{s(""),i()}},(0,b.__)("Regenerate","mind")," ",(0,Q.createElement)("kbd",null,"↻")),(0,Q.createElement)(X.Button,{onClick:()=>{window.navigator.clipboard.writeText((0,y.serialize)(u))}},(0,b.__)("Copy","mind")),"selected-blocks"===p&&I()&&(0,Q.createElement)(X.Button,{onClick:()=>t("insert")},(0,b.__)("Insert","mind")," ",(0,Q.createElement)("kbd",null,"⏎")),(0,Q.createElement)(X.Button,{className:"mind-popup-footer-actions-primary",onClick:t},"selected-blocks"===p&&I()?(0,b.__)("Replace Selected Blocks","mind"):(0,b.__)("Insert","mind")," ",(0,Q.createElement)("kbd",null,"⏎")))))}var fe;function Ee(){return Ee=Object.assign?Object.assign.bind():function(e){for(var t=1;t{const{getSettingsPageURL:t}=e("mind");return{settingsPageURL:t()}}));return(0,Q.createElement)("div",{className:"mind-popup-connected-screen"},(0,Q.createElement)("h2",null,(0,Q.createElement)(_e,null),(0,b.__)("AI API Key","mind")),(0,Q.createElement)("div",null,(0,Q.createElement)("p",null,(0,b.__)("In order to use Mind, you will need to provide your Anthropic or OpenAI API key. Please insert your API key in the plugin settings to get started.","mind"))),(0,Q.createElement)("div",null,(0,Q.createElement)("a",{className:"mind-popup-connected-screen-button",href:e,target:"_blank",rel:"noreferrer"},(0,b.__)("Go to Settings","mind"))))}const ke="mind-popup-container";function ve(){const{setHighlightBlocks:e}=(0,d.useDispatch)("mind/blocks"),{close:t,reset:n}=(0,d.useDispatch)("mind/popup"),[r,o]=(0,z.useState)(!1),[s,i]=(0,z.useState)(null),{connected:a,isOpen:c,insertionPlace:l,loading:u,response:p}=(0,d.useSelect)((e=>{const{isConnected:t}=e("mind"),{isOpen:n,getInsertionPlace:r,getLoading:o,getResponse:s}=e("mind/popup");return{connected:t(),isOpen:n(),insertionPlace:r(),loading:o(),response:s()}})),{selectedClientIds:m}=(0,d.useSelect)((e=>{const{getSelectedBlockClientIds:t}=e("core/block-editor");return{selectedClientIds:t()}}),[]),h=(0,z.useRef)();(0,z.useEffect)((()=>{if(!c||!h.current)return;if((!u&&!p?.length||r||s)&&(u||p?.length||!r||s))return;const{height:e}=h.current.children[0].getBoundingClientRect();i({height:`${e}px`}),setTimeout((()=>{i(null),o(!r)}),10)}),[r,u,p,c,s]);const{insertBlocks:g,replaceBlocks:f}=(0,d.useDispatch)("core/block-editor");function E(){p.length&&("selected-blocks"===l&&m&&m.length?f(m,p):g(p),e(p.map((e=>e.clientId)))),n(),t(),o(!1)}return c?(0,Q.createElement)(X.Modal,{ref:h,title:!1,className:ne("mind-popup",!a&&"mind-popup-not-connected"),overlayClassName:"mind-popup-overlay",onRequestClose:()=>{n(),t(),o(!1)},isFullScreen:r,style:s,__experimentalHideHeader:!0},a?(0,Q.createElement)(Q.Fragment,null,(0,Q.createElement)(ie,{onInsert:E,isFullscreen:r}),u&&(0,Q.createElement)(ae,null),(0,Q.createElement)(me,null),(0,Q.createElement)(ge,{onInsert:E})):(0,Q.createElement)(be,null)):null}ee()((()=>{if(document.querySelector(`.${ke}`))return;const e=document.querySelector(".block-editor");if(!e)return;const t=document.createElement("div");t.classList.add(ke),e.appendChild(t),(0,z.createRoot)(t).render((0,Q.createElement)(ve,null))}));const we=window.wp.hooks,ye=window.wp.compose;var Oe;function Ce(){return Ce=Object.assign?Object.assign.bind():function(e){for(var t=1;t