"use strict";
/**
 * Copyright (c) 2022
 *
 * Embed ThoughtSpot search or a saved answer
 * @summary Search embed
 * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchEmbed = exports.HiddenActionItemByDefaultForSearchEmbed = exports.DataPanelCustomColumnGroupsAccordionState = void 0;
const types_1 = require("../types");
const utils_1 = require("../utils");
const ts_embed_1 = require("./ts-embed");
const errors_1 = require("../errors");
const base_1 = require("./base");
const auth_1 = require("../auth");
const embedConfig_1 = require("./embedConfig");
/**
 * Define the initial state os column custom group accordions
 * in data panel v2.
 */
var DataPanelCustomColumnGroupsAccordionState;
(function (DataPanelCustomColumnGroupsAccordionState) {
    /**
     * Expand all the accordion initially in data panel v2.
     */
    DataPanelCustomColumnGroupsAccordionState["EXPAND_ALL"] = "EXPAND_ALL";
    /**
     * Collapse all the accordions initially in data panel v2.
     */
    DataPanelCustomColumnGroupsAccordionState["COLLAPSE_ALL"] = "COLLAPSE_ALL";
    /**
     * Expand the first accordion and collapse the rest.
     */
    DataPanelCustomColumnGroupsAccordionState["EXPAND_FIRST"] = "EXPAND_FIRST";
})(DataPanelCustomColumnGroupsAccordionState = exports.DataPanelCustomColumnGroupsAccordionState || (exports.DataPanelCustomColumnGroupsAccordionState = {}));
exports.HiddenActionItemByDefaultForSearchEmbed = [
    types_1.Action.EditACopy,
    types_1.Action.SaveAsView,
    types_1.Action.UpdateTML,
    types_1.Action.EditTML,
    types_1.Action.AnswerDelete,
];
/**
 * Embed ThoughtSpot search
 * @group Embed components
 */
