"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.featureFlagsReducer = exports.featureFlagsActions = exports.featureFlagsAsyncActions = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const featureGate_1 = require("../../../../common/features/featureGate");
const slice_1 = require("../current-user/slice");
const featureFlagsAsyncActions = {
    detectFeaturesAsync: (0, toolkit_1.createAsyncThunk)('featureFlags/detectFeatures', (...args_1) => tslib_1.__awaiter(void 0, [...args_1], void 0, function* (featuresOverride = null, thunkApi) {
        yield thunkApi.dispatch(slice_1.currentUserAsyncActions.getCapabilitiesAsync());
        if (featuresOverride !== null) {
            for (const [k, v] of Object.entries(featuresOverride)) {
                thunkApi.dispatch(featureFlagsActions.setFeatureFlag({
                    featureId: k,
                    isEnabled: v,
                }));
            }
        }
    })),
};
exports.featureFlagsAsyncActions = featureFlagsAsyncActions;
const initialState = {
    data: featureGate_1.STANDALONE_DEPLOYMENT_ENABLED_FEATURES,
    error: null,
    meta: {
        status: 'idle',
        updatedAt: null,
        currentRequestId: null,
    },
};
const featureFlagsSlice = (0, toolkit_1.createSlice)({
    name: 'featureFlags',
    initialState,
    reducers: {
        setMeta: (state, action) => {
            state.meta.updatedAt = new Date().toUTCString();
            state.meta.status = action.payload.meta.requestStatus;
            state.meta.currentRequestId = action.payload.meta.requestId;
            return state;
        },
        setFeatureFlag: (state, action) => {
            const { featureId, isEnabled } = action.payload;
            state.data[featureId] = isEnabled;
            state.error = null;
            state.meta.updatedAt = new Date().toUTCString();
            return state;
        },
    },
    extraReducers: (builder) => {
        for (const { pending, fulfilled, rejected } of Object.values(featureFlagsAsyncActions)) {
            builder.addCase(pending, (state, action) => {
                if (/idle|fulfilled|rejected/.test(state.meta.status)) {
                    featureFlagsActions.setMeta(action);
                }
                return state;
            });
            builder.addCase(fulfilled, (state, action) => {
                if (/idle|pending/.test(state.meta.status)) {
                    featureFlagsActions.setMeta(action);
                }
                return state;
            });
            builder.addCase(rejected, (state, action) => {
                if (/idle|pending/.test(state.meta.status)) {
                    featureFlagsActions.setMeta(action);
                    state.error = action.error;
                }
                return state;
            });
        }
    },
});
const featureFlagsActions = featureFlagsSlice.actions;
exports.featureFlagsActions = featureFlagsActions;
const featureFlagsReducer = featureFlagsSlice.reducer;
exports.featureFlagsReducer = featureFlagsReducer;
//# sourceMappingURL=slice.js.map