"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.currentUserReducer = exports.currentUserActions = exports.currentUserAsyncActions = void 0;
const tslib_1 = require("tslib");
const toolkit_1 = require("@reduxjs/toolkit");
const current_account_api_1 = require("../../../../common/api/accounts-management-service/current-account-api");
const organizations_api_1 = require("../../../../common/api/accounts-management-service/organizations-api");
const currentUserAsyncActions = {
    getAccountAsync: (0, toolkit_1.createAsyncThunk)('currentUser/getAccountAsync', (_, thunkApi) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const account = yield current_account_api_1.CurrentAccountApi.getCurrentAccount();
        if (account) {
            thunkApi.dispatch(currentUserActions.setAccount(account));
        }
        else {
            thunkApi.rejectWithValue({
                message: 'No account data available',
            });
        }
    })),
    getUserOrganizationWithCapabilitiesAsync: (0, toolkit_1.createAsyncThunk)('currentUser/getUserOrganizationWithCapabilitiesAsync', (orgId, thunkApi) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const organization = yield organizations_api_1.OrganizationsApi.getOrganization(orgId);
        if (organization) {
            thunkApi.dispatch(currentUserActions.setOrganizationCapabilities((_a = organization.capabilities) !== null && _a !== void 0 ? _a : []));
        }
        else {
            thunkApi.rejectWithValue({
                message: 'No organization data found',
            });
        }
    })),
    getCapabilitiesAsync: (0, toolkit_1.createAsyncThunk)('currentUser/getCapabilitiesAsync', (_, thunkApi) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        yield thunkApi.dispatch(currentUserAsyncActions.getAccountAsync());
        const { currentUser } = thunkApi.getState();
        const orgId = (_a = currentUser.data) === null || _a === void 0 ? void 0 : _a.organization_id;
        if (orgId) {
            yield thunkApi.dispatch(currentUserAsyncActions.getUserOrganizationWithCapabilitiesAsync(orgId));
        }
        else {
            thunkApi.rejectWithValue({
                message: 'Account has no organization ID',
            });
        }
    })),
};
exports.currentUserAsyncActions = currentUserAsyncActions;
const currentUserSlice = (0, toolkit_1.createSlice)({
    name: 'currentUser',
    initialState: () => ({
        data: null,
        error: null,
        meta: {
            status: 'idle',
            updatedAt: null,
            currentRequestId: null,
        },
    }),
    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;
        },
        setAccount: (state, action) => {
            state.data = action.payload;
            state.error = null;
            state.meta.updatedAt = new Date().toUTCString();
            return state;
        },
        setOrganizationCapabilities: (state, action) => {
            if (state.data !== null && state.data.organization) {
                state.data.organization.capabilities = action.payload;
                state.error = null;
                state.meta.updatedAt = new Date().toUTCString();
            }
            return state;
        },
    },
    extraReducers: (builder) => {
        for (const { pending, fulfilled, rejected } of Object.values(currentUserAsyncActions)) {
            builder.addCase(pending, (state, action) => {
                if (/idle|fulfilled|rejected/.test(state.meta.status)) {
                    currentUserActions.setMeta(action);
                }
                return state;
            });
            builder.addCase(fulfilled, (state, action) => {
                if (/idle|pending/.test(state.meta.status)) {
                    currentUserActions.setMeta(action);
                }
                return state;
            });
            builder.addCase(rejected, (state, action) => {
                if (/idle|pending/.test(state.meta.status)) {
                    currentUserActions.setMeta(action);
                    state.error = action.error;
                }
                return state;
            });
        }
    },
});
const currentUserActions = currentUserSlice.actions;
exports.currentUserActions = currentUserActions;
const currentUserReducer = currentUserSlice.reducer;
exports.currentUserReducer = currentUserReducer;
//# sourceMappingURL=slice.js.map