class SearchEmbed extends ts_embed_1.TsEmbed {
    constructor(domSelector, viewConfig) {
        viewConfig.embedComponentType = 'SearchEmbed';
        super(domSelector, viewConfig);
    }
    /**
     * Get the state of the data sources panel that the embedded
     * ThoughtSpot search will be initialized with.
     */
    getDataSourceMode() {
        let dataSourceMode = types_1.DataSourceVisualMode.Expanded;
        if (this.viewConfig.collapseDataSources === true
            || this.viewConfig.collapseDataPanel === true) {
            dataSourceMode = types_1.DataSourceVisualMode.Collapsed;
        }
        if (this.viewConfig.hideDataSources === true) {
            dataSourceMode = types_1.DataSourceVisualMode.Hidden;
        }
        return dataSourceMode;
    }
    getSearchInitData() {
        var _a;
        return {
            ...(this.viewConfig.excludeSearchTokenStringFromURL ? {
                searchOptions: {
                    searchTokenString: (_a = this.viewConfig.searchOptions) === null || _a === void 0 ? void 0 : _a.searchTokenString,
                },
            } : {}),
        };
    }
    async getAppInitData() {
        const defaultAppInitData = await super.getAppInitData();
        return { ...defaultAppInitData, ...this.getSearchInitData() };
    }
    getEmbedParams() {
        var _a;
        const { hideResults, enableSearchAssist, forceTable, searchOptions, runtimeFilters, dataSource, dataSources, excludeRuntimeFiltersfromURL, hideSearchBar, dataPanelV2 = false, useLastSelectedSources = false, runtimeParameters, collapseSearchBarInitially = false, enableCustomColumnGroups = false, isOnBeforeGetVizDataInterceptEnabled = false, 
        /* eslint-disable-next-line max-len */
        dataPanelCustomGroupsAccordionInitialState = DataPanelCustomColumnGroupsAccordionState.EXPAND_ALL, focusSearchBarOnRender = true, excludeRuntimeParametersfromURL, excludeSearchTokenStringFromURL, collapseSearchBar = true, } = this.viewConfig;
        const queryParams = this.getBaseQueryParams();
        queryParams[types_1.Param.HideActions] = [
            ...((_a = queryParams[types_1.Param.HideActions]) !== null && _a !== void 0 ? _a : []),
            ...exports.HiddenActionItemByDefaultForSearchEmbed,
        ];
        if (dataSources && dataSources.length) {
            queryParams[types_1.Param.DataSources] = JSON.stringify(dataSources);
        }
        if (dataSource) {
            queryParams[types_1.Param.DataSources] = `["${dataSource}"]`;
        }
        if (searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.searchTokenString) {
            if (!excludeSearchTokenStringFromURL) {
                queryParams[types_1.Param.searchTokenString] = encodeURIComponent(searchOptions.searchTokenString);
            }
            if (searchOptions.executeSearch) {
                queryParams[types_1.Param.executeSearch] = true;
            }
        }
        if (enableSearchAssist) {
            queryParams[types_1.Param.EnableSearchAssist] = true;
        }
        if (hideResults) {
            queryParams[types_1.Param.HideResult] = true;
        }
        if (forceTable) {
            queryParams[types_1.Param.ForceTable] = true;
        }
        if (hideSearchBar) {
            queryParams[types_1.Param.HideSearchBar] = true;
        }
        if (isOnBeforeGetVizDataInterceptEnabled) {
            /* eslint-disable-next-line max-len */
            queryParams[types_1.Param.IsOnBeforeGetVizDataInterceptEnabled] = isOnBeforeGetVizDataInterceptEnabled;
        }
        if (!focusSearchBarOnRender) {
            queryParams[types_1.Param.FocusSearchBarOnRender] = focusSearchBarOnRender;
        }
        queryParams[types_1.Param.DataPanelV2Enabled] = dataPanelV2;
        queryParams[types_1.Param.DataSourceMode] = this.getDataSourceMode();
        queryParams[types_1.Param.UseLastSelectedDataSource] = useLastSelectedSources;
        if (dataSource || dataSources) {
            queryParams[types_1.Param.UseLastSelectedDataSource] = false;
        }
        queryParams[types_1.Param.searchEmbed] = true;
        /* eslint-disable-next-line max-len */
        queryParams[types_1.Param.CollapseSearchBarInitially] = collapseSearchBarInitially || collapseSearchBar;
        queryParams[types_1.Param.EnableCustomColumnGroups] = enableCustomColumnGroups;
        if (dataPanelCustomGroupsAccordionInitialState
            === DataPanelCustomColumnGroupsAccordionState.COLLAPSE_ALL
            || dataPanelCustomGroupsAccordionInitialState
                === DataPanelCustomColumnGroupsAccordionState.EXPAND_FIRST) {
            /* eslint-disable-next-line max-len */
            queryParams[types_1.Param.DataPanelCustomGroupsAccordionInitialState] = dataPanelCustomGroupsAccordionInitialState;
        }
        else {
            /* eslint-disable-next-line max-len */
            queryParams[types_1.Param.DataPanelCustomGroupsAccordionInitialState] = DataPanelCustomColumnGroupsAccordionState.EXPAND_ALL;
        }
        let query = '';
        const queryParamsString = (0, utils_1.getQueryParamString)(queryParams, true);
        if (queryParamsString) {
            query = `?${queryParamsString}`;
        }
        const parameterQuery = (0, utils_1.getRuntimeParameters)(runtimeParameters || []);
        if (parameterQuery && !excludeRuntimeParametersfromURL)
            query += `&${parameterQuery}`;
        const filterQuery = (0, utils_1.getFilterQuery)(runtimeFilters || []);
        if (filterQuery && !excludeRuntimeFiltersfromURL) {
            query += `&${filterQuery}`;
        }
        return query;
    }
    /**
     * Construct the URL of the embedded ThoughtSpot search to be
     * loaded in the iframe
     * @param answerId The GUID of a saved answer
     * @param dataSources A list of data source GUIDs
     */
    getIFrameSrc() {
        const { answerId } = this.viewConfig;
        const answerPath = answerId ? `saved-answer/${answerId}` : 'answer';
        const tsPostHashParams = this.getThoughtSpotPostUrlParams();
        return `${this.getRootIframeSrc()}/embed/${answerPath}${tsPostHashParams}`;
    }
    /**
     * Render the embedded ThoughtSpot search
     */
    async render() {
        await super.render();
        const { answerId } = this.viewConfig;
        const src = this.getIFrameSrc();
        await this.renderIFrame(src);
        (0, base_1.getAuthPromise)().then(() => {
            if ((0, utils_1.checkReleaseVersionInBeta)((0, auth_1.getReleaseVersion)(), (0, embedConfig_1.getEmbedConfig)().suppressSearchEmbedBetaWarning
                || (0, embedConfig_1.getEmbedConfig)().suppressErrorAlerts)) {
                alert(errors_1.ERROR_MESSAGE.SEARCHEMBED_BETA_WRANING_MESSAGE);
            }
        });
        return this;
    }
}
exports.SearchEmbed = SearchEmbed;
//# sourceMappingURL=search.js.map