'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var miscCookieHelper = require('./miscCookieHelper.js');
var store = require('./store-bb6f9909.js');
var togetherComponentGlobalContext = require('./utils-302bc65c.js');
var constants = require('./constants-762249c4.js');
require('./tslib.es6-2c2ed574.js');
require('zustand');
require('./index-e083a917.js');
require('./constantsIsProduction.js');
require('./normalizeLink-20e290e8.js');
require('./constantsDomainLocales.js');
require('./Locale-fbe01177.js');
require('./index-6ce6e395.js');
require('date-fns/locale');
require('./index-da3b86cd.js');
require('./constantsSite.js');
require('./constantsRiskScreener.js');
require('react-phone-number-input');
require('./index-3b44943e.js');
require('slugify');
require('string-format');
require('./togetherApiPaths.js');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

const TogetherAuthConfigContext = React.createContext({});
const TogetherAuthConfigContextProvider = ({ children, defaultParams, }) => {
    return (React__default["default"].createElement(TogetherAuthConfigContext.Provider, { value: {
            defaultParams,
        } }, children));
};
const useTogetherAuthRequiredAction = (propsParams) => {
    const { triggerLogin } = store.useSSOV2Store();
    const { data: { userInfo, env: { togetherBasePath }, }, } = React.useContext(togetherComponentGlobalContext.TogetherComponentGlobalContext);
    const { defaultParams } = React.useContext(TogetherAuthConfigContext) || {};
    const loggedInRef = React.useRef(!!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id));
    loggedInRef.current = !!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id);
    const isFirstInteractRef = React.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) {
                miscCookieHelper.deleteCookie(constants.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 = togetherComponentGlobalContext.getUrlToSignUp(currentLocation, params, togetherBasePath);
            if (store.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),
    };
};

exports.TogetherAuthConfigContext = TogetherAuthConfigContext;
exports.TogetherAuthConfigContextProvider = TogetherAuthConfigContextProvider;
exports.useTogetherAuthRequiredAction = useTogetherAuthRequiredAction;
