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

hub.exec.boto3.client.codecommit.delete_file
hub.exec.boto3.client.codecommit.get_file
hub.exec.boto3.client.codecommit.put_file
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, repository_name: Text, branch_name: Text, file_content: ByteString, file_path: Text, name: Text = None, file_mode: Text = None, parent_commit_id: Text = None, commit_message: Text = None, email: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Adds or updates a file in a branch in an AWS CodeCommit repository, and generates a commit for the addition in
    the specified branch.

    Args:
        name(Text, optional): The name of the person adding or updating the file. Although it is optional, a name makes the
            commit history for your repository more useful. Defaults to None.
        repository_name(Text): The name of the repository where you want to add or update the file.
        branch_name(Text): The name of the branch where you want to add or update the file. If this is an empty repository,
            this branch is created.
        file_content(ByteString): The content of the file, in binary object format.
        file_path(Text): The name of the file you want to add or update, including the relative path to the file in the
            repository.  If the path does not currently exist in the repository, the path is created as part
            of adding the file.
        file_mode(Text, optional): The file mode permissions of the blob. Valid file mode permissions are listed here. Defaults to None.
        parent_commit_id(Text, optional): The full commit ID of the head commit in the branch where you want to add or update the file. If
            this is an empty repository, no commit ID is required. If this is not an empty repository, a
            commit ID is required.  The commit ID must match the ID of the head commit at the time of the
            operation. Otherwise, an error occurs, and the file is not added or updated. Defaults to None.
        commit_message(Text, optional): A message about why this file was added or updated. Although it is optional, a message makes the
            commit history for your repository more useful. Defaults to None.
        email(Text, optional): An email address for the person adding or updating the file. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.codecommit.file.present:
                - repository_name: value
                - branch_name: value
                - file_content: value
                - file_path: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codecommit.file.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.codecommit.get_file(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.codecommit.put_file(
                ctx,
                
                
                **{"Name": name, "repositoryName": repository_name, "branchName": branch_name, "fileContent": file_content, "filePath": file_path, "fileMode": file_mode, "parentCommitId": parent_commit_id, "commitMessage": commit_message, "email": email}
            )
            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.codecommit.get_file(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, repository_name: Text, branch_name: Text, file_path: Text, parent_commit_id: Text, name: Text = None, keep_empty_folders: bool = None, commit_message: Text = None, email: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a specified file from a specified branch. A commit is created on the branch that contains the revision.
    The file still exists in the commits earlier to the commit that contains the deletion.

    Args:
        name(Text, optional): The name of the author of the commit that deletes the file. If no name is specified, the user's
            ARN is used as the author name and committer name. Defaults to None.
        repository_name(Text): The name of the repository that contains the file to delete.
        branch_name(Text): The name of the branch where the commit that deletes the file is made.
        file_path(Text): The fully qualified path to the file that to be deleted, including the full name and extension
            of that file. For example, /examples/file.md is a fully qualified path to a file named file.md
            in a folder named examples.
        parent_commit_id(Text): The ID of the commit that is the tip of the branch where you want to create the commit that
            deletes the file. This must be the HEAD commit for the branch. The commit that deletes the file
            is created from this commit ID.
        keep_empty_folders(bool, optional): If a file is the only object in the folder or directory, specifies whether to delete the folder
            or directory that contains the file. By default, empty folders are deleted. This includes empty
            folders that are part of the directory structure. For example, if the path to a file is
            dir1/dir2/dir3/dir4, and dir2 and dir3 are empty, deleting the last file in dir4 also deletes
            the empty folders dir4, dir3, and dir2. Defaults to None.
        commit_message(Text, optional): The commit message you want to include as part of deleting the file. Commit messages are limited
            to 256 KB. If no message is specified, a default message is used. Defaults to None.
        email(Text, optional): The email address for the commit that deletes the file. If no email address is specified, the
            email address is left blank. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.codecommit.file.absent:
                - repository_name: value
                - branch_name: value
                - file_path: value
                - parent_commit_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codecommit.file.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.codecommit.get_file(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.codecommit.delete_file(
                ctx,
                
                
                **{"Name": name, "repositoryName": repository_name, "branchName": branch_name, "filePath": file_path, "parentCommitId": parent_commit_id, "keepEmptyFolders": keep_empty_folders, "commitMessage": commit_message, "email": email}
            )
            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.codecommit.get_file(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

