import { useEffect, useMemo } from "react";
import { useUnmount } from "react-use";
import { NostrEvent } from "nostr-tools";

import { NostrRequestFilter } from "../types/nostr-relay";
import timelineCacheService from "../services/timeline-cache";
import { EventFilter } from "../classes/timeline-loader";
import { createSimpleQueryMap } from "../helpers/nostr/filter";

type Options = {
  /** @deprecated */
  enabled?: boolean;
  eventFilter?: EventFilter;
  cursor?: number;
  customSort?: (a: NostrEvent, b: NostrEvent) => number;
};

export default function useTimelineLoader(
  key: string,
  relays: Iterable<string>,
  query: NostrRequestFilter | undefined,
  opts?: Options,
) {
  const timeline = useMemo(() => timelineCacheService.createTimeline(key), [key]);

  useEffect(() => {
    if (query) {
      timeline.setQueryMap(createSimpleQueryMap(relays, query));
      timeline.open();
    } else timeline.close();
  }, [timeline, JSON.stringify(query), Array.from(relays).join("|")]);

  useEffect(() => {
    timeline.setEventFilter(opts?.eventFilter);
  }, [timeline, opts?.eventFilter]);
  useEffect(() => {
    if (opts?.cursor !== undefined) {
      timeline.setCursor(opts.cursor);
    }
  }, [timeline, opts?.cursor]);
  useEffect(() => {
    timeline.events.customSort = opts?.customSort;
  }, [timeline, opts?.customSort]);

  useUnmount(() => {
    timeline.close();
  });

  return timeline;
}
