import React__default, { createContext, useContext, useRef } from 'react';
import { deleteCookie } from './miscCookieHelper.js';
import { u as useSSOV2Store, I as IS_SSOV2_ENABLED } from './store-b88d97b9.js';
import { T as TogetherComponentGlobalContext, k as getUrlToSignUp } from './utils-f5ed68ed.js';
import { B as BEARER_TOKEN_COOKIE } from './constants-67be5c94.js';
import './tslib.es6-ab63fd84.js';
import 'zustand';
import './index-9ddd1558.js';
import './constantsIsProduction.js';
import './normalizeLink-0bc93fbf.js';
import './constantsDomainLocales.js';
import './Locale-87b46912.js';
import './index-a8162282.js';
import 'date-fns/locale';
import './index-91c6a49c.js';
import './constantsSite.js';
import './constantsRiskScreener.js';
import 'react-phone-number-input';
import './index-95942e9e.js';
import 'slugify';
import 'string-format';
import './togetherApiPaths.js';

const TogetherAuthConfigContext = createContext({});
const TogetherAuthConfigContextProvider = ({ children, defaultParams, }) => {
    return (React__default.createElement(TogetherAuthConfigContext.Provider, { value: {
            defaultParams,
        } }, children));
};
const useTogetherAuthRequiredAction = (propsParams) => {
    const { triggerLogin } = useSSOV2Store();
    const { data: { userInfo, env: { togetherBasePath }, }, } = useContext(TogetherComponentGlobalContext);
    const { defaultParams } = useContext(TogetherAuthConfigContext) || {};
    const loggedInRef = useRef(!!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id));
    loggedInRef.current = !!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id);
    const isFirstInteractRef = useRef(userInfo &&
        typeof userInfo.communities_interacted === 'number' &&
        userInfo.communities_interacted === 0);
    isFirstInteractRef.current =
        userInfo &&
            typeof userInfo.communities_interacted === 'number' &&
            userInfo.communities_interacted === 0;
    const authActionWrapper = (func, extraParams) => {
        const params = Object.assign(Object.assign(Object.assign({}, defaultParams), propsParams), extraParams);
        if (!loggedInRef.current || isFirstInteractRef.current) {
            if (!loggedInRef.current) {
                deleteCookie(BEARER_TOKEN_COOKIE);
            }
            if (params === null || params === void 0 ? void 0 : params._joinCommunityIds) {
                localStorage.setItem('to_join_community_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_community_ids') || '[]') || [])
                    .concat(params._joinCommunityIds)
                    .filter(Boolean)))));
                params === null || params === void 0 ? true : delete params._joinCommunityIds;
            }
            if (params === null || params === void 0 ? void 0 : params._joinTopicIds) {
                localStorage.setItem('to_join_topic_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_topic_ids') || '[]') || [])
                    .concat(params._joinTopicIds)
                    .filter(Boolean)))));
                params === null || params === void 0 ? true : delete params._joinTopicIds;
            }
            const currentLocation = (params === null || params === void 0 ? void 0 : params._location) || window.location.href;
            params === null || params === void 0 ? true : delete params._location;
            const redirectUrl = getUrlToSignUp(currentLocation, params, togetherBasePath);
            if (IS_SSOV2_ENABLED()) {
                const returnUrlParam = redirectUrl.slice(redirectUrl.indexOf('returnUrl=') + 10);
                if (returnUrlParam) {
                    const originalRedirectUrl = decodeURIComponent(returnUrlParam);
                    triggerLogin({
                        returnUrl: originalRedirectUrl,
                        source: 'communities',
                    });
                }
            }
            else {
                window.location.href = redirectUrl;
            }
            return null;
        }
        else {
            return func && func();
        }
    };
    return {
        authActionWrapper,
        params: Object.assign(Object.assign({}, defaultParams), propsParams),
    };
};

export { TogetherAuthConfigContext, TogetherAuthConfigContextProvider, useTogetherAuthRequiredAction };
