from typing import Dict
import logging


def collect_content_packs_to_install(id_set: Dict, integration_ids: set, playbook_names: set, script_names: set) -> set:
    """Iterates all content entities in the ID set and extract the pack names for the modified ones.

    Args:
        id_set (Dict): Structure which holds all content entities to extract pack names from.
        integration_ids (set): Set of integration IDs to get pack names for.
        playbook_names (set): Set of playbook names to get pack names for.
        script_names (set): Set of script names to get pack names for.

    Returns:
        set. Pack names to install.
    """
    packs_to_install = set()

    id_set_integrations = id_set.get('integrations', [])
    for integration in id_set_integrations:
        integration_id = list(integration.keys())[0]
        integration_object = integration[integration_id]
        if integration_id in integration_ids:
            integration_pack = integration_object.get('pack')
            if integration_pack:
                logging.info(
                    f'Found integration {integration_id} in pack {integration_pack} - adding to packs to install')
                packs_to_install.add(integration_object.get('pack'))
            else:
                logging.warning(f'Found integration {integration_id} without pack - not adding to packs to install')

    id_set_playbooks = id_set.get('playbooks', [])
    for playbook in id_set_playbooks:
        playbook_object = list(playbook.values())[0]
        playbook_name = playbook_object.get('name')
        if playbook_name in playbook_names:
            playbook_pack = playbook_object.get('pack')
            if playbook_pack:
                logging.info(f'Found playbook {playbook_name} in pack {playbook_pack} - adding to packs to install')
                packs_to_install.add(playbook_pack)
            else:
                logging.warning(f'Found playbook {playbook_name} without pack - not adding to packs to install')

    id_set_script = id_set.get('scripts', [])
    for script in id_set_script:
        script_id = list(script.keys())[0]
        script_object = script[script_id]
        if script_id in script_names:
            script_pack = script_object.get('pack')
            if script_pack:
                logging.info(f'Found script {script_id} in pack {script_pack} - adding to packs to install')
                packs_to_install.add(script_object.get('pack'))
            else:
                logging.warning(f'Found script {script_id} without pack - not adding to packs to install')

    return packs_to_install