/**
 * FeatureFlags — Lightweight feature flag system.
 *
 * Inspired by ConfigCat/LaunchDarkly evaluation patterns.
 * Supports boolean flags (enabled/disabled) and multivariate string flags
 * (AI_ENGINE enum) with Predefined Variations — define allowed values once,
 * select safely, no free-form typing.
 *
 * Imperative API (no React dependency):
 *   FeatureFlags.isEnabled(key)        → boolean
 *   FeatureFlags.getValue(key)         → string | null  (multivariate flags)
 *
 * React API:
 *   useFeatureFlag(key)                → boolean (boolean flags)
 *   useFeatureFlagValue(key)           → string | null (multivariate flags)
 *
 * Migration path:
 *   Replace _loadFlags() with a fetch to a remote flag provider
 *   (ConfigCat, LaunchDarkly, AWS AppConfig) when backend is available.
 *   The public API contract remains identical.
 */
var FeatureFlags = (function () {

    var _flags = {};
    var _loaded = false;

    /**
     * Initialise flags from the JSON config.
     * Called once — subsequent calls are no-ops.
     * Returns a Promise that resolves when flags are ready.
     */
    function init() {
        if (_loaded) {
            return Promise.resolve();
        }
        return fetch('components/FeatureFlags/feature-flags.json')
            .then(function (res) {
                if (!res.ok) throw new Error('Failed to load feature flags: ' + res.status);
                return res.json();
            })
            .then(function (config) {
                _flags = (config && config.flags) ? config.flags : {};
                _loaded = true;
            })
            .catch(function (err) {
                console.warn('[FeatureFlags] Could not load config, all flags default to false:', err.message);
                _flags = {};
                _loaded = true;
            });
    }

    /**
     * Get the string value of a multivariate flag.
     * Returns the flag's `value` field, or null for unknown/boolean-only flags.
     * Safe to call before init() — returns null.
     */
    function getValue(key) {
        var flag = _flags[key];
        if (!flag || flag.type !== 'string') return null;
        return typeof flag.value === 'string' ? flag.value : null;
    }

    /**
     * Check if a flag is enabled.
     * Returns false for unknown keys (safe default — ConfigCat/LaunchDarkly convention).
     * For multivariate flags, returns the `enabled` boolean (flag is active/inactive).
     */
    function isEnabled(key) {
        var flag = _flags[key];
        if (!flag) return false;
        return !!flag.enabled;
    }

    return {
        init: init,
        isEnabled: isEnabled,
        getValue: getValue
    };
})();

/**
 * useFeatureFlag — React hook for boolean flag evaluation.
 * Returns false for unknown keys. Reads from in-memory cache synchronously.
 */
var useFeatureFlag = function (key) {
    var state = React.useState(function () { return FeatureFlags.isEnabled(key); });
    return state[0];
};

/**
 * useFeatureFlagValue — React hook for multivariate flag evaluation.
 * Returns the string value of the flag, or null for unknown/boolean flags.
 * Used by AIEngineAdapter and components that branch on AI_ENGINE value.
 */
var useFeatureFlagValue = function (key) {
    var state = React.useState(function () { return FeatureFlags.getValue(key); });
    return state[0];
};

window.FeatureFlags = FeatureFlags;
window.useFeatureFlag = useFeatureFlag;
window.useFeatureFlagValue = useFeatureFlagValue;
