// ARGUS Beta — terminal.shell.js (Shell v2: chrome polish)
//
// Pure DOM layer over the bundle's tab strip + launcher. The bundle owns session
// state (renderTabs writes .tab buttons into #tabs, #newtab opens the launcher);
// this file ADDS, without touching the minified bundle:
//   1) overflow affordance — a fade edge + a "⋯ all sessions" button that opens a
//      sheet listing EVERY open session, so no tab is unreachable when they overflow
//      (the bug: tabs spilled off-screen and couldn't be tapped). Items proxy a click
//      to the real .tab / .tab-x so the bundle's switchTo/closeTab run unchanged.
//   2) brand — injects the Device Connect "DC" terminal logomark into the launcher
//      card (honors dc_final.html: window dots · '>' prompt · big mono DC · cursor ·
//      device chain).
(function () {
  "use strict";

  // ---------------- DC logomark (matches dc_final.html lockup) ----------------
  var DC_SVG =
    '<svg viewBox="0 0 120 116" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">' +
      '<g fill="#5f6a64"><circle cx="16" cy="15" r="2.6"/><circle cx="25" cy="15" r="2.6"/><circle cx="34" cy="15" r="2.6"/></g>' +
      '<text x="13" y="68" font-family="ui-monospace,Cascadia Code,Consolas,monospace" font-size="15" font-weight="700" fill="#5f6a64">&gt;</text>' +
      '<text x="62" y="72" text-anchor="middle" font-family="ui-monospace,Cascadia Code,Consolas,monospace" font-size="52" font-weight="800" fill="#f4f6f6" letter-spacing="1">DC</text>' +
      '<rect x="99" y="48" width="5" height="15" fill="#0a84ff"/>' +
      '<g stroke="#f4f6f6" stroke-width="1.4" stroke-linecap="round"><line x1="49" y1="98" x2="54" y2="98"/><line x1="66" y1="98" x2="72" y2="98"/></g>' +
      '<g fill="#f4f6f6">' +
        '<rect x="35" y="92" width="13" height="9" rx="1.2"/><rect x="40.5" y="101" width="2" height="2"/><rect x="38" y="103" width="7" height="1.6" rx="0.8"/>' +
        '<rect x="54.5" y="93" width="11" height="7.6" rx="1"/><path d="M52.5 100.6 h15 l-1.6 2.4 h-11.8 Z"/>' +
        '<rect x="75" y="91" width="6" height="12" rx="1.8"/><circle cx="78" cy="101" r="0.7" fill="#0b0b0c"/>' +
      '</g>' +
    '</svg>';

  // ---------------- all-sessions sheet ----------------
  var sheet = null, listEl = null;
  function buildSheet() {
    if (sheet) return;
    sheet = document.createElement("div");
    sheet.id = "dc-sheet";
    sheet.innerHTML =
      '<div class="sheet-card">' +
        '<div class="sheet-head"><span>Oturumlar</span>' +
          '<button class="sheet-close" aria-label="Kapat">×</button></div>' +
        '<div class="sheet-list"></div>' +
      '</div>';
    document.body.appendChild(sheet);
    listEl = sheet.querySelector(".sheet-list");
    sheet.querySelector(".sheet-close").addEventListener("click", closeSheet);
    sheet.addEventListener("click", function (e) { if (e.target === sheet) closeSheet(); });
  }
  function openSheet() {
    buildSheet();
    listEl.innerHTML = "";
    var tabs = document.querySelectorAll("#tabs .tab");
    if (!tabs.length) return;
    tabs.forEach(function (tab) {
      var label = (tab.querySelector(".tab-l") || {}).textContent || "oturum";
      var state = tab.getAttribute("data-state") || "";
      var isActive = tab.classList.contains("active");
      var item = document.createElement("button");
      item.className = "sheet-item" + (isActive ? " active" : "");
      var dot = document.createElement("span"); dot.className = "dot " + state;
      var lbl = document.createElement("span"); lbl.className = "lbl"; lbl.textContent = label;
      var sx = document.createElement("span"); sx.className = "sx"; sx.textContent = "×"; sx.title = "Kapat";
      item.appendChild(dot); item.appendChild(lbl); item.appendChild(sx);
      item.addEventListener("click", function (e) {
        if (e.target === sx) { e.stopPropagation(); var x = tab.querySelector(".tab-x"); if (x) x.click(); setTimeout(openSheet, 0); return; }
        tab.click(); closeSheet();
      });
      listEl.appendChild(item);
    });
    sheet.classList.add("show");
  }
  function closeSheet() { if (sheet) sheet.classList.remove("show"); }

  // ---------------- overflow detection (fade + ⋯ button) ----------------
  function refreshOverflow() {
    var tabs = document.getElementById("tabs");
    var wrap = document.getElementById("tabwrap");
    var more = document.getElementById("tabmore");
    if (!tabs || !wrap || !more) return;
    var overflowing = tabs.scrollWidth > tabs.clientWidth + 1;
    wrap.classList.toggle("scrollable", overflowing);
    more.classList.toggle("show", overflowing);
  }

  function wireTabs() {
    var tabs = document.getElementById("tabs");
    var more = document.getElementById("tabmore");
    if (!tabs || !more || tabs.__dcWired) return;
    tabs.__dcWired = true;
    more.addEventListener("click", openSheet);
    // re-check overflow whenever the strip's children or size change
    try { new MutationObserver(refreshOverflow).observe(tabs, { childList: true, subtree: true, characterData: true }); } catch (_) {}
    try { new ResizeObserver(refreshOverflow).observe(tabs); } catch (_) {}
    tabs.addEventListener("scroll", function () {}, { passive: true });
    window.addEventListener("resize", refreshOverflow);
    window.addEventListener("orientationchange", refreshOverflow);
    if (window.visualViewport) window.visualViewport.addEventListener("resize", refreshOverflow);
    refreshOverflow();
  }

  // ---------------- launcher brand injection ----------------
  function decorateLauncher() {
    var card = document.querySelector("#launcher .lc-card");
    if (!card || card.querySelector(".dc-brand")) return;
    var brand = document.createElement("div");
    brand.className = "dc-brand";
    brand.innerHTML = DC_SVG;
    card.insertBefore(brand, card.firstChild);
  }

  // ---------------- bootstrap ----------------
  function tick() { wireTabs(); refreshOverflow(); decorateLauncher(); }
  function boot() {
    tick();
    // body childList only (NOT subtree) — #launcher/#dc-sheet are direct body children;
    // the tab strip has its own scoped observers. Avoids firing on terminal DOM churn.
    try { new MutationObserver(tick).observe(document.body, { childList: true }); } catch (_) {}
  }
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
  else boot();
})();
