"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useStartPostOnboardingCallback = void 0;
const react_redux_1 = require("react-redux");
const usePostOnboardingContext_1 = require("./usePostOnboardingContext");
const react_1 = require("react");
const featureFlags_1 = require("../../featureFlags");
const actions_1 = require("../actions");
/**
 * Use this to initialize AND navigate to the post onboarding hub for a given
 * device model.
 *
 * @param deviceModelId
 * @returns a function that can be called to initialize the post
 * onboarding for the given device model and navigate to the post onboarding
 * hub.
 * TODO: unit test this
 */
function useStartPostOnboardingCallback() {
    const dispatch = (0, react_redux_1.useDispatch)();
    const { getFeature } = (0, featureFlags_1.useFeatureFlags)();
    const { getPostOnboardingActionsForDevice, navigateToPostOnboardingHub } = (0, usePostOnboardingContext_1.usePostOnboardingContext)();
    return (0, react_1.useCallback)((options) => {
        const { deviceModelId, mock = false, fallbackIfNoAction, resetNavigationStack } = options;
        const actions = getPostOnboardingActionsForDevice(deviceModelId, mock).filter(actionWithState => !actionWithState.featureFlagId || getFeature(actionWithState.featureFlagId)?.enabled);
        dispatch((0, actions_1.initPostOnboarding)({
            deviceModelId,
            actionsIds: actions.map(action => action.id),
        }));
        if (actions.length === 0) {
            dispatch((0, actions_1.postOnboardingSetFinished)());
            if (fallbackIfNoAction)
                fallbackIfNoAction();
            return;
        }
        navigateToPostOnboardingHub(resetNavigationStack);
    }, [dispatch, getFeature, getPostOnboardingActionsForDevice, navigateToPostOnboardingHub]);
}
exports.useStartPostOnboardingCallback = useStartPostOnboardingCallback;
//# sourceMappingURL=useStartPostOnboardingCallback.js.map