"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLiveboardWithAnswers = void 0;
const tokenizedFetch_1 = require("../../tokenizedFetch");
const embedConfig_1 = require("../../embed/embedConfig");
const base_1 = require("../../embed/base");
/**
 * Create a liveboard with the given answers
 * @param answers AnswerService[]
 * @param name string
 * @returns result Promise
 * @version SDK: 1.33.1 | ThoughtSpot: *
 * @example
 * ```js
 *   const lb = createLiveboardWithAnswers([
 *      new AnswerService(session, null, 'tshost'),
 *   ], "my-liveboard");
 * ```
 */
const createLiveboardWithAnswers = async (answers, name) => {
    var _a;
    const { thoughtSpotHost, authType } = (0, embedConfig_1.getEmbedConfig)();
    const resp = await (0, tokenizedFetch_1.tokenizedFetch)(`${thoughtSpotHost}/api/rest/2.0/metadata/search`, {
        method: 'POST',
        credentials: 'include',
        headers: {
            'content-type': 'application/json',
        },
        body: JSON.stringify({
            metadata: [{
                    type: 'LIVEBOARD',
                    identifier: name,
                }],
        }),
    });
    const lbList = await resp.json();
    const liveboardId = (_a = lbList[0]) === null || _a === void 0 ? void 0 : _a.metadata_id;
    const answerTMLs = (await Promise.all(answers.map((a) => a.getTML())))
        .filter((tml) => tml.answer.search_query);
    const lbTml = {
        guid: liveboardId,
        liveboard: {
            name,
            visualizations: answerTMLs.map((tml, idx) => ({
                id: `Viz_${idx}`,
                answer: tml.answer,
            })),
            layout: {
                tiles: answerTMLs.map((tml, idx) => ({
                    visualization_id: `Viz_${idx}`,
                    size: 'MEDIUM_SMALL',
                })),
            },
        },
    };
    const result = await (0, base_1.executeTML)({
        metadata_tmls: [JSON.stringify(lbTml)],
        import_policy: 'ALL_OR_NONE',
    });
    return result;
};
exports.createLiveboardWithAnswers = createLiveboardWithAnswers;
//# sourceMappingURL=liveboardService.js.map