import { onMounted, ref, watch } from "vue";
const STORAGE_PREFIX = "shwfed.tabs.";
export function useActiveTabId(blockId, tabIds) {
  const active = ref(tabIds.value[0] ?? "");
  const key = () => `${STORAGE_PREFIX}${blockId}`;
  onMounted(() => {
    if (typeof window === "undefined") return;
    try {
      const stored = window.sessionStorage.getItem(key());
      if (stored && tabIds.value.includes(stored)) {
        active.value = stored;
      }
    } catch {
    }
  });
  watch(tabIds, (next) => {
    if (next.includes(active.value)) return;
    active.value = next[0] ?? "";
    if (typeof window === "undefined") return;
    try {
      window.sessionStorage.removeItem(key());
    } catch {
    }
  });
  watch(active, (next) => {
    if (typeof window === "undefined") return;
    if (!next) return;
    try {
      window.sessionStorage.setItem(key(), next);
    } catch {
    }
  });
  return active;
}
