"""Init backend plugin"""

import copy
import logging
from kheops.plugin.common import PluginBackendClass

log = logging.getLogger(__name__)


class Plugin(PluginBackendClass):
    """Init backend plugin"""

    _plugin_name = "init"
    _schema_props_new = None

    default_engine = "jerakia"

    def process(self, backends: list, ctx: dict) -> (list, dict):
        """Return the new backend list"""

        new_backends = []
        for index, item in enumerate(backends):
            default = {
                "value": item,
            }

            if not isinstance(item, dict):
                item = default

            item["engine"] = item.get("engine", self.default_engine)
            item["_run"] = copy.deepcopy(ctx)
            item["_run"]["backend"] = {
                "index": index,
            }
            new_backends.append(item)

        return new_backends, ctx
