var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useEffect } from 'react';
import { GrowthBook, useFeatureIsOn as featureIsOn, useFeatureValue as featureValue, IfFeatureEnabled as ifFeatureEnabled } from '@growthbook/growthbook-react';
let gb = void 0;
function getInstance() {
    if (gb)
        return gb;
    throw new Error('Growthbook has not been initialized');
}
export function init(context) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!gb) {
            gb = new GrowthBook(context);
            yield gb.loadFeatures();
        }
        return gb;
    });
}
export function useFlags(cb, deps = []) {
    useEffect(() => {
        if (gb)
            cb(gb);
    }, [gb, ...deps]);
}
export function isOn(flag) {
    const gb = getInstance();
    return gb.isOn(flag);
}
export function isOff(flag) {
    const gb = getInstance();
    return gb.isOff(flag);
}
export function getFeatureValue(flag, defaultValue) {
    const gb = getInstance();
    return gb.getFeatureValue(flag, defaultValue);
}
export function isReady() {
    return (gb && gb.ready) || false;
}
export const useFeatureIsOn = featureIsOn;
export const useFeatureValue = featureValue;
export const IfFeatureEnabled = ifFeatureEnabled;
export const IfFeatureDisabled = ({ feature, children }) => {
    const ready = isReady();
    if (!ready)
        return null;
    const enabled = isOn(feature);
    return enabled ? null : _jsx(_Fragment, { children: children });
};
export const IfFeature = ({ feature, enabled, disabled }) => {
    const ready = isReady();
    if (!ready)
        return null;
    const isEnabled = isOn(feature);
    return isEnabled ? _jsx(_Fragment, { children: enabled }) : _jsx(_Fragment, { children: disabled });
};
