/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import type { MangaStatus } from '@/lib/graphql/generated/graphql-base.types.ts';
import type { GridLayout } from '@/base/Base.types.ts';
import type { MangaDescriptionInfo, MangaIdInfo, MangaTitleInfo } from '@/features/manga/Manga.types.ts';
import type { TrackerIdInfo } from '@/features/tracker/Tracker.types.ts';

export type MetadataLibrarySettings = {
    showAddToLibraryCategorySelectDialog: boolean;
    ignoreFilters: boolean;
    removeMangaFromCategories: boolean;
    showTabSize: boolean;
    showContinueReadingButton: boolean;
    showDownloadBadge: boolean;
    showUnreadBadge: boolean;
    gridLayout: GridLayout;
};
export type LibrarySortMode =
    | 'unreadChapters'
    | 'totalChapters'
    | 'alphabetically'
    | 'dateAdded'
    | 'lastRead'
    | 'latestFetchedChapter'
    | 'latestUploadedChapter'
    | 'random';

export interface LibraryOptions {
    // sort options
    sortBy: NullAndUndefined<LibrarySortMode>;
    sortDesc: NullAndUndefined<boolean>;

    // filter options
    hasDownloadedChapters: NullAndUndefined<boolean>;
    hasBookmarkedChapters: NullAndUndefined<boolean>;
    hasUnreadChapters: NullAndUndefined<boolean>;
    hasReadChapters: NullAndUndefined<boolean>;
    hasDuplicateChapters: NullAndUndefined<boolean>;
    hasTrackerBinding: Record<TrackerIdInfo['id'], NullAndUndefined<boolean>>;
    hasStatus: Record<MangaStatus, NullAndUndefined<boolean>>;
    hasSource: Record<string, NullAndUndefined<boolean>>;
}

export type TMangaDuplicate = MangaIdInfo & MangaTitleInfo & MangaDescriptionInfo;

export type TMangaDuplicates<Manga> = Record<string, Manga[]>;

export type TMangaDuplicateResult<Manga> = { byTitle: Manga[]; byAlternativeTitle: Manga[] };

export type LibraryDuplicatesWorkerInput<Manga extends TMangaDuplicate = TMangaDuplicate> = {
    mangas: Manga[];
    checkAlternativeTitles: boolean;
};

export type LibraryDuplicatesDescriptionWorkerInput<Manga extends TMangaDuplicate = TMangaDuplicate> = {
    mangasToCheck: Manga[];
    mangas: Manga[];
};

export type LibraryOptionsContextType = {
    options: LibraryOptions;
    setOptions: React.Dispatch<React.SetStateAction<LibraryOptions>>;
};
