#!/usr/bin/env python3
"""PE resource helpers."""

from typing import Any

from ..infrastructure.command_helpers import cmd as cmd_helper
from ..infrastructure.command_helpers import cmdj as cmdj_helper
from ..domain.formats.pe_info import normalize_resource_entries, parse_version_info_text


def get_resource_info(adapter: Any, logger: Any) -> list[dict[str, Any]]:
    resources: list[dict[str, Any]] = []

    try:
        getter = getattr(adapter, "get_resources_info", None)
        res_info = getter() if callable(getter) else cmdj_helper(adapter, None, "iRj", [])
        if res_info:
            resources.extend(normalize_resource_entries(res_info))
    except Exception as exc:
        logger.error("Error getting resource info: %s", exc)

    return resources


def get_version_info(adapter: Any, logger: Any) -> dict[str, str]:
    version_info: dict[str, str] = {}

    try:
        getter = getattr(adapter, "get_pe_version_info_text", None)
        version_result = getter() if callable(getter) else cmd_helper(adapter, None, "iR~version")
        if version_result:
            version_info = parse_version_info_text(version_result)
    except Exception as exc:
        logger.error("Error getting version info: %s", exc)

    return version_info
