// VibePost Ai brand patch in MAIN world for popup roots, including closed shadow DOM
(function () {
  var SUPPORT_URL = 'https://wa.me/8801927341627';
  var LICENSE_PLACEHOLDER = 'VibePost-XXXX-XXXX-XXXX';
  var BRAND_TEXT = 'VibePost Ai';
  var FOOTER_TEXT = 'Developed by VibePost Ai';
  var roots = new Set();

  function addRoot(root) { if (root && !roots.has(root)) roots.add(root); }
  addRoot(document);

  var originalAttachShadow = Element.prototype.attachShadow;
  if (!window.__aiguruShadowPatched && originalAttachShadow) {
    window.__aiguruShadowPatched = true;
    Element.prototype.attachShadow = function(init) {
      var root = originalAttachShadow.call(this, init);
      addRoot(root);
      return root;
    };
  }

  function cleanText(value) {
    if (!value) return value;
    return value
      .replace(/Developed by\s*VibeX\s*Academy/gi, FOOTER_TEXT)
      .replace(/Developed by\s*EliteSub/gi, FOOTER_TEXT)
      .replace(/Developed by\s*AI\s*GURU/gi, FOOTER_TEXT)
      .replace(/VibeX\s*Academy/gi, BRAND_TEXT)
      .replace(/\bEliteSub\b/gi, BRAND_TEXT)
      .replace(/\bAI\s*GURU\b/gi, BRAND_TEXT)
      .replace(/\bAIGURU\b/gi, BRAND_TEXT)
      .replace(/\bVIBEX\b/gi, BRAND_TEXT)
      .replace(/\bVibeX\b/gi, BRAND_TEXT)
      .replace(/(EliteSub|AIGURU|AI-GURU|VIBEX)-XXXX-XXXX-XXXX/gi, LICENSE_PLACEHOLDER);
  }

  function patchNode(root) {
    if (!root) return;
    var elementList = [];
    if (root.querySelectorAll) elementList = Array.prototype.slice.call(root.querySelectorAll('*'));

    elementList.forEach(function(el) {
      if (el.shadowRoot) addRoot(el.shadowRoot);

      if (el.childNodes && el.childNodes.length) {
        Array.prototype.forEach.call(el.childNodes, function(node) {
          if (node && node.nodeType === 3) {
            var next = cleanText(node.nodeValue || '');
            if (next !== node.nodeValue) node.nodeValue = next;
          }
        });
      }

      if (el.matches) {
        if (el.matches('.ql-brand-text, .sp-brand-text, .ql-title, .ql-brand')) {
          if (/VibeX|VIBEX|EliteSub|AI\s*GURU|AIGURU/i.test(el.textContent || '')) {
            el.textContent = BRAND_TEXT;
          }
        }
        if (el.matches('#ql-native-badge, .ql-native-badge, [id*="native-badge"], [class*="native-badge"]')) {
          var span = el.querySelector('span');
          if (span) {
            if (span.textContent !== 'VibePost Ai Native Active') {
              span.textContent = 'VibePost Ai Native Active';
            }
          } else {
            Array.prototype.forEach.call(el.childNodes, function(node) {
              if (node.nodeType === 3 && /VibeX\s*Academy|VibePost\s*Ai/i.test(node.nodeValue)) {
                var next = node.nodeValue.replace(/VibeX\s*Academy|VibePost\s*Ai/gi, 'VibePost Ai Native Active');
                if (node.nodeValue !== next) node.nodeValue = next;
              }
            });
          }
        }
        if (el.matches('.ql-footer-version, .sp-footer-badge, [data-i18n="footer.brand"]')) {
          el.textContent = FOOTER_TEXT;
        }
        if (el.matches('input')) {
          var placeholder = el.getAttribute('placeholder') || '';
          if (/VIBEX|VibeX|AI-?GURU|AIGURU|EliteSub|XXXX-XXXX-XXXX/i.test(placeholder)) {
            el.setAttribute('placeholder', LICENSE_PLACEHOLDER);
          }
          if (/^(VIBEX|AI-?GURU|AIGURU|EliteSub)/i.test(el.value || '')) {
            el.value = (el.value || '').replace(/^(VIBEX|AI-?GURU|AIGURU|EliteSub)/i, 'VibePost');
          }
        }
        if (el.matches('a[href*="wa.me/"], .ql-support-link, .sp-support-link')) {
          el.setAttribute('href', SUPPORT_URL);
          el.setAttribute('target', '_blank');
          el.setAttribute('rel', 'noopener noreferrer');
        }
      }
    });
  }

  function runPatch() { addRoot(document); roots.forEach(patchNode); }
  var observer = new MutationObserver(function() { runPatch(); });

  function start() {
    runPatch();
    observer.observe(document.documentElement || document, {
      childList: true, subtree: true, attributes: true, characterData: true,
    });
    setInterval(runPatch, 700);
  }

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true });
  else start();
})();
