"use strict";
function aiomatic_uniq_fast(a) 
{
    var seen = {};
    var out = [];
    var len = a.length;
    var j = 0;
    for(var i = 0; i < len; i++) {
        var item = a[i];
        var jsit = JSON.stringify(item);
        if(seen[jsit] !== 1) {
              seen[jsit] = 1;
              out[j++] = item;
        }
    }
    return out;
}
(() => {
	var t, e = {
			401: () => {
				const t = window.wp.element,
					e = window.wp.richText,
					o = window.wp.blockEditor,
					r = window.wp.components;
				async function c() {
					let t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "below",
						e = l(),
						[o, r] = p(e),
						c = r.clientId,
						a = o.clientId,
						n = wp.data.select("core/block-editor").getBlock(c),
						s = '<span id="' + (Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)) + '" class="aiomatic-editor-loading"></span>';
					if ("above" === t) {
						let t = wp.blocks.createBlock("core/paragraph", {
								content: s
							}),
							e = wp.data.select("core/block-editor").getBlockIndex(a),
							o = wp.data.select("core/block-editor").getBlockRootClientId(a);
						return await wp.data.dispatch("core/block-editor").insertBlock(t, e, o), t
					}
					if (e.length > 1 || "core/paragraph" !== n.name) {
						let t = wp.blocks.createBlock("core/paragraph", {
								content: s
							}),
							e = wp.data.select("core/block-editor").getBlockRootClientId(c),
							o = wp.data.select("core/block-editor").getBlockIndex(c) + 1;
						if (!wp.data.select("core/block-editor").canInsertBlockType("core/paragraph", e))
							for (; e && (o = wp.data.select("core/block-editor").getBlockIndex(e) + 1, e = wp.data.select("core/block-editor").getBlockRootClientId(e), !wp.data.select("core/block-editor").canInsertBlockType("core/paragraph", e)););
						return await wp.data.dispatch("core/block-editor").insertBlock(t, o, e), t
					}
					let d = wp.data.select("core/block-editor").getBlockRootClientId(c);
					if (!wp.data.select("core/block-editor").canInsertBlockType("core/paragraph", d)) {
						for (; d && (d = wp.data.select("core/block-editor").getBlockRootClientId(d), !wp.data.select("core/block-editor").canInsertBlockType("core/paragraph", d)););
						let t = wp.blocks.createBlock("core/paragraph", {
							content: s
						});
						return await wp.data.dispatch("core/block-editor").insertBlock(t, void 0, d), t
					}
					let w = i(n),
						u = wp.richText.create({
							html: w
						}),
						g = w.length;
					"offset" in r && (g = r.offset);
					let b = wp.richText.slice(u, 0, g),
						k = wp.richText.slice(u, g, u.text.length),
						h = wp.richText.toHTMLString({
							value: b
						}),
						f = wp.richText.toHTMLString({
							value: k
						}),
						m = n.attributes;
					const v = r.attributeKey;
					let B = m;
					B[v] = h;
					const I = wp.blocks.createBlock(n.name, B);
					let x = m;
					x[v] = s;
					let _ = wp.blocks.createBlock("core/paragraph", x),
						T = m;
					T[v] = f;
					let y = [I, _, wp.blocks.createBlock(n.name, T)];
					return 0 === k.text.trim().length && (y = [I, _]), await wp.data.dispatch("core/block-editor").replaceBlock(c, y), _
				}

				function n() {
					let t = l(),
						[e, o] = p(t);
					return s(t, e, o).trim()
				}

				function l() {
					let t = wp.data.select("core/block-editor").getMultiSelectedBlockClientIds();
					return 0 === t.length && (t = [wp.data.select("core/block-editor").getSelectedBlockClientId()]), t
				}

				function i(t) {
					let e = "";
					return "content" in t.attributes ? e = t.attributes.content : "citation" in t.attributes ? e = t.attributes.citation : "value" in t.attributes ? e = t.attributes.value : "values" in t.attributes ? e = t.attributes.values : "text" in t.attributes && (e = t.attributes.text), e
				}

				function s(t, e, o) {
					let r = "";
					return t.forEach((t => {
						const c = wp.data.select("core/block-editor").getBlock(t);
						let a = i(c),
							n = wp.richText.create({
								html: a
							}).text,
							l = 0,
							p = n.length;
						e.clientId === t && "offset" in e && (l = e.offset), o.clientId === t && "offset" in o && (p = o.offset), n = n.substring(l, p), r += "\n" + n, c.innerBlocks.length > 0 && (r += s(c.innerBlocks.map((t => t.clientId))))
					})), r
				}

				function p(t) {
					const e = wp.data.select("core/block-editor").getSelectionStart(),
						o = wp.data.select("core/block-editor").getSelectionEnd();
					if (e.clientId === o.clientId) return [e, o];
					let r = e,
						c = o;
					return t.length > 0 && t[0] === o.clientId && (r = o, c = e), [r, c]
				}

				function d() {
					let t = n();
					return t.length > 0 && t
				}(0, e.registerFormatType)("aiomatic/custom-buttons", {
					title: "Aimogen Content Wizard",
					tagName: "aiomatic",
					className: null,
					edit: e => {
						let {
							isActive: n,
							onChange: l,
							value: i
						} = e, s = [];
						if ("object" == typeof aiomatic && "object" == typeof aiomatic.prompts) 
            {
							for (let operation in aiomatic.prompts) 
              {
                  let prompt = aiomatic.prompts[operation];
                  let icon = 'text';
                  if(prompt[1] == 'image')
                  {
                      icon = 'format-image';
                  }
                  s.push({
                    title: operation,
                    icon: icon,
                    onClick: async function() {
                        var selectedText = d();
                        var send_prompt = prompt[0].replace('%%selected_text%%', selectedText);
                        if(send_prompt.includes('%%'))
                        {
                          if (wp && wp.data) 
                          {
                              const postId = wp.data.select("core/editor").getCurrentPostId();
                              const ajaxurl = aiomatic.ajaxurl;
                              const nonce = aiomatic.nonce;
                              const xdata = new FormData();
                              xdata.append( 'postId', postId );
                              xdata.append( 'nonce', nonce );
                              xdata.append( 'send_prompt', send_prompt );
                              xdata.append( 'action', 'aiomatic_shortcode_replacer' );
                              const response = await fetch(ajaxurl, {
                                  method: 'POST',
                                  body: xdata
                              }).catch(async error => {
                                  console.log('An exception occurred: ' + error.text());
                              })

                              if (!response.ok) 
                              {
                                  console.log('An error occurred: ' + response.text());
                              }
                              else
                              {
                                  const ret = await response.json();
                                  if (ret.message !== undefined) {
                                      console.log('A general error occurred: ' + response.text());
                                  }
                                  else
                                  {
                                      send_prompt = ret.data.content;
                                  }
                              }
                          }
                        }
                        var block = await aiomaticCreateBlockForAutocompletion(aiomatic.placement);
                        if(icon == 'text')
                        {
                            await aiomaticAutocomplete(block, send_prompt);
                        }
                        else
                        {
                            if(icon == 'format-image')
                            {
                                await aiomaticImager(block, send_prompt);
                            }
                        }
                    },
                  });
							}
						}
            s = aiomatic_uniq_fast(s);
						return (0, t.createElement)(o.BlockControls, null, (0, t.createElement)(r.ToolbarGroup, null, (0, t.createElement)(r.ToolbarDropdownMenu, {
							className: "aiomatic_editor_icon",
							label: "Aimogen Content Wizard",
              icon: '',
							controls: s
						})))
					}
				})
			}
		},
		o = {};

	function r(t) {
		var c = o[t];
		if (void 0 !== c) return c.exports;
		var a = o[t] = {
			exports: {}
		};
		return e[t](a, a.exports, r), a.exports
	}
	r.m = e, t = [], r.O = (e, o, c, a) => {
		if (!o) {
			var n = 1 / 0;
			for (p = 0; p < t.length; p++) {
				for (var [o, c, a] = t[p], l = !0, i = 0; i < o.length; i++)(!1 & a || n >= a) && Object.keys(r.O).every((t => r.O[t](o[i]))) ? o.splice(i--, 1) : (l = !1, a < n && (n = a));
				if (l) {
					t.splice(p--, 1);
					var s = c();
					void 0 !== s && (e = s)
				}
			}
			return e
		}
		a = a || 0;
		for (var p = t.length; p > 0 && t[p - 1][2] > a; p--) t[p] = t[p - 1];
		t[p] = [o, c, a]
	}, r.o = (t, e) => Object.prototype.hasOwnProperty.call(t, e), (() => {
		var t = {
			826: 0,
			431: 0
		};
		r.O.j = e => 0 === t[e];
		var e = (e, o) => {
				var c, a, [n, l, i] = o,
					s = 0;
				if (n.some((e => 0 !== t[e]))) {
					for (c in l) r.o(l, c) && (r.m[c] = l[c]);
					if (i) var p = i(r)
				}
				for (e && e(o); s < n.length; s++) a = n[s], r.o(t, a) && t[a] && t[a][0](), t[a] = 0;
				return r.O(p)
			},
			o = globalThis.webpackChunkgutenpride = globalThis.webpackChunkgutenpride || [];
		o.forEach(e.bind(null, 0)), o.push = e.bind(null, o.push.bind(o))
	})();
	var c = r.O(void 0, [431], (() => r(401)));
	c = r.O(c)
})();

