import type { ReaderMetadata } from "../../types/reader";

type ReaderType = "ebook" | "pdf" | "comic" | "manga";

interface UniversalReader {
  type: "ebook";
  cif: any;
  currentSpineIndex: number;
}

interface PDFReader {
  type: "pdf";
  doc: any;
  currentPage: number;
}

interface ComicReader {
  type: "comic";
  images: Blob[];
  currentPage: number;
}

interface MangaReader {
  type: "manga";
  images: Blob[];
  currentPage: number;
  readingDirection: "rtl" | "vertical";
}

type CurrentReader = UniversalReader | PDFReader | ComicReader | MangaReader;

export interface ReaderContext {
  getState: () => {
    currentReader: CurrentReader | null;
    readerMetadata: ReaderMetadata | null;
  };
  setState: (updates: Partial<ReaderState>) => void;
  navigation: {
    nextPage: () => void;
    previousPage: () => void;
    goToPage: (page: number) => void;
    goToChapter: (chapterIndex: number) => void;
  };
  render: () => void;
  elements: {
    readerContent: HTMLElement;
    chrome: HTMLElement | null;
    progressDisplay: HTMLElement | null;
  };
  events: {
    on: (event: string, handler: Function) => void;
    emit: (event: string, data?: any) => void;
  };
}

export interface ReaderState {
  currentReader: CurrentReader | null;
  readerMetadata: ReaderMetadata | null;
}

export function createReaderContext(
  getState: () => ReaderState,
  setState: (updates: Partial<ReaderState>) => void,
  navigation: ReaderContext["navigation"],
  render: () => void,
): ReaderContext {
  return {
    getState,
    setState,
    navigation,
    render,
    elements: {
      readerContent: document.getElementById("reader-content")!,
      chrome: document.getElementById("reader-chrome"),
      progressDisplay: document.getElementById("progress-display"),
    },
    events: {
      on: (event: string, handler: Function) => readerEvents.on(event, handler),
      emit: (event: string, data?: any) => readerEvents.emit(event, data),
    },
  };
}

import { readerEvents } from "./reader-events";
