# Path: dhis2_core/src/dhis2/facility_list/mcsd_resources.py
# def build_bundle(org_units: List[OrgUnit], base_url: str) -> Bundle:
#     bundle = Bundle()
#     bundle.type = "transaction"
#     bundle.entry = []
# 
#     log.info(f"Building FHIR bundle from '{len(org_units)}' organisation units")
# 
#     for org_unit in org_units:
#         bundle.entry.append(build_location_bundle_entry(org_unit, base_url))
#         bundle.entry.append(build_organization_bundle_entry(org_unit, base_url))
# 
#     return bundle
# 
# Path: dhis2_core/src/dhis2/facility_list/models/mcsd.py
# class MCSDConfig(BaseModel):
#     id: str = Field(default_factory=lambda: str(uuid4()))
#     source: MCSDSource
#     target: MCSDTarget
# 
# class OrgUnit(BaseEntity):
#     geometry: Optional[OrgUnitGeometry]
#     parent: Optional[BaseEntity]


import json
import logging
import sys
from typing import Any, Callable, Dict, List
from dhis2.core.http import BaseHttpRequest
from dhis2.core.inventory import HostResolved, Inventory, resolve_one
from fhir.resources.bundle import Bundle
from .mcsd_resources import build_bundle
from .models.mcsd import MCSDConfig, OrgUnit

        log.info("Creating 'null://' target")

        def target_null(data: Any):
            log.info("Doing nothing with result")

        return target_null

    host = resolve_one(id, inventory)

    if "dhis2" in host.type:
        log.error("'dhis2' target type is not currently supported")
        sys.exit(-1)

    log.info(f"Creating target from '{host.key}' with base url '{host.baseUrl}'")

    def target_push(data: Any):
        payload: Bundle = data[1]
        return BaseHttpRequest(host).post("", data=payload.as_json())

    return target_push


def transform(config: MCSDConfig, data: Any):
    host: HostResolved = data[0]
    payload: Dict[str, Any] = data[1]

    org_units: List[OrgUnit] = []

    for org_unit in payload.get("organisationUnits", []):

# Path: dhis2_core/src/dhis2/facility_list/mcsd_resources.py
# def build_bundle(org_units: List[OrgUnit], base_url: str) -> Bundle:
#     bundle = Bundle()
#     bundle.type = "transaction"
#     bundle.entry = []
# 
#     log.info(f"Building FHIR bundle from '{len(org_units)}' organisation units")
# 
#     for org_unit in org_units:
#         bundle.entry.append(build_location_bundle_entry(org_unit, base_url))
#         bundle.entry.append(build_organization_bundle_entry(org_unit, base_url))
# 
#     return bundle
# 
# Path: dhis2_core/src/dhis2/facility_list/models/mcsd.py
# class MCSDConfig(BaseModel):
#     id: str = Field(default_factory=lambda: str(uuid4()))
#     source: MCSDSource
#     target: MCSDTarget
# 
# class OrgUnit(BaseEntity):
#     geometry: Optional[OrgUnitGeometry]
#     parent: Optional[BaseEntity]


# Path: dhis2_core/src/dhis2/facility_list/mcsd.py
import json
import logging
import sys
from typing import Any, Callable, Dict, List
from dhis2.core.http import BaseHttpRequest
from dhis2.core.inventory import HostResolved, Inventory, resolve_one
from fhir.resources.bundle import Bundle
from .mcsd_resources import build_bundle
from .models.mcsd import MCSDConfig, OrgUnit


        log.info("Creating 'null://' target")

        def target_null(data: Any):
            log.info("Doing nothing with result")

        return target_null

    host = resolve_one(id, inventory)

    if "dhis2" in host.type:
        log.error("'dhis2' target type is not currently supported")
        sys.exit(-1)

    log.info(f"Creating target from '{host.key}' with base url '{host.baseUrl}'")

    def target_push(data: Any):
        payload: Bundle = data[1]
        return BaseHttpRequest(host).post("", data=payload.as_json())

    return target_push


def transform(config: MCSDConfig, data: Any):
    host: HostResolved = data[0]
    payload: Dict[str, Any] = data[1]

    org_units: List[OrgUnit] = []

    for org_unit in payload.get("organisationUnits", []):

        org_units.append(OrgUnit(**org_unit))