async function aiomaticCreateBlockForAutocompletion(placement) {
  let selectedBlockClientIds = aiomaticgetSelectedBlockClientIds();
  let [selectionStart, selectionEnd] = aiomaticGetAdjustedSelections(selectedBlockClientIds);
  let lastBlockClientId = selectionEnd.clientId;
  let firstBlockClientId = selectionStart.clientId;
  let lastBlock = wp.data.select('core/block-editor').getBlock(lastBlockClientId);
  let loadingSpinner = aiomaticCreateLoadingSpinner();
  if (placement === 'above') {
    let autoCompleteBlock = wp.blocks.createBlock('core/paragraph', {
      content: loadingSpinner
    }); 

    let index = wp.data.select('core/block-editor').getBlockIndex(firstBlockClientId);

    let parentClientId = wp.data.select('core/block-editor').getBlockRootClientId(firstBlockClientId); 

    await wp.data.dispatch('core/block-editor').insertBlock(autoCompleteBlock, index, parentClientId);
    return autoCompleteBlock;
  } 
  if (selectedBlockClientIds.length > 1 || lastBlock.name !== 'core/paragraph') {

    let autoCompleteBlock = wp.blocks.createBlock('core/paragraph', {
      content: loadingSpinner
    });
    let parentBlockClientId = wp.data.select('core/block-editor').getBlockRootClientId(lastBlockClientId);
    let indexToInsertAt = wp.data.select('core/block-editor').getBlockIndex(lastBlockClientId) + 1;

    if (!wp.data.select('core/block-editor').canInsertBlockType('core/paragraph', parentBlockClientId)) {
      while (parentBlockClientId) {
        indexToInsertAt = wp.data.select('core/block-editor').getBlockIndex(parentBlockClientId) + 1;
        parentBlockClientId = wp.data.select('core/block-editor').getBlockRootClientId(parentBlockClientId);

        if (wp.data.select('core/block-editor').canInsertBlockType('core/paragraph', parentBlockClientId)) {
          break;
        }
      }
    } 
    await wp.data.dispatch('core/block-editor').insertBlock(autoCompleteBlock, indexToInsertAt, parentBlockClientId);
    return autoCompleteBlock;
  }

  let parentBlockClientId = wp.data.select('core/block-editor').getBlockRootClientId(lastBlockClientId);

  if (!wp.data.select('core/block-editor').canInsertBlockType('core/paragraph', parentBlockClientId)) {

    while (parentBlockClientId) {
      parentBlockClientId = wp.data.select('core/block-editor').getBlockRootClientId(parentBlockClientId);

      if (wp.data.select('core/block-editor').canInsertBlockType('core/paragraph', parentBlockClientId)) {
        break;
      }
    }

    let autoCompleteBlock = wp.blocks.createBlock('core/paragraph', {
      content: loadingSpinner
    }); 

    await wp.data.dispatch('core/block-editor').insertBlock(autoCompleteBlock, undefined, parentBlockClientId);
    return autoCompleteBlock;
  }

  let lastBlockContent = aiomaticExtractBlockContent(lastBlock);
  let richText = wp.richText.create({
    html: lastBlockContent
  });
  let start = 0;
  let end = lastBlockContent.length;

  if ('offset' in selectionEnd) {
    end = selectionEnd.offset;
  }

  let firstPart = wp.richText.slice(richText, start, end);
  let secondPart = wp.richText.slice(richText, end, richText.text.length);
  let firstPartContent = wp.richText.toHTMLString({
    value: firstPart
  });
  let secondPartContent = wp.richText.toHTMLString({
    value: secondPart
  });
  let inheritedAttributes = lastBlock.attributes;

  const key = selectionEnd.attributeKey;
  let firstBlockAttributes = inheritedAttributes;
  firstBlockAttributes[key] = firstPartContent;
  const firstPartBlock = wp.blocks.createBlock(lastBlock.name, firstBlockAttributes); 

  let autoCompleteAttributes = inheritedAttributes;
  autoCompleteAttributes[key] = loadingSpinner;
  let autoCompleteBlock = wp.blocks.createBlock('core/paragraph', autoCompleteAttributes);

  let secondBlockAttributes = inheritedAttributes;
  secondBlockAttributes[key] = secondPartContent;
  const secondPartBlock = wp.blocks.createBlock(lastBlock.name, secondBlockAttributes);
  let replacementBlocks = [firstPartBlock, autoCompleteBlock, secondPartBlock];

  if (secondPart.text.trim().length === 0) {
    replacementBlocks = [firstPartBlock, autoCompleteBlock];
  } 


  await wp.data.dispatch('core/block-editor').replaceBlock(lastBlockClientId, replacementBlocks);
  return autoCompleteBlock;
}

