export interface FeatureDetectionResult {
    critical: boolean;
    nice: boolean;
    missing: string[];
}

export function featureDetect(): FeatureDetectionResult {
    console.info("Running feature detection");
    // Define critical features
    const criticalFeatures = {
        fetch: "fetch" in window,
        localStorage: "localStorage" in window && window["localStorage"] !== null,
        sessionStorage: "sessionStorage" in window && window["sessionStorage"] !== null,
        promise: "Promise" in window,
    };

    // Define nice-to-have features
    const niceToHaveFeatures = {
        geolocation: "geolocation" in navigator,
        serviceWorker: "serviceWorker" in navigator,
        dialog: "HTMLDialogElement" in window,
        popover: "showPopover" in HTMLElement.prototype, // Note: 'Popover' might need more specific checks based on implementation
    };

    // Initialize result object
    const result = {
        critical: true,
        nice: true,
        missing: [],
    };

    // Check critical features
    for (const [feature, available] of Object.entries(criticalFeatures)) {
        if (!available) {
            result.critical = false;
            result.missing.push(feature);
        }
    }

    // Check nice-to-have features
    for (const [feature, available] of Object.entries(niceToHaveFeatures)) {
        if (!available) {
            result.nice = false;
            result.missing.push(feature);
        }
    }

    return result;
}
