{"version":3,"file":"useExperiment.cjs","names":["useState","isEnabled"],"sources":["../../../src/analytics/useExperiment.tsx"],"sourcesContent":["'use client';\n\nimport { isEnabled } from '@intlayer/analytics/isEnabled';\nimport { useEffect, useState } from 'react';\n\n/**\n * The state of an A/B experiment for the current session.\n */\nexport type ExperimentState = {\n  /**\n   * The variant assigned to this session. Until assignment resolves (and on\n   * the server, or when analytics is disabled), this is the first variant —\n   * treat the first variant as the control.\n   */\n  variant: string;\n  /**\n   * `true` once the variant has been deterministically assigned and its\n   * exposure recorded. Useful to defer rendering when a flash of the control\n   * variant is not acceptable.\n   */\n  isAssigned: boolean;\n};\n\n/**\n * Assigns this session to a variant of an A/B experiment and records the\n * exposure, scoped to `experimentKey`, so the backend can compute per-variant\n * conversion rates against `useConversion` events.\n *\n * Assignment is deterministic per session (no server round-trip, stable across\n * re-mounts). The exposure is recorded once per mount.\n *\n * @param experimentKey - Unique key identifying the experiment.\n * @param variants - The candidate variant names; the first one is the control.\n * @param weights - Optional relative weights, one per variant (e.g. `[9, 1]`).\n * @returns The {@link ExperimentState} for this session.\n *\n * @example\n * ```tsx\n * const { variant } = useExperiment('homepage-hero', ['a', 'b']);\n * const content = useIntlayer(variant === 'b' ? 'hero-b' : 'hero');\n * ```\n */\nexport const useExperiment = (\n  experimentKey: string,\n  variants: string[],\n  weights?: number[]\n): ExperimentState => {\n  const [state, setState] = useState<ExperimentState>({\n    variant: variants[0] ?? '',\n    isAssigned: false,\n  });\n\n  useEffect(() => {\n    if (process.env.INTLAYER_ANALYTICS_ENABLED === 'false' || !isEnabled) {\n      return;\n    }\n\n    let cancelled = false;\n    let initialized = false;\n\n    import('@intlayer/analytics')\n      .then(({ initAnalyticsClient }) => {\n        if (cancelled) return;\n\n        // Ref-counted: guarantees a live client even when this hook's import\n        // resolves before the provider's. Balanced by stop in the cleanup.\n        const client = initAnalyticsClient();\n        initialized = true;\n\n        const variant = client.getVariant(experimentKey, variants, weights);\n        setState({ variant, isAssigned: true });\n\n        // Experiment-level exposure — the denominator of the conversion rate.\n        client.trackContentExposure({\n          dictionaryKey: experimentKey,\n          keyPath: '',\n          nodeType: 'experiment',\n          experimentKey,\n          variant,\n        });\n      })\n      .catch(() => {\n        /* chunk failed to load — control variant stays in place */\n      });\n\n    return () => {\n      cancelled = true;\n      import('@intlayer/analytics')\n        .then(({ stopAnalyticsClient }) => {\n          if (initialized) stopAnalyticsClient();\n        })\n        .catch(() => {});\n    };\n    // Assignment is deterministic; re-running on variant-array identity churn\n    // would double-record exposures.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [experimentKey]);\n\n  return state;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,MAAa,iBACX,eACA,UACA,YACoB;CACpB,MAAM,CAAC,OAAO,gBAAYA,gBAA0B;EAClD,SAAS,SAAS,MAAM;EACxB,YAAY;CACd,CAAC;CAED,2BAAgB;EACd,IAAI,QAAQ,IAAI,+BAA+B,WAAW,CAACC,yCACzD;EAGF,IAAI,YAAY;EAChB,IAAI,cAAc;EAElB,OAAO,sBAAsB,CAC1B,MAAM,EAAE,0BAA0B;GACjC,IAAI,WAAW;GAIf,MAAM,SAAS,oBAAoB;GACnC,cAAc;GAEd,MAAM,UAAU,OAAO,WAAW,eAAe,UAAU,OAAO;GAClE,SAAS;IAAE;IAAS,YAAY;GAAK,CAAC;GAGtC,OAAO,qBAAqB;IAC1B,eAAe;IACf,SAAS;IACT,UAAU;IACV;IACA;GACF,CAAC;EACH,CAAC,CAAC,CACD,YAAY,CAEb,CAAC;EAEH,aAAa;GACX,YAAY;GACZ,OAAO,sBAAsB,CAC1B,MAAM,EAAE,0BAA0B;IACjC,IAAI,aAAa,oBAAoB;GACvC,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACnB;CAIF,GAAG,CAAC,aAAa,CAAC;CAElB,OAAO;AACT"}