"use client";
import { useCallback, useSyncExternalStore } from "react";
const STORAGE_KEY_PREFIX = "stigmer:recent-workspaces:";
const MAX_RECENT = 8;
function storageKey(runnerId) {
    return `${STORAGE_KEY_PREFIX}${runnerId}`;
}
function readEntries(runnerId) {
    try {
        const raw = localStorage.getItem(storageKey(runnerId));
        if (!raw)
            return [];
        return JSON.parse(raw);
    }
    catch {
        return [];
    }
}
function writeEntries(runnerId, entries) {
    try {
        localStorage.setItem(storageKey(runnerId), JSON.stringify(entries));
    }
    catch {
        // localStorage full or unavailable — silently degrade.
    }
}
function sortEntries(entries) {
    return [...entries].sort((a, b) => {
        if (a.pinned !== b.pinned)
            return a.pinned ? -1 : 1;
        return b.lastUsed - a.lastUsed;
    });
}
const listeners = new Set();
let snapshotVersion = 0;
function notifyListeners() {
    snapshotVersion++;
    for (const fn of listeners)
        fn();
}
function subscribe(callback) {
    listeners.add(callback);
    return () => listeners.delete(callback);
}
const EMPTY = [];
let cachedRunnerId = null;
let cachedVersion = -1;
let cachedResult = EMPTY;
function getSnapshot(runnerId) {
    if (!runnerId)
        return EMPTY;
    if (runnerId === cachedRunnerId && snapshotVersion === cachedVersion) {
        return cachedResult;
    }
    cachedRunnerId = runnerId;
    cachedVersion = snapshotVersion;
    cachedResult = sortEntries(readEntries(runnerId));
    return cachedResult;
}
/**
 * Manages recently used workspace paths for a specific runner,
 * persisted in `localStorage`.
 *
 * Entries are keyed by `runner_id` so each runner maintains its own
 * history. Pinned paths appear first, then by most-recently-used.
 *
 * @param runnerId - Runner to scope the history to. When `null`, returns empty.
 */
export function useRecentWorkspaces(runnerId) {
    const entries = useSyncExternalStore(subscribe, () => getSnapshot(runnerId), () => EMPTY);
    const recordSelection = useCallback((path) => {
        if (!runnerId)
            return;
        const existing = readEntries(runnerId);
        const idx = existing.findIndex((e) => e.path === path);
        const now = Date.now();
        let updated;
        if (idx >= 0) {
            updated = [...existing];
            updated[idx] = { ...updated[idx], lastUsed: now };
        }
        else {
            updated = [{ path, pinned: false, lastUsed: now }, ...existing];
        }
        if (updated.length > MAX_RECENT) {
            const unpinned = updated.filter((e) => !e.pinned);
            if (unpinned.length > 0) {
                unpinned.sort((a, b) => a.lastUsed - b.lastUsed);
                const oldest = unpinned[0];
                updated = updated.filter((e) => e !== oldest);
            }
        }
        writeEntries(runnerId, updated);
        notifyListeners();
    }, [runnerId]);
    const togglePin = useCallback((path) => {
        if (!runnerId)
            return;
        const existing = readEntries(runnerId);
        const idx = existing.findIndex((e) => e.path === path);
        if (idx < 0)
            return;
        const updated = [...existing];
        updated[idx] = { ...updated[idx], pinned: !updated[idx].pinned };
        writeEntries(runnerId, updated);
        notifyListeners();
    }, [runnerId]);
    const remove = useCallback((path) => {
        if (!runnerId)
            return;
        const updated = readEntries(runnerId).filter((e) => e.path !== path);
        writeEntries(runnerId, updated);
        notifyListeners();
    }, [runnerId]);
    return { entries, recordSelection, togglePin, remove };
}
//# sourceMappingURL=useRecentWorkspaces.js.map