function aiomaticCreateLoadingSpinner() {
  const loadingSpinnerId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
  let spinnersrc = "data:image/gif;base64, ";
  let spinnerHtml = '<p id="' + loadingSpinnerId + '" class="aiomatic-mce-loading"><img src="' + spinnersrc + '"></p>';
  return spinnerHtml;
}
async function aiomaticAutocomplete(autocompleteBlock, selectedText) {
  let autocompletedText = '';

  try {
    autocompletedText = await aiomatic_doAutocompleteRequest(selectedText);
  } catch (error) {
    await wp.data.dispatch('core/block-editor').removeBlocks(autocompleteBlock.clientId);
    alert('An API error occurred with the following response body: \n\n' + error.message);
    return;
  }
  if(autocompletedText === undefined)
  {
      await wp.data.dispatch('core/block-editor').removeBlocks(autocompleteBlock.clientId);
      alert('Incorrect response by AI API!');
      return;
  }
  const autocompletedTextWithLineBreaks = autocompletedText.replace(/\n/g, '<br>');
  
  await wp.data.dispatch('core/block-editor').updateBlockAttributes(autocompleteBlock.clientId, { isLoading: true });
  setTimeout(async () => {
    await wp.data.dispatch('core/block-editor').updateBlockAttributes(autocompleteBlock.clientId, { content: autocompletedTextWithLineBreaks, isLoading: false });
    
    wp.data.dispatch('core/block-editor').selectBlock(autocompleteBlock.clientId);
    wp.data.dispatch('core/block-editor').clearSelectedBlock();
  }, 100);
}
async function aiomaticImager(autocompleteBlock, selectedText) {
  let autocompletedText = '';

  try {
    autocompletedText = await aiomatic_doImageRequest(selectedText);
  } catch (error) {
    await wp.data.dispatch('core/block-editor').removeBlocks(autocompleteBlock.clientId);
    alert('An API error occurred with the following response body: \n\n' + error.message);
    return;
  }
  if(autocompletedText === undefined)
  {
      await wp.data.dispatch('core/block-editor').removeBlocks(autocompleteBlock.clientId);
      alert('Incorrect response returned by AI API!');
      return;
  }
  
  await wp.data.dispatch('core/block-editor').updateBlockAttributes(autocompleteBlock.clientId, { isLoading: true });
  setTimeout(async () => {
    await wp.data.dispatch('core/block-editor').updateBlockAttributes(autocompleteBlock.clientId, { content: autocompletedText, isLoading: false });
    
    wp.data.dispatch('core/block-editor').selectBlock(autocompleteBlock.clientId);
    wp.data.dispatch('core/block-editor').clearSelectedBlock();
  }, 100);
}

