{"version":3,"sources":["../src/providers/growthbook.ts"],"sourcesContent":["import { GrowthBook, InitResponse } from '@growthbook/growthbook'\nimport { RudderAnalytics } from '@rudderstack/analytics-js'\nimport {\n    TGrowthbookAttributes,\n    TGrowthbookOptions,\n    GrowthbookConfigs,\n    TGrowthbookCoreAttributes,\n} from './growthbookTypes'\nimport { growthbookApi } from '../utils/urls'\nimport { createLogger } from '../utils/helpers'\n\nexport class Growthbook {\n    analytics = new RudderAnalytics()\n    GrowthBook: GrowthBook<GrowthbookConfigs>\n    private static _instance: Growthbook\n    isLoaded = false\n    status: void | InitResponse = undefined\n    private debug = false\n    private log = createLogger('[GrowthBook]', () => this.debug)\n\n    // we have to pass settings due the specific framework implementation\n    constructor(clientKey: string, decryptionKey: string, growthbookOptions: TGrowthbookOptions = {}, debug = false) {\n        this.debug = debug\n        const isLocalhost = typeof window !== 'undefined' ? window.location.hostname.includes('localhost') : false\n\n        this.GrowthBook = new GrowthBook<GrowthbookConfigs>({\n            apiHost: growthbookApi,\n            clientKey,\n            decryptionKey,\n            antiFlicker: false,\n            navigateDelay: 0,\n            antiFlickerTimeout: 3500,\n            subscribeToChanges: true,\n            enableDevMode: isLocalhost,\n            trackingCallback: (experiment, result) => {\n                this.log('trackingCallback | experiment viewed', {\n                    experiment_id: experiment.key,\n                    variation_id: result.variationId,\n                })\n                if (typeof window !== 'undefined' && window.dataLayer) {\n                    window.dataLayer.push({\n                        event: 'experiment_viewed',\n                        event_category: 'experiment',\n                        rudder_anonymous_id: this.analytics.getAnonymousId(),\n                        experiment_id: experiment.key,\n                        variation_id: result.variationId,\n                    })\n                }\n                this.analytics.track('experiment_viewed', {\n                    experimentId: experiment.key,\n                    variationId: result.variationId,\n                })\n            },\n            ...growthbookOptions,\n        })\n        this.init()\n    }\n\n    // for make instance by singleton\n    public static getGrowthBookInstance = (\n        clientKey: string,\n        decryptionKey?: string,\n        growthbookOptions?: TGrowthbookOptions,\n        debug = false\n    ) => {\n        if (!Growthbook._instance) {\n            Growthbook._instance = new Growthbook(clientKey, decryptionKey ?? '', growthbookOptions, debug)\n            return Growthbook._instance\n        }\n\n        // Warn if trying to reinitialize with different parameters\n        if (typeof window !== 'undefined' && console.warn) {\n            console.warn('GrowthBook instance already exists. Ignoring new initialization parameters.')\n        }\n\n        return Growthbook._instance\n    }\n\n    reapplyExperiment(url?: string) {\n        const currentUrl = url ?? (typeof window !== 'undefined' ? window.location.href : '')\n        this.GrowthBook.setURL(currentUrl)\n    }\n\n    // Utility function to wait for isLoaded to become true\n    private waitForIsLoaded(timeout = 10000): Promise<void> {\n        return new Promise((resolve, reject) => {\n            const startTime = Date.now()\n            const checkInterval = setInterval(() => {\n                if (this.isLoaded) {\n                    clearInterval(checkInterval)\n                    resolve()\n                } else if (Date.now() - startTime >= timeout) {\n                    clearInterval(checkInterval)\n                    reject(new Error('GrowthBook initialization timeout'))\n                }\n            }, 100)\n        })\n    }\n\n    setAttributes = ({\n        id,\n        country,\n        user_language,\n        device_language,\n        device_type,\n        utm_source,\n        utm_medium,\n        utm_campaign,\n        is_authorised,\n        url,\n        domain,\n        utm_content,\n        residence_country,\n        loggedIn,\n        network_type,\n        network_downlink,\n        user_id,\n        anonymous_id,\n        account_mode,\n    }: TGrowthbookAttributes) => {\n        const currentAttributes = this.GrowthBook.getAttributes()\n        const newAttributes = {\n            ...currentAttributes,\n            id,\n            ...(user_id !== undefined && { user_id }),\n            ...(anonymous_id !== undefined && { anonymous_id }),\n            ...(country !== undefined && { country }),\n            ...(residence_country !== undefined && { residence_country }),\n            ...(user_language !== undefined && { user_language }),\n            ...(device_language !== undefined && { device_language }),\n            ...(device_type !== undefined && { device_type }),\n            ...(utm_source !== undefined && { utm_source }),\n            ...(utm_medium !== undefined && { utm_medium }),\n            ...(utm_campaign !== undefined && { utm_campaign }),\n            ...(is_authorised !== undefined && { is_authorised }),\n            ...(url !== undefined && { url }),\n            ...(domain !== undefined && { domain }),\n            ...(utm_content !== undefined && { utm_content }),\n            ...(loggedIn !== undefined && { loggedIn }),\n            ...(network_type !== undefined && { network_type }),\n            ...(network_downlink !== undefined && { network_downlink }),\n            ...(account_mode !== undefined && { account_mode }),\n        }\n        this.log('setAttributes | updating GrowthBook attributes', newAttributes)\n        this.GrowthBook.setAttributes(newAttributes)\n    }\n    getFeatureValue = <K extends keyof GrowthbookConfigs, V extends GrowthbookConfigs[K]>(key: K, defaultValue: V) => {\n        const value = this.GrowthBook.getFeatureValue(key as string, defaultValue)\n        this.log('getFeatureValue', { key, value, defaultValue })\n        return value\n    }\n    getStatus = async (): Promise<{ isLoaded: boolean; status: void | InitResponse }> => {\n        await this.waitForIsLoaded()\n\n        return {\n            isLoaded: this.isLoaded,\n            status: this.status,\n        }\n    }\n    getFeatureState = (id: string) => {\n        const state = this.GrowthBook.evalFeature(id)\n        this.log('getFeatureState', { id, result: state })\n        return state\n    }\n    setUrl = (href: string) => {\n        this.log('setUrl', { href })\n        this.GrowthBook.setURL(href)\n    }\n    isOn = (key: string) => {\n        const result = this.GrowthBook.isOn(key)\n        this.log('isOn', { key, result })\n        return result\n    }\n\n    init = async () => {\n        this.log('init | initializing GrowthBook SDK')\n        const status = await this.GrowthBook.init({ timeout: 2000, streaming: true }).catch(() => {\n            // Silently handle initialization errors\n        })\n\n        this.status = status\n        this.isLoaded = true\n        this.log('init | GrowthBook SDK loaded', { status })\n    }\n\n    // Destroy the GrowthBook instance and reset singleton\n    destroy = () => {\n        this.GrowthBook.destroy()\n        this.isLoaded = false\n        this.status = undefined\n    }\n\n    // Reset the singleton instance (useful for testing)\n    public static resetInstance = () => {\n        if (Growthbook._instance) {\n            Growthbook._instance.destroy()\n            Growthbook._instance = undefined as any\n        }\n    }\n}\n\nexport type { GrowthbookConfigs, TGrowthbookAttributes, TGrowthbookOptions, TGrowthbookCoreAttributes }\n"],"mappings":";yFAAA,OAAS,cAAAA,MAAgC,yBAWlC,IAAMC,EAAN,MAAMA,CAAW,CAUpB,YAAYC,EAAmBC,EAAuBC,EAAwC,CAAC,EAAGC,E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sLK,cAAgB,IAAM,CAC5BA,EAAW,YACXA,EAAW,UAAU,QAAQ,EAC7BA,EAAW,UAAY,OAE/B,EA3LG,IAAM4C,EAAN5C","names":["GrowthBook","_Growthbook","clientKey","decryptionKey","growthbookOptions","debug","RudderAnalytics","createLogger","id","country","user_language","device_language","device_type","utm_source","utm_medium","utm_campaign","is_authorised","url","domain","utm_content","residence_country","loggedIn","network_type","network_downlink","user_id","anonymous_id","account_mode","newAttributes","key","defaultValue","value","state","href","result","status","isLocalhost","GrowthBook","growthbookApi","experiment","currentUrl","timeout","resolve","reject","startTime","checkInterval","Growthbook"]}