import { openCommentBoardSidebar, createFloatingIcons } from '../utils';
const { dispatch, select } = wp.data;

window.addEventListener( 'load', async () => {
  await cfDelay(500);  
  await openCommentBoard();
  const boardIds = wp.data.select('mdstore').getBoardIds();
  if(boardIds.length > 0){
    openCommentBoardSidebar();
  }
  
  //set dynamic sidebar color
  const iframe = cfGetIframe();
  const iframeDocument = iframe ? iframe.contentWindow.document : null;
  let bodyStyle, editorBackgroundColor;
  if (iframe) {
    setTimeout(function () {
      iframe.contentDocument?.body.classList.add('multicollab_body_class');
      iframe.contentDocument?.body.classList.add('commentOn');
    }, 500);
    
    bodyStyle = getComputedStyle(iframe.contentDocument.body);
    if(bodyStyle){  
      editorBackgroundColor = bodyStyle.getPropertyValue("background-color");
    }
  } else {
    
    bodyStyle = document.querySelector(".editor-styles-wrapper");
    if(bodyStyle){  
      editorBackgroundColor =
        getComputedStyle(bodyStyle).getPropertyValue("background-color");
    }
  }
  document.documentElement.style.setProperty(
    "--editor-background-color",
    editorBackgroundColor
  );

  
  let commentBoardToggle = wp.data.select("core/editor").getEditedPostAttribute("meta")?._sb_show_comment_boards;
  if ("1" !== multicollabRolePermissions.hide_comment) {
    const body = document.body;
  
    if (!commentBoardToggle && body.classList.contains("hide-comments")) {
      document.body.classList.remove("hide-comments");
      if(iframeDocument){
        iframe.contentDocument?.body.classList.remove('hide-comments');
      }
    } else if (commentBoardToggle && !body.classList.contains("hide-comments") && "1" !== multicollabRolePermissions.hide_comment) {
      body.classList.add("hide-comments");
      if(iframeDocument){
        iframe.contentDocument?.body.classList.add("hide-comments");
      }
    }
  }
  
  /**
   * Handles events for the floating button in the editor.
   */
  if (iframeDocument) {
    iframeDocument.addEventListener('click', handleEventForFloatingButton);
    iframeDocument.addEventListener('keydown', handleEventForFloatingButton);
  }else{
    document.addEventListener('click', handleEventForFloatingButton);
    document.addEventListener('keydown', handleEventForFloatingButton);
  }
  function handleEventForFloatingButton(event){
    if (event.target.matches('.is-selected') || event.target.closest('.is-selected')) {
      // Your event handler logic here
      if ('0' === multicollab_floating_icons.cf_hide_floating_icons) { 
        const html = getSelectionHtml();
        const selection = window.getSelection();
        let focusedElement;
        let selectedText;
        if(iframeDocument){
          focusedElement = iframeDocument?.activeElement?.localName;
          selectedText = iframeDocument.defaultView.getSelection().toString();
        } else{
          focusedElement = document.activeElement.localName;
          selectedText = selection.toString();
        }
        
        const selectedTextLength = selectedText.length;
        const selectedBlock = wp.data.select("core/block-editor").getSelectedBlock();

        if(selectedTextLength){
          if (document.querySelectorAll('.cf-floating-wrapper').length < 1) {
            var prefix = 'core/';
            if (
              ( null !== selectedBlock ) &&
              selectedBlock?.name !== 'core/freeform'
              && selectedBlock?.name.startsWith(prefix)
            ) {
              createFloatingIcons(focusedElement);
            }
          }
        } else {
          removeFloatingIcon();
        }
      
      }
    }
  }

});