const aiomatic_doAutocompleteRequest = async function (text) {
  const ajaxurl = aiomatic.ajaxurl;
  const nonce = aiomatic.nonce;
  const xdata = new FormData();
  xdata.append( 'prompt', text );
  xdata.append( 'nonce', nonce );
  xdata.append( 'action', 'aiomatic_editor' );
  const response = await fetch(ajaxurl, {
      method: 'POST',
      body: xdata
  }).catch(async error => {
      throw new Error(await error.text());
  })

  if (!response.ok) {
      throw new Error(await response.text());
  }

  const ret = await response.json();
  if (ret.message !== undefined) {
      throw new Error(await response.text());
  }
  return ret.data.content;
}
const aiomatic_doImageRequest = async function (text) {
  const ajaxurl = aiomatic.ajaxurl;
  const nonce = aiomatic.nonce;
  const xdata = new FormData();
  xdata.append( 'prompt', text );
  xdata.append( 'nonce', nonce );
  xdata.append( 'action', 'aiomatic_imager' );
  const response = await fetch(ajaxurl, {
      method: 'POST',
      body: xdata
  }).catch(async error => {
      throw new Error(await error.text());
  })

  if (!response.ok) {
      throw new Error(await response.text());
  }

  const ret = await response.json();
  if (ret.message !== undefined) {
      throw new Error(await response.text());
  }
  return ret.data.content;
}
function aiomaticgetSelectedBlockClientIds() {
  let selectedBlockClientIds = wp.data.select('core/block-editor').getMultiSelectedBlockClientIds();

  if (selectedBlockClientIds.length === 0) {
    selectedBlockClientIds = [wp.data.select('core/block-editor').getSelectedBlockClientId()];
  }

  return selectedBlockClientIds;
}
function aiomaticGetAdjustedSelections(selectedBlockClientIds) {
  const selectionStart = wp.data.select('core/block-editor').getSelectionStart();
  const selectionEnd = wp.data.select('core/block-editor').getSelectionEnd();

  if (selectionStart.clientId === selectionEnd.clientId) {
    return [selectionStart, selectionEnd];
  }

  let adjustedSelectionStart = selectionStart;
  let adjustedSelectionEnd = selectionEnd;

  if (selectedBlockClientIds.length > 0 && selectedBlockClientIds[0] === selectionEnd.clientId) {
    adjustedSelectionStart = selectionEnd;
    adjustedSelectionEnd = selectionStart;
  }

  return [adjustedSelectionStart, adjustedSelectionEnd];
}
function aiomaticExtractBlockContent(block) {
  let content = '';
  if ('content' in block.attributes) {
    content = block.attributes.content;
  } else if ('citation' in block.attributes) {
    content = block.attributes.citation;
  } else if ('value' in block.attributes) {
    content = block.attributes.value;
  } else if ('values' in block.attributes) {
    content = block.attributes.values;
  } else if ('text' in block.attributes) {
    content = block.attributes.text;
  }
  return content;
}