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

hub.exec.boto3.client.cloudfront.create_function
hub.exec.boto3.client.cloudfront.delete_function
hub.exec.boto3.client.cloudfront.describe_function
hub.exec.boto3.client.cloudfront.get_function
hub.exec.boto3.client.cloudfront.list_functions
hub.exec.boto3.client.cloudfront.publish_function
hub.exec.boto3.client.cloudfront.test_function
hub.exec.boto3.client.cloudfront.update_function
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, function_config: Dict, function_code: ByteString)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a CloudFront function. To create a function, you provide the function code and some configuration
    information about the function. The response contains an Amazon Resource Name (ARN) that uniquely identifies the
    function. When you create a function, it’s in the DEVELOPMENT stage. In this stage, you can test the function
    with TestFunction, and update it with UpdateFunction. When you’re ready to use your function with a CloudFront
    distribution, use PublishFunction to copy the function from the DEVELOPMENT stage to LIVE. When it’s live, you
    can attach the function to a distribution’s cache behavior, using the function’s ARN.

    Args:
        name(Text): A name to identify the function.
        function_config(Dict): Configuration information about the function, including an optional comment and the function’s
            runtime.
        function_code(ByteString): The function code. For more information about writing a CloudFront function, see Writing
            function code for CloudFront Functions in the Amazon CloudFront Developer Guide.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.cloudfront.function.present:
                - name: value
                - function_config: value
                - function_code: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.cloudfront.function.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.cloudfront.describe_function(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.cloudfront.create_function(
                ctx,
                
                
                **{"Name": name, "FunctionConfig": function_config, "FunctionCode": function_code}
            )
            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.cloudfront.describe_function(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, if_match: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a CloudFront function. You cannot delete a function if it’s associated with a cache behavior. First,
    update your distributions to remove the function association from all cache behaviors, then delete the function.
    To delete a function, you must provide the function’s name and version (ETag value). To get these values, you
    can use ListFunctions and DescribeFunction.

    Args:
        name(Text): The name of the function that you are deleting.
        if_match(Text): The current version (ETag value) of the function that you are deleting, which you can get using
            DescribeFunction.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.cloudfront.function.absent:
                - name: value
                - if_match: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.cloudfront.function.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.cloudfront.describe_function(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.cloudfront.delete_function(
                ctx,
                
                
                **{"Name": name, "IfMatch": if_match}
            )
            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.cloudfront.describe_function(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

