from typing import Dict

from pypos.modules.printer.config.printer_config import DEFAULT_PAPER_SIZE

# edited by glg


class PrinterSettingsUiStateService:
    @staticmethod
    def normalize_printer_entry(name: str, paper_size: str, is_default: bool = False) -> Dict:
        return {
            "name": str(name or "").strip(),
            "paper_size": str(paper_size or DEFAULT_PAPER_SIZE).strip() or DEFAULT_PAPER_SIZE,
            "default": bool(is_default),
        }

    @staticmethod
    def is_valid_printer_index(index: int, total_items: int) -> bool:
        try:
            idx = int(index)
        except (TypeError, ValueError, KeyError, AttributeError, RuntimeError, OSError, LookupError, ArithmeticError, ImportError):
            return False
        try:
            total = int(total_items)
        except (TypeError, ValueError, KeyError, AttributeError, RuntimeError, OSError, LookupError, ArithmeticError, ImportError):
            total = 0
        return 0 <= idx < max(0, total)

    @staticmethod
    def build_settlement_option_log(option_key: str, enabled: bool) -> str:
        key = str(option_key or "").strip() or "-"
        return f"[PrinterSettings] Settlement option disimpan: {key}={1 if bool(enabled) else 0}"
