"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFlags = exports.Flags = void 0;
const react_1 = __importDefault(require("react"));
const useReactifySearchContext_1 = require("./useReactifySearchContext");
const utility_1 = require("../utility");
var Flags;
(function (Flags) {
    Flags["SENSOR_SORT"] = "reactify-search:flag_sensor_sort";
    Flags["SENSOR_COLLECTION"] = "reactify-search:flag_sensor_collection";
    Flags["SENSOR_PUBLISHED"] = "reactify-search:flag_sensor_published";
    Flags["SENSOR_INVENTORYAVAILABLE"] = "reactify-search:flag_sensor_inventoryavailable";
    Flags["SENSOR_SEARCHTERM"] = "reactify-search:flag_sensor_searchterm";
    Flags["SENSOR_SEARCHTERM_VALUE"] = "reactify-search:flag_sensor_searchterm_value";
    Flags["SENSOR_SEARCHTERM_PHRASE"] = "reactify-search:flag_sensor_searchterm_phrase";
    Flags["SENSOR_SEARCHTERM_PHRASE_SYNONYMS"] = "reactify-search:flag_sensor_searchterm_phrase_synonyms";
    Flags["SENSOR_SEARCHTERM_PHRASE_PREFIX"] = "reactify-search:flag_sensor_searchterm_phrase_prefix";
    Flags["SENSOR_SEARCHTERM_PHRASE_PREFIX_SYNONYMS"] = "reactify-search:flag_sensor_searchterm_phrase_prefix_synonyms";
    Flags["SENSOR_SEARCHTERM_CROSS_FIELDS"] = "reactify-search:flag_sensor_searchterm_cross_fields";
    Flags["SENSOR_SEARCHTERM_CROSS_FIELDS_SYNONYMS"] = "reactify-search:flag_sensor_searchterm_cross_fields_synonyms";
    Flags["SENSOR_SEARCHTERM_SPAN_FIRST"] = "reactify-search:flag_sensor_searchterm_span_first";
    Flags["SENSOR_SEARCHTERM_SPAN_FIRST_SYNONYMS"] = "reactify-search:flag_sensor_searchterm_span_first_synonyms";
})(Flags || (exports.Flags = Flags = {}));
const useFlags = () => {
    const { options } = (0, useReactifySearchContext_1.useReactifySearchContext)();
    const [delta, setDelta] = react_1.default.useState(Date.now());
    const getFlag = react_1.default.useCallback((flag) => {
        var _a;
        if (typeof window === "undefined") {
            return true;
        }
        const flagFromOptions = (_a = options.flags) === null || _a === void 0 ? void 0 : _a[flag];
        const flagFromSession = window.sessionStorage.getItem(flag) === "false" ? false : true;
        if (typeof flagFromOptions === "undefined") {
            return flagFromSession;
        }
        return flagFromOptions;
    }, [delta]);
    const setFlag = react_1.default.useCallback((flag, value) => {
        if (typeof window === "undefined") {
            return;
        }
        window.sessionStorage.setItem(flag, `${value}`);
        setDelta(Date.now());
    }, [delta]);
    const flags = react_1.default.useMemo(() => {
        return Object.values(Flags).reduce((acc, flag) => (Object.assign(Object.assign({}, acc), { [flag]: getFlag(flag) })), {});
    }, [delta]);
    react_1.default.useEffect(() => {
        for (const flag of Object.values(Flags)) {
            setFlag(flag, getFlag(flag));
        }
    }, []);
    react_1.default.useEffect(() => {
        utility_1.debug.log("useFlags", "flags", flags);
    }, [flags]);
    return {
        flags: flags,
        setFlag: setFlag,
        getFlag: getFlag,
    };
};
exports.useFlags = useFlags;
//# sourceMappingURL=useFlags.js.map