"""
Autogenerated state module using `pop-create-idem <https://gitlab.com/saltstack/pop/pop-create-idem>`__

hub.exec.boto3.client.config.delete_stored_query
hub.exec.boto3.client.config.get_stored_query
hub.exec.boto3.client.config.list_stored_queries
hub.exec.boto3.client.config.put_stored_query
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, stored_query: Dict, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Saves a new query or updates an existing saved query. The QueryName must be unique for a single AWS account and
    a single AWS Region. You can create upto 300 queries in a single AWS account and a single AWS Region.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        stored_query(Dict): A list of StoredQuery objects. The mandatory fields are QueryName and Expression.  When you are
            creating a query, you must provide a query name and an expression. When you are updating a
            query, you must provide a query name but updating the description is optional.
        tags(List, optional): A list of Tags object. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.config.stored_query.present:
                - name: value
                - stored_query: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.config.stored_query.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    
    before = await hub.exec.boto3.client.config.get_stored_query(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.config.put_stored_query(
                ctx,
                
                
                **{"StoredQuery": stored_query, "Tags": tags}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            ret["comment"] = f"Created '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    
    # TODO perform other modifications as needed here
    ...

    after = await hub.exec.boto3.client.config.get_stored_query(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, query_name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the stored query for a single AWS account and a single AWS Region.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        query_name(Text): The name of the query that you want to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.config.stored_query.absent:
                - name: value
                - query_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.config.stored_query.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    

    before = await hub.exec.boto3.client.config.get_stored_query(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.config.delete_stored_query(
                ctx,
                
                
                **{"QueryName": query_name}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            result["comment"] = f"Deleted '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    

    after = await hub.exec.boto3.client.config.get_stored_query(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

