"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSelectedTabs = exports.isTileInSelectedTabs = exports.convertChartSourceTypeToDashboardTileType = void 0;
const tslib_1 = require("tslib");
const content_1 = require("../types/content");
const dashboard_1 = require("../types/dashboard");
const errors_1 = require("../types/errors");
const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
const convertChartSourceTypeToDashboardTileType = (sourceType) => {
    switch (sourceType) {
        case content_1.ChartSourceType.DBT_EXPLORE:
            return dashboard_1.DashboardTileTypes.SAVED_CHART;
        case content_1.ChartSourceType.SQL:
            return dashboard_1.DashboardTileTypes.SQL_CHART;
        default:
            return (0, assertUnreachable_1.default)(sourceType, `Unknown source type: ${sourceType}`);
    }
};
exports.convertChartSourceTypeToDashboardTileType = convertChartSourceTypeToDashboardTileType;
/**
 * Whether a tile should be rendered for the given tab selection.
 *
 * Includes orphan (legacy) tiles — tiles whose `tabUuid` is null/undefined —
 * regardless of `selectedTabs`, mirroring the frontend behaviour that surfaces
 * legacy tiles on the first tab. When `selectedTabs` is null, no filtering is
 * applied. See PROD-2505.
 */
const isTileInSelectedTabs = (tile, selectedTabs) => !selectedTabs || !tile.tabUuid || selectedTabs.includes(tile.tabUuid);
exports.isTileInSelectedTabs = isTileInSelectedTabs;
/**
 * Validates that selected tabs exist in the dashboard tiles.
 * If selectedTabs is provided and not empty, ensures at least one selected tab exists in dashboard tabs.
 * @param selectedTabs - Array of selected tab UUIDs or null
 * @param dashboardTiles - Array of dashboard tiles
 * @throws ParameterError if none of the selected tabs exist in the dashboard
 */
const validateSelectedTabs = (selectedTabs, dashboardTiles) => {
    // If selectedTabs is provided and not empty, validate that at least one exists in dashboard tabs
    if (selectedTabs && selectedTabs.length > 0) {
        const availableTabs = dashboardTiles
            .map((tile) => tile.tabUuid)
            .filter((tabUuid) => !!tabUuid)
            .filter((tabUuid, index, self) => self.indexOf(tabUuid) === index);
        const validSelectedTabs = selectedTabs.filter((tabUuid) => availableTabs.includes(tabUuid));
        if (validSelectedTabs.length === 0) {
            throw new errors_1.ParameterError(`None of the selected tabs exist in the dashboard`);
        }
    }
};
exports.validateSelectedTabs = validateSelectedTabs;
//# sourceMappingURL=dashboard.js.map