# SPDX-FileCopyrightText: (c) TagStudio Contributors
# SPDX-License-Identifier: GPL-3.0-only


import typing

from PySide6.QtCore import QObject, Signal

from tagstudio.core.library.alchemy.library import Library
from tagstudio.core.library.alchemy.registries.dupe_files_registry import DupeFilesRegistry
from tagstudio.qt.mixed.progress_bar import ProgressWidget
from tagstudio.qt.translations import Translations

# Only import for type checking/autocompletion, will not be imported at runtime.
if typing.TYPE_CHECKING:
    from tagstudio.qt.ts_qt import QtDriver


class MergeDuplicateEntries(QObject):
    done = Signal()

    def __init__(self, library: "Library", driver: "QtDriver"):
        super().__init__()
        self.lib = library
        self.driver = driver
        self.tracker = DupeFilesRegistry(library=self.lib)

    def merge_entries(self):
        pw = ProgressWidget(
            cancel_button_text=None,
            minimum=0,
            maximum=self.tracker.groups_count,
        )
        pw.setWindowTitle(Translations["entries.duplicate.merge.label"])
        pw.update_label(Translations["entries.duplicate.merge.label"])

        pw.from_iterable_function(self.tracker.merge_dupe_entries, None, self.done.emit)
