from pypos.core.utils.config_utils import read_app_settings


_CUSTOMER_DEFAULTS = {
    "customer_search_dialog_width": 900,
    "customer_search_dialog_height": 600,
    "customer_search_page_size": 100,
    "customer_setup_page_size": 100,
}


def get_customer_config():
    cfg = dict(_CUSTOMER_DEFAULTS)
    cfg.update(read_app_settings() or {})
    return cfg


def _as_int(value, default, min_value=1):
    try:
        parsed = int(value)
    except (TypeError, ValueError, KeyError, AttributeError, RuntimeError, OSError, LookupError, ArithmeticError, ImportError):
        parsed = int(default)
    if parsed < min_value:
        return int(default)
    return parsed


def get_customer_search_page_size(cfg=None):
    source = cfg if isinstance(cfg, dict) else get_customer_config()
    return _as_int(source.get("customer_search_page_size"), 100, min_value=10)


def get_customer_search_dialog_size(cfg=None):
    source = cfg if isinstance(cfg, dict) else get_customer_config()
    width = _as_int(source.get("customer_search_dialog_width"), 900, min_value=360)
    height = _as_int(source.get("customer_search_dialog_height"), 600, min_value=300)
    return width, height


def get_customer_setup_page_size(cfg=None):
    source = cfg if isinstance(cfg, dict) else get_customer_config()
    return _as_int(source.get("customer_setup_page_size"), 100, min_value=10)
