# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/routes/card.ipynb.

# %% auto 0
__all__ = ['get_kpi_definition', 'get_card_metadata', 'generate_body_search_cards_admin_summary', 'search_cards_admin_summary']

# %% ../../nbs/routes/card.ipynb 2
from typing import Optional

import io
import pandas as pd

import httpx

import domolibrary.client.get_data as gd
import domolibrary.client.ResponseGetData as rgd
import domolibrary.client.DomoAuth as dmda


# %% ../../nbs/routes/card.ipynb 3
async def get_kpi_definition(auth: dmda.DomoAuth, card_id: str, debug_api: bool = False) -> rgd.ResponseGetData:
    
    url = f"https://{auth.domo_instance}.domo.com/api/content/v3/cards/kpi/definition"

    body = {"urn": card_id}

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='PUT',
        body=body,
        debug_api=False
    )

    return res

# %% ../../nbs/routes/card.ipynb 6
async def get_card_metadata(auth: dmda.DomoAuth, card_id: str, debug_api: bool = False) -> rgd.ResponseGetData:
    optional_params = "metadata,certification,datasources,owners,problems"
    url = f"https://{auth.domo_instance}.domo.com/api/content/v1/cards?urns={card_id}&parts={optional_params}"

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='GET',
        debug_api=debug_api
    )

    return res

# %% ../../nbs/routes/card.ipynb 9
def generate_body_search_cards_admin_summary(page_ids: [str] = None,
                                             searchPages: bool = True,
                                             cardSearchText: str = '',
                                             pageSearchText: str = '') -> dict:
    body = {
        "ascending": True,
        "orderBy": "cardTitle"
    }

    

    if cardSearchText:
        body.update({'cardTitleSearchText': cardSearchText, "includeCardTitleClause": True })

    if pageSearchText:
        body.update({'pageTitleSearchText': pageSearchText, "includePageTitleClause": True, "notOnPage": False})

    if page_ids:
        body.update({'pageIds': page_ids})

    return body

# %% ../../nbs/routes/card.ipynb 10
async def search_cards_admin_summary(auth: dmda.DomoAuth,
                                     body: dict,

                                     maximum :int = None,
                                     
                                     debug_api: bool = False,
                                     debug_loop: bool = False,
                                     session : httpx.AsyncClient  = None,
                                     wait_sleep: int = 3

                                     ) -> rgd.ResponseGetData:

    limit = 100
    offset = 0
    loop_until_end = False if maximum else True

    url = f'https://{auth.domo_instance}.domo.com/api/content/v2/cards/adminsummary?skip={offset}&limit={limit}'
    
    offset_params = {
        "offset": "skip",
        "limit": "limit",
    }

    def arr_fn(res):
        return res.response.get('cardAdminSummaries')

    res = await gd.looper(
        auth=auth,
        method="POST",
        url=url,
        arr_fn=arr_fn,
        offset_params=offset_params,
        limit=limit,
        skip=offset,
        body = body,
        maximum = maximum,
        session=session,
        debug_api=debug_api,
        debug_loop=debug_loop,
        loop_until_end=loop_until_end,
        wait_sleep=wait_sleep
    )
    return res

