<?php
/**
 * 判定理由辅助
 */

if (!function_exists('cloak_reason_append')) {
    function cloak_reason_append($reason, $suffix)
    {
        $reason = trim((string) $reason);
        $suffix = trim((string) $suffix);
        if ($suffix === '') {
            return $reason;
        }
        if ($reason === '') {
            return $suffix;
        }
        if (strpos($reason, $suffix) !== false) {
            return $reason;
        }
        return $reason . '；' . $suffix;
    }
}

if (!function_exists('cloak_reason_or')) {
    function cloak_reason_or($reason, $fallback)
    {
        $reason = trim((string) $reason);
        return $reason !== '' ? $reason : (string) $fallback;
    }
}

if (!function_exists('cloak_api_reason')) {
    function cloak_api_reason(array $return, $checkResult)
    {
        $reason = trim((string) ($return['reason'] ?? ''));
        if ($reason !== '') {
            return $reason;
        }
        $pass = ($checkResult === 'true' || $checkResult === true || !empty($return['result']));
        return $pass ? 'API判定通过' : 'API判定拒绝';
    }
}

if (!function_exists('cloak_finalize_reason')) {
    function cloak_finalize_reason($reason, $checkResult)
    {
        $reason = trim((string) $reason);
        if ($reason !== '') {
            return $reason;
        }
        $pass = ($checkResult === 'true');
        return $pass ? '判定通过(真实页)' : '判定拒绝(安全页)';
    }
}
