# Path: dhis2_core/src/dhis2/code_list/models/svcm.py
# class CodeList(BaseEntity):
#     type: Literal["optionSets", "categories"] = "optionSets"
#     version: Union[str, int] = "1"
#     codes: List[Code] = []
# 
# class SVCMConfig(BaseModel):
#     id: str = Field(default_factory=lambda: str(uuid4()))
#     source: SVCMSource
#     target: SVCMTarget
# 
# Path: dhis2_core/src/dhis2/code_list/svcm_resources.py
# def build_bundle(code_lists: List[CodeList], base_url: str) -> Bundle:
#     bundle = Bundle()
#     bundle.type = "transaction"
#     bundle.entry = []
# 
#     log.info(f"Building FHIR bundle from '{len(code_lists)}' DHIS2 code lists")
# 
#     for code_list in code_lists:
#         bundle.entry.append(build_value_set_bundle_entry(code_list, base_url))
#         bundle.entry.append(build_code_system_bundle_entry(code_list, base_url))
# 
#     return bundle


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 .models.svcm import CodeList, SVCMConfig
from .svcm_resources import build_bundle
        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: SVCMConfig, data: Any):
    host: HostResolved = data[0]
    payload: Dict[str, Any] = data[1]

    code_lists: List[CodeList] = []

    option_sets = payload.get("optionSets", [])
    categories = payload.get("categories", [])

    for option_set in option_sets:
        code_lists.append(CodeList(**option_set))

    for category in categories:

# Path: dhis2_core/src/dhis2/code_list/models/svcm.py
# class CodeList(BaseEntity):
#     type: Literal["optionSets", "categories"] = "optionSets"
#     version: Union[str, int] = "1"
#     codes: List[Code] = []
# 
# class SVCMConfig(BaseModel):
#     id: str = Field(default_factory=lambda: str(uuid4()))
#     source: SVCMSource
#     target: SVCMTarget
# 
# Path: dhis2_core/src/dhis2/code_list/svcm_resources.py
# def build_bundle(code_lists: List[CodeList], base_url: str) -> Bundle:
#     bundle = Bundle()
#     bundle.type = "transaction"
#     bundle.entry = []
# 
#     log.info(f"Building FHIR bundle from '{len(code_lists)}' DHIS2 code lists")
# 
#     for code_list in code_lists:
#         bundle.entry.append(build_value_set_bundle_entry(code_list, base_url))
#         bundle.entry.append(build_code_system_bundle_entry(code_list, base_url))
# 
#     return bundle


# Path: dhis2_core/src/dhis2/code_list/svcm.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 .models.svcm import CodeList, SVCMConfig
from .svcm_resources import build_bundle

        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: SVCMConfig, data: Any):
    host: HostResolved = data[0]
    payload: Dict[str, Any] = data[1]

    code_lists: List[CodeList] = []

    option_sets = payload.get("optionSets", [])
    categories = payload.get("categories", [])

    for option_set in option_sets:
        code_lists.append(CodeList(**option_set))

    for category in categories:

        code_lists.append(CodeList(**category, type="categories"))