import { k as head, l as escape_html, n as derived, o as ensure_array_like } from './index-BHoOpAe1.js';
import { g as goto } from './client-Do_WijF3.js';
import { E as EmptyState, R as RefreshSvg, c as clearCache } from './cache-tQqq4VCX.js';
import { S as SettingsSvg } from './settings-Dv3Esboo.js';
import './markdown-W_mTBct0.js';
import { o as onDestroy } from './index-server-BVf8_1QW.js';
import { B as Button } from './Button-DPcpECFE.js';
import { I as Icon } from './Icon-C2-613_Z.js';
import { S as Shimmer } from './Shimmer-BrakrBeG.js';
import { S as SvelteMap, a as SvelteSet } from './index-server2-BJrT0wnA.js';
import './root-CW7Ox2wn.js';
import './index2-CKh4I94n.js';
import './state.svelte-Cg5VK5nH.js';
import 'dompurify';
import 'marked';

const ACTIVE_THRESHOLD_MS = 3e4;
let sessions = [];
const summarizers = new SvelteMap();
const sessionSockets = new SvelteMap();
const pendingSessionSockets = new SvelteSet();
function disconnect() {
  for (const [, sock] of sessionSockets) {
    sock.onclose = null;
    sock.close();
  }
  sessionSockets.clear();
  pendingSessionSockets.clear();
  summarizers.clear();
}
function getCards() {
  const now = Date.now();
  return sortSessions(sessions).map((s) => {
    const createdMs = Date.parse(s.createdAt);
    const lastEventTs = s.events.length > 0 ? Date.parse(s.events[s.events.length - 1].timestamp) : 0;
    return {
      ...s,
      duration: now - createdMs,
      goal: s.goal,
      // Ensure goal is mapped
      isActive: lastEventTs > 0 && now - lastEventTs < ACTIVE_THRESHOLD_MS,
      isSummarizing: s.isSummarizing,
      // Ensure isSummarizing is mapped
      summary: s.summary
      // Ensure summary is mapped
    };
  });
}
function sortSessions(list) {
  return [...list].sort((a, b) => {
    const order = { error: 2, exited: 3, idle: 1, running: 0 };
    const rankA = order[a.status];
    const rankB = order[b.status];
    if (rankA !== rankB) {
      return rankA - rankB;
    }
    if (a.status !== "exited" && b.status !== "exited") {
      return b.createdAt.localeCompare(a.createdAt);
    }
    const exitA = a.exitedAt ?? a.createdAt;
    const exitB = b.exitedAt ?? b.createdAt;
    return exitB.localeCompare(exitA);
  });
}
function _page($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    let projects = [];
    let loading = false;
    const cards = derived(getCards);
    onDestroy(() => {
      disconnect();
    });
    async function fetchSessions(append = false, bustCache = false) {
      {
        return;
      }
    }
    async function forceRefresh() {
      loading = true;
      clearCache("shooter_projects");
      await fetchSessions(false, true);
    }
    function navigateToConfig() {
      void goto();
    }
    function totalSessionCount() {
      return projects.reduce((sum, p) => sum + p.sessionCount, 0);
    }
    head("1uha8ag", $$renderer2, ($$renderer3) => {
      $$renderer3.title(($$renderer4) => {
        $$renderer4.push(`<title>Dashboard - Shooter</title>`);
      });
      $$renderer3.push(`<meta name="description" content="Active terminals and Claude Code sessions"/>`);
    });
    $$renderer2.push(`<main class="main"><div class="page-header"><div class="page-header-content"><div><h1 class="page-title">Dashboard</h1> <p class="page-description">Active terminals and Claude Code sessions</p></div> <div class="page-actions">`);
    Button($$renderer2, {
      classes: "btn-secondary",
      onclick: forceRefresh,
      disabled: loading,
      children: ($$renderer3) => {
        Icon($$renderer3, { svg: RefreshSvg, classes: "icon-14" });
        $$renderer3.push(`<!----> Refresh`);
      },
      $$slots: { default: true }
    });
    $$renderer2.push(`<!----></div></div> `);
    if (projects.length > 0) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<div class="stats-bar svelte-1uha8ag"><div class="stat-chip svelte-1uha8ag"><span class="stat-value svelte-1uha8ag">${escape_html(projects.length)}</span> <span class="stat-label svelte-1uha8ag">${escape_html(projects.length === 1 ? "project" : "projects")}</span></div> <div class="stat-chip svelte-1uha8ag"><span class="stat-value svelte-1uha8ag">${escape_html(totalSessionCount())}</span> <span class="stat-label svelte-1uha8ag">${escape_html(totalSessionCount() === 1 ? "session" : "sessions")}</span></div> `);
      if (cards().length > 0) {
        $$renderer2.push("<!--[0-->");
        $$renderer2.push(`<div class="stat-chip stat-chip-active svelte-1uha8ag"><span class="stat-value svelte-1uha8ag">${escape_html(cards().length)}</span> <span class="stat-label svelte-1uha8ag">active</span></div>`);
      } else {
        $$renderer2.push("<!--[-1-->");
      }
      $$renderer2.push(`<!--]--></div>`);
    } else {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--></div> `);
    {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--> `);
    if (loading && projects.length === 0 && cards().length === 0) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<div class="loading-container"><!--[-->`);
      const each_array = ensure_array_like(Array(3));
      for (let i = 0, $$length = each_array.length; i < $$length; i++) {
        each_array[i];
        Shimmer($$renderer2, { classes: "shimmer-card" });
      }
      $$renderer2.push(`<!--]--></div>`);
    } else {
      $$renderer2.push("<!--[1-->");
      {
        let icon = function($$renderer3) {
          Icon($$renderer3, { svg: SettingsSvg, classes: "icon-24" });
        };
        EmptyState($$renderer2, {
          title: "Configuration Required",
          description: "Set up your API credentials to start tracking sessions",
          icon,
          children: ($$renderer3) => {
            Button($$renderer3, {
              classes: "btn-primary",
              onclick: navigateToConfig,
              text: "Configure Settings"
            });
          }
        });
      }
    }
    $$renderer2.push(`<!--]--></main>`);
  });
}

export { _page as default };
//# sourceMappingURL=_page.svelte-COFpm-S2.js.map
