import { __, sprintf } from "@wordpress/i18n";


const prettifyCode = (code) => String(code || "").trim().replace(/_/g, " ");

const getErrorDetail = (code, prefix) => {
    const detail = code.slice(prefix.length).trim();
    return detail || __("No error details were returned.", "cache-autopilot");
};

const humanizeBrotliProfileErrorCode = (code) => {
    const normalized = String(code || "").trim();

    if (normalized === "") {
        return "";
    }

    if (normalized.startsWith("wordpress_http_exception:")) {
        return sprintf(
            /* translators: %s: HTTP exception details. */
            __("The browser-profile request raised an exception: %s", "cache-autopilot"),
            getErrorDetail(normalized, "wordpress_http_exception:"),
        );
    }

    if (normalized.startsWith("wordpress_http_error:")) {
        return sprintf(
            /* translators: %s: WordPress HTTP API error details. */
            __("The browser-profile request failed: %s", "cache-autopilot"),
            getErrorDetail(normalized, "wordpress_http_error:"),
        );
    }

    switch (normalized) {
    case "wordpress_http_api_unavailable":
        return __("The WordPress HTTP API is unavailable.", "cache-autopilot");
    case "curl_decoding_override_unavailable":
        return __("This cURL runtime cannot safely handle the browser-profile response body.", "cache-autopilot");
    case "empty_url":
        return __("The target URL is empty.", "cache-autopilot");
    case "non_https_url":
        return __("The target URL is not HTTPS.", "cache-autopilot");
    case "wordpress_http_request_unsuccessful":
        return __("Browser-profile request returned an unsuccessful HTTP status.", "cache-autopilot");
    default:
        return prettifyCode(normalized);
    }
};

const humanizeFallbackReasonCode = (code) => {
    const normalized = String(code || "").trim();

    if (normalized === "") {
        return "";
    }

    if (normalized.startsWith("brotli_profile_failed:")) {
        const errorCode = normalized.slice("brotli_profile_failed:".length)
            || "wordpress_http_request_unsuccessful";
        return humanizeBrotliProfileErrorCode(errorCode);
    }

    return humanizeBrotliProfileErrorCode(normalized);
};

export const getFallbackReasonDisplay = (reasonCode) => {
    const code = String(reasonCode || "").trim();
    if (code === "") {
        return {
            code: "\u2014",
            text: "",
        };
    }

    return {
        code,
        text: humanizeFallbackReasonCode(code),
    };
};

export const getBrotliProfileErrorMessage = (errorCode) => {
    const code = String(errorCode || "").trim();
    if (code === "") {
        return "";
    }

    return humanizeBrotliProfileErrorCode(code);
};

export const formatTransportUsedLabel = (transportUsed) => {
    const normalized = String(transportUsed || "").trim();
    if (normalized === "") {
        return "\u2014";
    }

    if (normalized === "brotli_profile") {
        return __("Browser-profile request (WordPress HTTP API)", "cache-autopilot");
    }

    if (normalized === "wp_remote_get") {
        return __("Standard request (WordPress HTTP API)", "cache-autopilot");
    }

    return normalized;
};
