import { getEmbedConfig } from '../embed/embedConfig';
import { disableAutoLogin, notifyAuthFailure, notifyAuthSuccess, notifyLogout, } from '../embed/base';
import { AuthFailureType } from '../auth';
import { AuthType, EmbedEvent } from '../types';
import { AnswerService } from './graphql/answerService/answerService';
import { resetCachedAuthToken } from '../authToken';
import { ERROR_MESSAGE } from '../errors';
/**
 *
 * @param e
 * @param thoughtSpotHost
 */
export function processCustomAction(e, thoughtSpotHost) {
    const { session, embedAnswerData, contextMenuPoints } = e.data;
    const answerService = new AnswerService(session, embedAnswerData || {}, thoughtSpotHost, contextMenuPoints === null || contextMenuPoints === void 0 ? void 0 : contextMenuPoints.selectedPoints);
    return {
        ...e,
        answerService,
    };
}
/**
 * Responds to AuthInit sent from host signifying successful authentication in host.
 * @param e
 * @returns {any}
 */
function processAuthInit(e) {
    var _a, _b;
    notifyAuthSuccess();
    // Expose only allowed details (eg: userGUID) back to SDK users.
    return {
        ...e,
        data: {
            userGUID: ((_a = e.data) === null || _a === void 0 ? void 0 : _a.userGUID) || ((_b = e.payload) === null || _b === void 0 ? void 0 : _b.userGUID),
        },
    };
}
/**
 *
 * @param e
 * @param containerEl
 */
function processNoCookieAccess(e, containerEl) {
    const { loginFailedMessage, suppressNoCookieAccessAlert, ignoreNoCookieAccess, suppressErrorAlerts, } = getEmbedConfig();
    if (!ignoreNoCookieAccess) {
        if (!suppressNoCookieAccessAlert && !suppressErrorAlerts) {
            // eslint-disable-next-line no-alert
            alert(ERROR_MESSAGE.THIRD_PARTY_COOKIE_BLOCKED_ALERT);
        }
        // eslint-disable-next-line no-param-reassign
        containerEl.innerHTML = loginFailedMessage;
    }
    notifyAuthFailure(AuthFailureType.NO_COOKIE_ACCESS);
    return e;
}
/**
 *
 * @param e
 * @param containerEl
 */
export function processAuthFailure(e, containerEl) {
    var _a;
    const { loginFailedMessage, authType, disableLoginFailurePage, autoLogin, } = getEmbedConfig();
    const isEmbeddedSSO = authType === AuthType.EmbeddedSSO;
    const isTrustedAuth = authType === AuthType.TrustedAuthToken || authType === AuthType.TrustedAuthTokenCookieless;
    const isEmbeddedSSOInfoFailure = isEmbeddedSSO && ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.type) === AuthFailureType.UNAUTHENTICATED_FAILURE;
    if (autoLogin && isTrustedAuth) {
        // eslint-disable-next-line no-param-reassign
        containerEl.innerHTML = loginFailedMessage;
        notifyAuthFailure(AuthFailureType.IDLE_SESSION_TIMEOUT);
    }
    else if (authType !== AuthType.None && !disableLoginFailurePage && !isEmbeddedSSOInfoFailure) {
        // eslint-disable-next-line no-param-reassign
        containerEl.innerHTML = loginFailedMessage;
        notifyAuthFailure(AuthFailureType.OTHER);
    }
    resetCachedAuthToken();
    return e;
}
/**
 *
 * @param e
 * @param containerEl
 */
function processAuthLogout(e, containerEl) {
    const { loginFailedMessage } = getEmbedConfig();
    // eslint-disable-next-line no-param-reassign
    containerEl.innerHTML = loginFailedMessage;
    resetCachedAuthToken();
    disableAutoLogin();
    notifyLogout();
    return e;
}
/**
 *
 * @param type
 * @param e
 * @param thoughtSpotHost
 * @param containerEl
 */
export function processEventData(type, e, thoughtSpotHost, containerEl) {
    switch (type) {
        case EmbedEvent.CustomAction:
            return processCustomAction(e, thoughtSpotHost);
        case EmbedEvent.AuthInit:
            return processAuthInit(e);
        case EmbedEvent.NoCookieAccess:
            return processNoCookieAccess(e, containerEl);
        case EmbedEvent.AuthFailure:
            return processAuthFailure(e, containerEl);
        case EmbedEvent.AuthLogout:
            return processAuthLogout(e, containerEl);
        default:
    }
    return e;
}
//# sourceMappingURL=processData.js.map