# ********************************************************************************
# *         WARNING: This file is automatically generated by unasync.py.         *
# *                             DO NOT MANUALLY EDIT                             *
# *      Source File: patent_client/_async/uspto/global_dossier/manager.py       *
# ********************************************************************************

from patent_client.util.manager import Manager

from .api import GlobalDossierApi
from .query import QueryBuilder

query_builder = QueryBuilder()

global_dossier_api = GlobalDossierApi()


class GlobalDossierBaseManager(Manager):
    def filter(self, *args, **kwargs):
        raise NotImplementedError("GlobalDossier can only retrieve using the GET interface")

    def order_by(self, *args, **kwargs):
        raise NotImplementedError("GlobalDossier can only retrieve using the GET interface")

    def limit(self, *args, **kwargs):
        raise NotImplementedError("GlobalDossier can only retrieve using the GET interface")

    def offset(self, *args, **kwargs):
        raise NotImplementedError("GlobalDossier can only retrieve using the GET interface")


class GlobalDossierManager(GlobalDossierBaseManager):
    def get(self, *args, **kwargs):
        return global_dossier_api.get_file(**query_builder.build_query(*args, **kwargs))


class GlobalDossierApplicationManager(GlobalDossierBaseManager):
    def get(self, *args, **kwargs):
        query = query_builder.build_query(*args, **kwargs)
        gd_file = global_dossier_api.get_file(**query)
        if query["type_code"] == "application":
            return next(a for a in gd_file.applications if a.app_num in query["doc_number"])
        elif query["type_code"] == "publication":
            return next(
                a
                for a in gd_file.applications
                if any(p.pub_num in query["doc_number"] for p in a.pub_list)
            )


class DocumentListManager(GlobalDossierBaseManager):
    def get(self, country, doc_number, kind_code):
        return global_dossier_api.get_doc_list(country, doc_number, kind_code)
