# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
# MIT License (see LICENSE)

from __future__ import annotations

import copy
from typing import TYPE_CHECKING, Any

from ansible_collections.ssnailed.ez_compose.plugins.module_utils import (
    label,
    service,
)

if TYPE_CHECKING:
    from ansible_collections.ssnailed.ez_compose.plugins.module_utils.common import (
        State,
    )

# NOTE: Label helper arguments are added in the compose module itself
EXTRA_ARGS = {
    "internal_network": {"type": "bool", "default": False},
    "definition": {"type": "dict", "required": True},
}


def helper(state: State, params: dict[str, Any]) -> State:
    definition: dict[str, Any] = params["definition"]
    internal_network: bool = params["internal_network"]

    update = copy.deepcopy(definition)

    networks = update.get("networks", {})

    if internal_network:
        networks["internal"] = None

    update["networks"] = networks

    for name, args in params["label_helpers"].items():
        state = getattr(label, name).helper(state, params["name"], args)

    return service.common.update(params["name"], state, update)