"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePostOnboardingHubState = void 0;
const react_redux_1 = require("react-redux");
const react_1 = require("react");
const featureFlags_1 = require("../../featureFlags");
const reducer_1 = require("../reducer");
const usePostOnboardingContext_1 = require("./usePostOnboardingContext");
const getIsFeatureEnabled = (action, getFeature) => {
    if (!action)
        return false;
    if (!action.featureFlagId)
        return true;
    const flag = getFeature(action.featureFlagId);
    if (!flag?.enabled)
        return false;
    return !action.featureFlagParamId || !!flag.params?.[action.featureFlagParamId];
};
/**
 * @returns an object representing the state that should be rendered on the post
 * onboarding hub screen.
 *
 * This takes feature flagging into account so the logic is
 * resistant to flags getting enabled/disabled over time (for a given disabled
 * feature flag, the actions pointing to it will be excluded).
 * */
function usePostOnboardingHubState() {
    const hubState = (0, react_redux_1.useSelector)(reducer_1.hubStateSelector);
    const postOnboardingContext = (0, usePostOnboardingContext_1.usePostOnboardingContext)();
    const { getPostOnboardingAction } = postOnboardingContext;
    const { getFeature } = (0, featureFlags_1.useFeatureFlags)();
    return (0, react_1.useMemo)(() => {
        if (!getPostOnboardingAction)
            return {
                deviceModelId: hubState.deviceModelId,
                lastActionCompleted: null,
                actionsState: [],
                postOnboardingInProgress: hubState.postOnboardingInProgress,
            };
        const actionsState = hubState.actionsToComplete.flatMap(actionId => {
            const action = getPostOnboardingAction(actionId);
            const isFeatureEnabled = getIsFeatureEnabled(action, getFeature);
            if (!action || !isFeatureEnabled) {
                return [];
            }
            return [{ ...action, completed: !!hubState.actionsCompleted[actionId] }];
        });
        const lastActionCompleted = hubState.lastActionCompleted
            ? getPostOnboardingAction(hubState.lastActionCompleted)
            : null;
        const isLastActionCompletedEnabled = lastActionCompleted && getIsFeatureEnabled(lastActionCompleted, getFeature);
        return {
            deviceModelId: hubState.deviceModelId,
            lastActionCompleted: isLastActionCompletedEnabled ? lastActionCompleted : null,
            actionsState,
            postOnboardingInProgress: hubState.postOnboardingInProgress,
        };
    }, [getFeature, hubState, getPostOnboardingAction]);
}
exports.usePostOnboardingHubState = usePostOnboardingHubState;
//# sourceMappingURL=usePostOnboardingHubState.js.map