/**
 * Generated by a build script. Do not modify.
 * Check orginal .jsx file.
 */
/* eslint-disable */

(async (wp2, Drupal2, drupalSettings2) => {
  const { blockEditor, components, compose, element, hooks, i18n } = wp2;
  const { useState, useEffect } = element;
  const { addFilter } = hooks;
  const { createHigherOrderComponent } = compose;
  function getCsrfToken() {
    return drupalSettings2.gutenberg.csrfToken;
  }
  async function cloneIfUsed(contentBlockId, isDuplicate) {
    const options = {
      isDuplicate,
      contentBlockId,
      entityId: drupalSettings2.gutenberg.entityId || null,
      entityType: "node",
      entityBundle: drupalSettings2.gutenberg.nodeType
    };
    const csrfToken = getCsrfToken();
    const response = await fetch(Drupal2.url(`editor/content_block/clone_if_used`), {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-CSRF-Token": csrfToken
      },
      body: JSON.stringify(options)
    });
    const block = await response.json();
    return block.id;
  }
  let contentBlockClientIds = /* @__PURE__ */ new Set();
  let contentBlockIds = /* @__PURE__ */ new Set();
  const handleDuplicateContentBlocks = createHigherOrderComponent((BlockEdit) => {
    return (props) => {
      const { attributes, setAttributes, clientId, name } = props;
      if (name.startsWith("content-block/")) {
        let { contentBlockId } = attributes;
        useEffect(() => {
          if (contentBlockId && !contentBlockClientIds.has(clientId)) {
            console.log(clientId, "Checking clientId");
            contentBlockClientIds.add(clientId);
            cloneIfUsed(contentBlockId, contentBlockIds.has(contentBlockId)).then((clonedId) => {
              if (clonedId == contentBlockId) {
                console.log(contentBlockId, "is unique and unused elsewhere, we can use it.");
                contentBlockIds.add(contentBlockId);
              } else {
                if (clonedId) {
                  console.log(clonedId, "cloned block " + contentBlockId);
                  contentBlockIds.add(clonedId);
                  setAttributes({ contentBlockId: clonedId });
                } else {
                  console.log("must create a new block");
                  setAttributes({ contentBlockId: null });
                }
              }
            });
          }
        });
      }
      return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(BlockEdit, { ...props }));
    };
  }, "handleDuplicateContentBlocks");
  addFilter(
    "editor.BlockEdit",
    "drupalgutenberg/duplicate-content-block-ids",
    handleDuplicateContentBlocks
  );
})(wp, Drupal, drupalSettings);
