// [DEF:activity:Store]
// @TIER: STANDARD
// @PURPOSE: Track active task count for navbar indicator
// @LAYER: UI
// @RELATION: DEPENDS_ON -> WebSocket connection, taskDrawer store

import { derived } from 'svelte/store';
import { taskDrawerStore } from './taskDrawer.js';

/**
 * Derived store that counts active tasks
 * @UX_STATE: Idle -> No active tasks, badge hidden
 * @UX_STATE: Active -> Badge shows count of running tasks
 */
export const activityStore = derived(taskDrawerStore, ($drawer) => {
  const activeCount = Object.values($drawer.resourceTaskMap)
    .filter(t => t.status === 'RUNNING').length;
  
  console.log(`[activityStore][State] Active count: ${activeCount}`);
  
  return {
    activeCount,
    recentTasks: Object.entries($drawer.resourceTaskMap)
      .map(([resourceId, taskInfo]) => ({
        taskId: taskInfo.taskId,
        resourceId,
        status: taskInfo.status
      }))
      .slice(-5) // Last 5 tasks
  };
});

// [/DEF:activity:Store]
