import { OpenFeature, } from "@openfeature/web-sdk";
import { connectConfig } from "./connect.config.js";
/**
 * QueryParamProvider reads feature flags from URL query parameters.
 *
 * Usage:
 *   const params = new URLSearchParams(window.location.search);
 *   const provider = new QueryParamProvider(params);
 *   await OpenFeature.setProviderAndWait(provider);
 *
 * Query parameter format:
 *   ?FEATURE_DUAL_ACTION_CREATE_ENABLED=true&FEATURE_FOO=false
 */
export class QueryParamProvider {
    runsOn = "client";
    metadata = {
        name: "QueryParamProvider",
    };
    flags;
    constructor(searchParams) {
        this.flags = new Map();
        // Extract all query parameters that look like feature flags
        for (const [key, value] of searchParams.entries()) {
            this.flags.set(key, value);
        }
    }
    resolveBooleanEvaluation(flagKey, defaultValue) {
        const value = this.flags.get(flagKey);
        if (value === undefined) {
            return {
                value: defaultValue,
                reason: "DEFAULT",
            };
        }
        // Parse boolean from string
        const boolValue = value.toLowerCase() === "true" || value === "1";
        return {
            value: boolValue,
            reason: "STATIC",
            variant: value,
        };
    }
    resolveStringEvaluation(flagKey, defaultValue) {
        const value = this.flags.get(flagKey);
        if (value === undefined) {
            return {
                value: defaultValue,
                reason: "DEFAULT",
            };
        }
        return {
            value,
            reason: "STATIC",
            variant: value,
        };
    }
    resolveNumberEvaluation(flagKey, defaultValue) {
        const value = this.flags.get(flagKey);
        if (value === undefined) {
            return {
                value: defaultValue,
                reason: "DEFAULT",
            };
        }
        const numValue = Number(value);
        if (isNaN(numValue)) {
            return {
                value: defaultValue,
                reason: "ERROR",
                errorCode: "PARSE_ERROR",
                errorMessage: `Failed to parse "${value}" as a number`,
            };
        }
        return {
            value: numValue,
            reason: "STATIC",
            variant: value,
        };
    }
    resolveObjectEvaluation(flagKey, defaultValue) {
        const value = this.flags.get(flagKey);
        if (value === undefined) {
            return {
                value: defaultValue,
                reason: "DEFAULT",
            };
        }
        try {
            const objValue = JSON.parse(value);
            return {
                value: objValue,
                reason: "STATIC",
                variant: value,
            };
        }
        catch (error) {
            return {
                value: defaultValue,
                reason: "ERROR",
                errorCode: "PARSE_ERROR",
                errorMessage: `Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`,
            };
        }
    }
}
/**
 * Initialize OpenFeature with the QueryParamProvider.
 * Reads feature flags from query parameters.
 */
export async function initFeatureFlags(searchParams) {
    const params = searchParams ??
        (typeof window !== "undefined"
            ? new URLSearchParams(window.location.search)
            : new URLSearchParams());
    const provider = new QueryParamProvider(params);
    await OpenFeature.setProviderAndWait(provider);
    const features = new Map();
    const client = OpenFeature.getClient();
    features.set(FEATURE_LEGACY_READ_ENABLED, client.getBooleanValue(FEATURE_LEGACY_READ_ENABLED, true));
    features.set(FEATURE_LEGACY_WRITE_ENABLED, client.getBooleanValue(FEATURE_LEGACY_WRITE_ENABLED, true));
    features.set(FEATURE_CHANNEL_SYNC_ENABLED, client.getBooleanValue(FEATURE_CHANNEL_SYNC_ENABLED, false));
    // Query param overrides env var for inspector
    const inspectorFromParam = params.has(FEATURE_INSPECTOR_ENABLED);
    const inspectorEnabled = inspectorFromParam
        ? client.getBooleanValue(FEATURE_INSPECTOR_ENABLED, false)
        : connectConfig.content.inspectorEnabled;
    features.set(FEATURE_INSPECTOR_ENABLED, inspectorEnabled);
    return features;
}
const FEATURE_LEGACY_READ_ENABLED = "FEATURE_LEGACY_READ_ENABLED";
const FEATURE_LEGACY_WRITE_ENABLED = "FEATURE_LEGACY_WRITE_ENABLED";
const FEATURE_CHANNEL_SYNC_ENABLED = "FEATURE_CHANNEL_SYNC_ENABLED";
const FEATURE_INSPECTOR_ENABLED = "FEATURE_INSPECTOR_ENABLED";
/**
 * If true, reads go through legacy reactor.
 *
 * If false, reads go through the new reactor.
 */
export async function isLegacyReadEnabled() {
    const client = OpenFeature.getClient();
    return Promise.resolve(client.getBooleanValue(FEATURE_LEGACY_READ_ENABLED, true));
}
/**
 * If true, writes go through legacy reactor.
 *
 * If false, writes go through the new reactor.
 */
export async function isLegacyWriteEnabled() {
    const client = OpenFeature.getClient();
    return Promise.resolve(client.getBooleanValue(FEATURE_LEGACY_WRITE_ENABLED, true));
}
/**
 * If true, sync through channels and disables sync through legacy reactor.
 *
 * If false, sync through legacy reactor and disables sync through channels.
 */
export async function isChannelSyncEnabled() {
    const client = OpenFeature.getClient();
    return Promise.resolve(client.getBooleanValue(FEATURE_CHANNEL_SYNC_ENABLED, false));
}
/**
 * If true, shows the inspector button in the sidebar.
 * Defaults to false (hidden).
 */
export async function isInspectorEnabled() {
    const client = OpenFeature.getClient();
    return Promise.resolve(client.getBooleanValue(FEATURE_INSPECTOR_ENABLED, false));
}
//# sourceMappingURL=feature-flags.js.map