import sys

def apply_all():
    with open("examples/edge_explorer.html", "r") as f:
        content = f.read()

    # 1. Fix missing scope bracket
    content = content.replace(
"""            } // end plugins loop
            html += `</div>`; // Close plugins container
          }
        } // end edges loop

        html += `</div></div></div>`;

        document.getElementById('hierarchyContent').innerHTML = html;
      }""",
"""            } // end plugins loop
            html += `</div>`; // Close plugins container
          }
          html += `</details>`;
        } // end edges loop
      } // end else

      html += `</div></div></div>`;

      document.getElementById('hierarchyContent').innerHTML = html;
    }""")

    # 1.5 Fix the stray `}` at the bottom that was moved:
    content = content.replace(
"""      // Auto-fill form and optionally connect if URL is current host.
      if (window.location.protocol.startsWith('http')) {
        document.getElementById('bridgeUrl').value = window.location.origin;
        // You could optionally auto-connect here: doConnect();
      }
    }
  </script>
</body>""",
"""      // Auto-fill form and optionally connect if URL is current host.
      if (window.location.protocol.startsWith('http')) {
        document.getElementById('bridgeUrl').value = window.location.origin;
        // You could optionally auto-connect here: doConnect();
      }
  </script>
</body>""")


    # 2. Sidebar collapsible
    content = content.replace(
"""      for (const [ename, edata] of Object.entries(edges)) {
        const plugins = edata.plugins || {};
        html += `<ul class="tree" style="margin-top:12px">
      <li class="section-label">Edge: ${ename}</li>
      <li class="tree-item edge" onclick="showPage('hierarchy')">
        <span class="icon">💻</span> ${ename}
      </li>`;""",
"""      for (const [ename, edata] of Object.entries(edges)) {
        const plugins = edata.plugins || {};
        html += `<details>
      <summary class="tree-item edge" style="user-select:none; outline:none; font-weight:bold; cursor:pointer;" onclick="if(event.offsetX > 20) { showPage('hierarchy'); event.preventDefault(); }">
        <span class="icon">💻</span> ${ename}
      </summary>
      <ul class="tree" style="margin-top: 2px; padding-left: 16px; border-left: 1px solid var(--border); margin-left: 24px;">`;""")

    content = content.replace(
"""        }
        html += '</ul>';
      }
      el.innerHTML = html;""",
"""        }
        html += '</ul></details>';
      }
      el.innerHTML = html;""")

    # 3. Edges collapsible
    content = content.replace(
"""      } else {
        for (const ename of edgeNamesList) {
          html += `<div style="margin-top: 15px; display:flex; align-items:center;">
            <span style="font-size: 1.1em; margin-right:8px;">💻</span> <strong style="color:var(--accent2); font-size:1.05em;">${ename}</strong>
          </div>`;""",
"""      } else {
        for (const ename of edgeNamesList) {
          html += `<details style="margin-top: 15px;">
            <summary style="display:flex; align-items:center; cursor:pointer; user-select:none; outline:none; font-weight:bold; list-style:none;">
              <span style="font-size: 1.1em; margin-right:8px;">💻</span> <strong style="color:var(--accent2); font-size:1.05em;">${ename}</strong>
            </summary>`;""")

    # 4. the bug fix I meant to apply!
    content = content.replace(
        "if (window.lastEdges && window.lastBridge) renderHierarchy(window.lastEdges, window.lastBridge);",
        "if (window.lastEdges && window.lastBridge) { buildSidebar(window.lastEdges); renderHierarchy(window.lastEdges, window.lastBridge); }"
    )

    with open("examples/edge_explorer.html", "w") as f:
        f.write(content)

apply_all()
