"use strict";
/**
 * Copyright (c) 2023
 *
 * Embed ThoughtSpot Sage
 * @summary TS Sage embed
 * @author Mourya Balabhadra <mourya.balabhadra@thoughtspot.com>
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SageEmbed = void 0;
const types_1 = require("../types");
const utils_1 = require("../utils");
const ts_embed_1 = require("./ts-embed");
/**
 * Embed ThoughtSpot LLM and GPT-based Natural Language Search component.
 * @version: SDK: 1.23.0 | ThoughtSpot: 9.4.0.cl, 9.5.1-sw
 * @group Embed components
 */
class SageEmbed extends ts_embed_1.V1Embed {
    // eslint-disable-next-line no-useless-constructor
    constructor(domSelector, viewConfig) {
        viewConfig.embedComponentType = 'SageEmbed';
        super(domSelector, viewConfig);
    }
    /**
     * Constructs a map of parameters to be passed on to the
     * embedded Eureka or Sage search page.
     * @returns {string} query string
     */
    getEmbedParams() {
        const { disableWorksheetChange, hideWorksheetSelector, showObjectSuggestions, dataPanelV2, hideSampleQuestions, isProductTour, hideSageAnswerHeader, hideAutocompleteSuggestions, } = this.viewConfig;
        const params = this.getBaseQueryParams();
        params[types_1.Param.DataPanelV2Enabled] = !!dataPanelV2;
        params[types_1.Param.IsSageEmbed] = true;
        params[types_1.Param.DisableWorksheetChange] = !!disableWorksheetChange;
        params[types_1.Param.HideWorksheetSelector] = !!hideWorksheetSelector;
        params[types_1.Param.HideEurekaSuggestions] = !!hideAutocompleteSuggestions;
        if (showObjectSuggestions) {
            params[types_1.Param.HideEurekaSuggestions] = !showObjectSuggestions;
            // support backwards compatibility
        }
        params[types_1.Param.HideSampleQuestions] = !!hideSampleQuestions;
        params[types_1.Param.IsProductTour] = !!isProductTour;
        params[types_1.Param.HideSageAnswerHeader] = !!hideSageAnswerHeader;
        return (0, utils_1.getQueryParamString)(params, true);
    }
    /**
     * Construct the URL of the embedded ThoughtSpot sage to be
     * loaded in the iframe
     * @returns {string} iframe url
     */
    getIFrameSrc() {
        const path = 'eureka';
        const postHashObj = {};
        const tsPostHashParams = this.getThoughtSpotPostUrlParams();
        const { dataSource, searchOptions, } = this.viewConfig;
        if (dataSource)
            postHashObj[types_1.Param.WorksheetId] = dataSource;
        if ((searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.searchQuery) && searchOptions.executeSearch) {
            postHashObj[types_1.Param.executeSearch] = true;
        }
        let sagePostHashParams = new URLSearchParams(postHashObj).toString();
        if (sagePostHashParams)
            sagePostHashParams = `${tsPostHashParams ? '&' : '?'}${sagePostHashParams}`;
        if (searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.searchQuery)
            sagePostHashParams += `${sagePostHashParams ? '&' : '?'}${[types_1.Param.Query]}=${encodeURIComponent(searchOptions.searchQuery)}`;
        // use encodeURIComponent for query instead of URLSearchParams
        // as it adds + instead of %20 for spaces
        return `${this.getRootIframeSrc()}/embed/${path}${tsPostHashParams}${sagePostHashParams}`;
    }
    /**
     * Render the embedded ThoughtSpot Sage
     * @returns {SageEmbed} Eureka/Sage embed
     */
    async render() {
        await super.render();
        const src = this.getIFrameSrc();
        await this.renderV1Embed(src);
        return this;
    }
}
exports.SageEmbed = SageEmbed;
//# sourceMappingURL=sage.js.map