"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterExploreByTags = filterExploreByTags;
const tslib_1 = require("tslib");
const concat_1 = tslib_1.__importDefault(require("lodash/concat"));
const flatMap_1 = tslib_1.__importDefault(require("lodash/flatMap"));
const intersection_1 = tslib_1.__importDefault(require("lodash/intersection"));
const mapValues_1 = tslib_1.__importDefault(require("lodash/mapValues"));
const pickBy_1 = tslib_1.__importDefault(require("lodash/pickBy"));
function matcher(tags, availableTags) {
    return (0, intersection_1.default)(tags, availableTags).length > 0;
}
function hasFieldMatchingTags(table, availableTags) {
    return matcher((0, concat_1.default)((0, flatMap_1.default)(table.metrics, (m) => m.tags ?? []), (0, flatMap_1.default)(table.dimensions, (d) => d.tags ?? [])), availableTags);
}
function hasTableAnyFieldTags(table) {
    const allFields = (0, concat_1.default)(Object.values(table.metrics), Object.values(table.dimensions));
    return allFields.some((field) => field.tags !== undefined);
}
function hasMatchingTags(explore, baseTable, availableTags) {
    if (matcher(explore.tags, availableTags)) {
        return true;
    }
    if (hasFieldMatchingTags(baseTable, availableTags)) {
        return true;
    }
    return false;
}
/**
 * @description
 * Filters an explore based on available tags configured in AI settings.
 *
 * ## Base Table Requirement
 * An explore is only visible if the base table has matching tags at either:
 * - Explore level (explore.tags matches availableTags), OR
 * - Field level (at least one base table field has matching tags)
 *
 * ## Per-Table Filtering Modes
 * Each table in the explore is filtered independently:
 *
 * **Field-Level Mode** (if table has ANY field with tags defined):
 * - Expose only fields where field.tags matches availableTags
 * - Empty tags arrays (tags: []) activate this mode but match nothing
 *
 * **Explore-Level Mode** (if table has NO fields with tags defined):
 * - If explore.tags matches → expose ALL fields in this table
 * - If explore.tags doesn't match → expose NO fields (empty dimensions/metrics)
 *
 * ## Tagging Scenarios
 *
 * | Scenario                                        | Result                                    |
 * |-------------------------------------------------|-------------------------------------------|
 * | No tags configured (availableTags = null/[])    | Everything is visible                     |
 * | Explore tagged, no field tags                   | All tables/fields visible (explore-level) |
 * | Explore tagged, some fields tagged              | Per-table: field-level or explore-level   |
 * | No explore tag, base table fields tagged        | Only matching fields visible              |
 * | No explore tag, only joined table fields tagged | Explore hidden (base table requirement)   |
 * | No matching tags anywhere                       | Explore hidden                            |
 */
function filterExploreByTags({ explore, availableTags, }) {
    if (!availableTags || availableTags.length === 0) {
        return explore;
    }
    const baseTable = explore.tables[explore.baseTable];
    if (!baseTable) {
        throw new Error(`Base table not found`);
    }
    if (!hasMatchingTags(explore, baseTable, availableTags)) {
        return undefined;
    }
    // Apply per-table filtering logic
    const filteredExplore = {
        ...explore,
        tables: (0, mapValues_1.default)(explore.tables, (table) => {
            if (hasTableAnyFieldTags(table)) {
                // Field-level tagging mode: expose only fields with matching tags
                return {
                    ...table,
                    dimensions: (0, pickBy_1.default)(table.dimensions, (d) => matcher(d.tags ?? [], availableTags)),
                    metrics: (0, pickBy_1.default)(table.metrics, (m) => matcher(m.tags ?? [], availableTags)),
                };
            }
            // Explore-level tagging mode
            if (matcher(explore.tags, availableTags)) {
                // Explore tagged: expose all fields in this table
                return table;
            }
            // Explore not tagged: expose no fields
            return {
                ...table,
                dimensions: {},
                metrics: {},
            };
        }),
    };
    return filteredExplore;
}
//# sourceMappingURL=filterExploreByTags.js.map