import IconProp from "../../../Types/Icon/IconProp";
import Icon from "../Icon/Icon";
import React, { useState } from "react";
const KERNEL_HINTS = [
    /\bKernel Version\b/iu,
    /\broot:/iu,
    /\bxnu-/iu,
    /\bDarwin Kernel\b/iu,
];
const KERNEL_PREFIX_REGEX = /^(Darwin|Linux|FreeBSD|Windows_NT)\s/u;
const looksLikeKernelString = (value) => {
    if (value.length > 50) {
        return true;
    }
    for (const r of KERNEL_HINTS) {
        if (r.test(value)) {
            return true;
        }
    }
    return false;
};
const summarizeKernel = (kernel) => {
    const archMatch = kernel.match(/\b(arm64|aarch64|x86_64|amd64|i[3-6]86|riscv64|ppc64le)\b/iu);
    const versionMatch = kernel.match(/\b(\d+\.\d+(?:\.\d+)?)\b/u);
    const nameMatch = kernel.match(/^(Darwin|Linux|FreeBSD|Windows_NT)\b/u);
    const parts = [];
    if (nameMatch) {
        parts.push(nameMatch[1]);
    }
    if (versionMatch) {
        parts.push(versionMatch[1]);
    }
    const arch = archMatch ? archMatch[1] : null;
    const head = parts.join(" ");
    if (!head && !arch) {
        return null;
    }
    if (head && arch) {
        return `${head} · ${arch}`;
    }
    return head || arch;
};
const parseOsVersion = (raw) => {
    const text = raw.trim();
    const fullMatch = text.match(/^(.+?)\s*\(([^()]+)\)\s*\((.+)\)\s*$/u);
    if (fullMatch && fullMatch[1] && fullMatch[2] && fullMatch[3]) {
        return {
            primary: fullMatch[1].trim(),
            secondary: fullMatch[2].trim(),
            kernel: fullMatch[3].trim(),
        };
    }
    const singleParen = text.match(/^(.+?)\s*\(([^()]+)\)\s*$/u);
    if (singleParen && singleParen[1] && singleParen[2]) {
        const head = singleParen[1].trim();
        const inner = singleParen[2].trim();
        if (looksLikeKernelString(inner)) {
            return { primary: head, kernel: inner };
        }
        return { primary: head, secondary: inner };
    }
    if (KERNEL_PREFIX_REGEX.test(text) && text.length > 40) {
        const headline = summarizeKernel(text) || text.split(/\s+/u)[0];
        return { primary: headline, kernel: text };
    }
    return { primary: text };
};
export const getOsVersionPrimary = (raw) => {
    if (!raw || raw.trim() === "") {
        return "";
    }
    return parseOsVersion(raw).primary;
};
const OsVersionDisplay = (props) => {
    const [isExpanded, setIsExpanded] = useState(false);
    if (!props.text || props.text.trim() === "" || props.text === "-") {
        return React.createElement("span", { className: "text-sm text-gray-400" }, "\u2014");
    }
    const parts = parseOsVersion(props.text);
    const kernelSummary = parts.kernel
        ? summarizeKernel(parts.kernel)
        : null;
    return (React.createElement("div", { className: `flex flex-col gap-1.5 min-w-0 ${props.className || ""}` },
        React.createElement("div", { className: "flex flex-wrap items-center gap-1.5" },
            React.createElement("span", { className: "inline-flex items-center gap-1.5 rounded-md bg-indigo-50 px-2 py-0.5 text-sm font-semibold text-indigo-700 ring-1 ring-inset ring-indigo-200" },
                React.createElement(Icon, { icon: IconProp.Cog, className: "h-3.5 w-3.5 text-indigo-500" }),
                React.createElement("span", { className: "break-all" }, parts.primary)),
            parts.secondary ? (React.createElement("span", { className: "inline-flex items-center rounded-md bg-gray-100 px-2 py-0.5 text-xs font-mono text-gray-700 ring-1 ring-inset ring-gray-200" }, parts.secondary)) : null),
        parts.kernel ? (React.createElement("div", { className: "mt-0.5" },
            React.createElement("button", { type: "button", onClick: () => {
                    setIsExpanded(!isExpanded);
                }, "aria-expanded": isExpanded, className: "inline-flex items-center gap-1 rounded text-xs font-medium text-indigo-600 hover:text-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-1 transition-colors" },
                React.createElement(Icon, { icon: IconProp.Info, className: "h-3 w-3 text-indigo-500" }),
                React.createElement("span", null, kernelSummary ? `Kernel · ${kernelSummary}` : "Kernel details"),
                React.createElement("svg", { className: `w-3 h-3 transition-transform duration-200 ${isExpanded ? "rotate-180" : ""}`, fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 2.25, "aria-hidden": "true" },
                    React.createElement("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M19 9l-7 7-7-7" }))),
            isExpanded ? (React.createElement("pre", { className: "mt-2 max-w-full overflow-x-auto whitespace-pre-wrap break-words rounded-md bg-gray-50 p-3 text-xs font-mono text-gray-700 ring-1 ring-inset ring-gray-200" }, parts.kernel)) : null)) : null));
};
export default OsVersionDisplay;
//# sourceMappingURL=OsVersionDisplay.js.map