import { defineStore } from 'pinia';
import { client } from 'src/boot/appwrite';
import { Interval } from './schedule.types';
import { ref } from 'vue';
import { RealtimeResponseEvent } from 'appwrite';

export const useRealtimeStore = defineStore('realtime', () => {
  const subscriptions = ref<Map<string, () => void>>(new Map());

  const register = (
    channel: string,
    fn: (response: RealtimeResponseEvent<Interval>) => void
  ) => {
    if (subscriptions.value.has(channel)) return; // Already subscribed.  But maybe different callback fn?
    subscriptions.value.set(channel, client.subscribe(channel, fn));
  };

  return {
    register,
  };
});
