import re

# edited by glg


class TransaksiValueUtils:
    @staticmethod
    def as_positive_int(value, default=0):
        try:
            parsed = int(str(value).strip())
        except Exception:
            parsed = int(default or 0)
        return parsed if parsed > 0 else 0

    @staticmethod
    def extract_point_from_diskon_log(diskon_log):
        text = str(diskon_log or "").strip()
        if not text:
            return 0
        match = re.search(r"(?:^|;)point=([^;]*)", text)
        if not match:
            return 0
        try:
            return max(0, int(float(str(match.group(1) or "0").strip() or 0)))
        except Exception:
            return 0

    @staticmethod
    def extract_diskon_customer_from_diskon_log(diskon_log):
        text = str(diskon_log or "").strip()
        if not text:
            return 0.0
        match = re.search(r"(?:^|;)diskon_customer=([^;]*)", text)
        if not match:
            return 0.0
        try:
            return max(0.0, float(str(match.group(1) or "0").strip() or 0.0))
        except Exception:
            return 0.0

    @staticmethod
    def extract_ppn_mode_from_diskon_log(diskon_log):
        text = str(diskon_log or "").strip().lower()
        if not text:
            return ""
        match = re.search(r"(?:^|;)ppn_mode=([^;]*)", text)
        if not match:
            return ""
        value = str(match.group(1) or "").strip().lower()
        if value in {"include", "exclude"}:
            return value
        return ""
