"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Conversation = void 0;
const tslib_1 = require("tslib");
const graphql_request_1 = require("../graphql-request");
const queries = tslib_1.__importStar(require("./conversation-queries"));
class Conversation {
    constructor(thoughtSpotHost, worksheetId) {
        this.thoughtSpotHost = thoughtSpotHost;
        this.worksheetId = worksheetId;
        this.inProgress = null;
        this.inProgress = this.init();
    }
    async init() {
        const { convId } = await this.createConversation();
        this.conversationId = convId;
    }
    createConversation() {
        return this.executeQuery(queries.createConversation, {
            params: {
                initialCtx: {
                    tsWorksheetCtx: {
                        worksheet: {
                            worksheetId: this.worksheetId,
                        },
                    },
                    type: 'TS_WORKSHEET',
                },
                userInfo: {
                    tenantUrl: `${this.thoughtSpotHost}/prism`,
                },
            },
        });
    }
    async sendMessage(userMessage) {
        await this.inProgress;
        try {
            const { responses } = await this.executeQuery(queries.sendMessage, {
                params: {
                    convId: this.conversationId,
                    headers: [],
                    msg: {
                        data: {
                            userCmdData: {
                                cmdText: userMessage,
                                nlsData: {
                                    worksheetId: this.worksheetId,
                                    questionType: 'ANSWER_SPEC_GENERATION',
                                },
                            },
                        },
                        msgId: crypto.randomUUID(),
                        type: 'USER_COMMAND',
                    },
                },
            });
            const data = responses[0].data;
            return {
                data: data.asstRespData.nlsAnsData.sageQuerySuggestions[0],
                error: null,
            };
        }
        catch (error) {
            return { error };
        }
    }
    async executeQuery(query, variables) {
        return (0, graphql_request_1.graphqlQuery)({
            query,
            variables,
            thoughtSpotHost: this.thoughtSpotHost,
            isCompositeQuery: false,
        });
    }
}
exports.Conversation = Conversation;
//# sourceMappingURL=conversation-service.js.map