"""Backend access helpers for exploit mitigation analyzer."""

from __future__ import annotations

from typing import Any


class ExploitMitigationAccessMixin:
    """Small access helpers kept outside the main analyzer facade."""

    adapter: Any  # provided by host class
    _cmdj: Any  # provided by host class

    def _can_use_direct_method(self, method_name: str) -> bool:
        return self.adapter is not None and hasattr(self.adapter, method_name)

    @staticmethod
    def _coerce_dict_list(value: Any) -> list[dict[str, Any]]:
        """Coerce a value to a list of dicts, returning [] for non-list inputs."""
        if isinstance(value, list):
            return [item for item in value if isinstance(item, dict)]
        if isinstance(value, dict):
            return [value]
        return []

    def _get_imports(self) -> list[dict[str, Any]]:
        if self._can_use_direct_method("get_imports"):
            return self._coerce_dict_list(self.adapter.get_imports())
        return self._coerce_dict_list(self._cmdj("iij", []))

    def _get_strings(self) -> list[dict[str, Any]]:
        if self._can_use_direct_method("get_strings"):
            return self._coerce_dict_list(self.adapter.get_strings())
        return self._coerce_dict_list(self._cmdj("izzj", []))

    def _get_sections(self) -> list[dict[str, Any]]:
        if self._can_use_direct_method("get_sections"):
            return self._coerce_dict_list(self.adapter.get_sections())
        return self._coerce_dict_list(self._cmdj("iSj", []))
