"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbedJwtSchema = exports.EmbedWriteActionsSchema = exports.ChartInteractivityOptionsSchema = exports.InteractivityOptionsSchema = exports.ParameterInteractivityOptionsSchema = exports.DashboardFilterInteractivityOptionsSchema = exports.FilterInteractivityValuesSchema = exports.FilterInteractivityValues = void 0;
exports.isDashboardUuidContent = isDashboardUuidContent;
exports.isDashboardSlugContent = isDashboardSlugContent;
exports.isChartContent = isChartContent;
exports.isAiAgentContent = isAiAgentContent;
exports.isDashboardContent = isDashboardContent;
exports.getFilterInteractivityValue = getFilterInteractivityValue;
exports.isFilterInteractivityEnabled = isFilterInteractivityEnabled;
exports.canAddDashboardFiltersInEmbed = canAddDashboardFiltersInEmbed;
exports.isParameterInteractivityEnabled = isParameterInteractivityEnabled;
const tslib_1 = require("tslib");
const zod_1 = require("zod");
const assertUnreachable_1 = tslib_1.__importDefault(require("../../utils/assertUnreachable"));
var FilterInteractivityValues;
(function (FilterInteractivityValues) {
    FilterInteractivityValues["some"] = "some";
    FilterInteractivityValues["all"] = "all";
    FilterInteractivityValues["none"] = "none";
})(FilterInteractivityValues || (exports.FilterInteractivityValues = FilterInteractivityValues = {}));
exports.FilterInteractivityValuesSchema = zod_1.z.enum([
    FilterInteractivityValues.some,
    FilterInteractivityValues.all,
    FilterInteractivityValues.none,
]);
exports.DashboardFilterInteractivityOptionsSchema = zod_1.z.object({
    enabled: zod_1.z.union([zod_1.z.boolean(), exports.FilterInteractivityValuesSchema]),
    // Nullish because we have python clients that serialize None to null
    allowedFilters: zod_1.z.array(zod_1.z.string()).nullish(),
    hidden: zod_1.z.boolean().optional(),
    // Lets embed viewers add their own temporary filters via the filter bar.
    // Only effective when filter interactivity is enabled.
    canAddFilters: zod_1.z.boolean().optional(),
});
exports.ParameterInteractivityOptionsSchema = zod_1.z.object({
    enabled: zod_1.z.boolean(),
});
exports.InteractivityOptionsSchema = zod_1.z.object({
    dashboardFiltersInteractivity: exports.DashboardFilterInteractivityOptionsSchema.optional(),
    parameterInteractivity: exports.ParameterInteractivityOptionsSchema.optional(),
    canExportCsv: zod_1.z.boolean().optional(),
    canExportImages: zod_1.z.boolean().optional(),
    canExportPagePdf: zod_1.z.boolean().optional(),
    canDateZoom: zod_1.z.boolean().optional(),
    canExplore: zod_1.z.boolean().optional(),
    canViewUnderlyingData: zod_1.z.boolean().optional(),
    // Authorizes embed users to view data app tiles on the dashboard.
    // Off by default — without it, data app tiles render as a placeholder.
    // Setting it grants the embed JWT the broader CASL abilities a data app
    // needs (view:DataApp + view:Explore project-wide) so it can mint a
    // preview token and run its arbitrary metric queries. Mirrors the
    // existing canExplore opt-in: an explicit decision to widen the embed's
    // surface beyond pre-built chart queries.
    canViewDataApps: zod_1.z.boolean().optional(),
    // Pins tabs and the filter bar to the top while scrolling. Off by default.
    stickyHeader: zod_1.z.boolean().optional(),
});
exports.ChartInteractivityOptionsSchema = zod_1.z.object({
    scopes: zod_1.z.array(zod_1.z.string()).optional(),
    canExportCsv: zod_1.z.boolean().optional(),
    canExportImages: zod_1.z.boolean().optional(),
    canViewUnderlyingData: zod_1.z.boolean().optional(),
});
exports.EmbedWriteActionsSchema = zod_1.z
    .object({
    serviceAccountUserUuid: zod_1.z.string().uuid().optional(),
    userUuid: zod_1.z.string().uuid().optional(),
    spaceUuid: zod_1.z.string().uuid(),
})
    .refine(({ serviceAccountUserUuid, userUuid }) => serviceAccountUserUuid !== undefined || userUuid !== undefined, {
    message: 'Either serviceAccountUserUuid or userUuid is required for write actions',
});
exports.EmbedJwtSchema = zod_1.z
    .object({
    userAttributes: zod_1.z.record(zod_1.z.unknown()).optional(),
    user: zod_1.z
        .object({
        externalId: zod_1.z.string().nullish(),
        email: zod_1.z.string().nullish(),
    })
        .optional(),
    content: zod_1.z.union([
        zod_1.z
            .object({
            type: zod_1.z.literal('dashboard'),
            projectUuid: zod_1.z.string().optional(),
            dashboardUuid: zod_1.z.string(),
            isPreview: zod_1.z.boolean().optional(),
        })
            .merge(exports.InteractivityOptionsSchema),
        zod_1.z
            .object({
            type: zod_1.z.literal('dashboard'),
            projectUuid: zod_1.z.string().optional(),
            dashboardSlug: zod_1.z.string(),
            isPreview: zod_1.z.boolean().optional(),
        })
            .merge(exports.InteractivityOptionsSchema),
        zod_1.z
            .object({
            type: zod_1.z.literal('chart'),
            projectUuid: zod_1.z.string().optional(),
            contentId: zod_1.z.string(),
            isPreview: zod_1.z.boolean().optional(),
        })
            .merge(exports.ChartInteractivityOptionsSchema),
        zod_1.z.object({
            type: zod_1.z.literal('dataApp'),
            projectUuid: zod_1.z.string().optional(),
            appUuid: zod_1.z.string(),
            isPreview: zod_1.z.boolean().optional(),
        }),
        zod_1.z.object({
            type: zod_1.z.literal('aiAgent'),
            projectUuid: zod_1.z.string().optional(),
            agentUuid: zod_1.z.string(),
            canExplore: zod_1.z.boolean().optional(),
        }),
        zod_1.z.object({
            type: zod_1.z.literal('metricsCatalog'),
            projectUuid: zod_1.z.string().optional(),
            canExplore: zod_1.z.boolean().optional(),
        }),
        zod_1.z.object({
            type: zod_1.z.literal('apiAccess'),
            projectUuid: zod_1.z.string().optional(),
            serviceAccountUserUuid: zod_1.z.string().uuid(),
        }),
    ]),
    writeActions: exports.EmbedWriteActionsSchema.optional(),
    iat: zod_1.z.number().optional(),
    exp: zod_1.z.number(),
})
    .describe('Configuration file for generating a CSV file from a query with metrics and dimensions');
function isDashboardUuidContent(content) {
    return content.type === 'dashboard' && 'dashboardUuid' in content;
}
function isDashboardSlugContent(content) {
    return content.type === 'dashboard' && 'dashboardSlug' in content;
}
function isChartContent(content) {
    return content.type === 'chart';
}
function isAiAgentContent(content) {
    return content.type === 'aiAgent';
}
function isDashboardContent(content) {
    return content.type === 'dashboard';
}
// allows for backwards compatibility with old filter interactivity boolean values
function getFilterInteractivityValue(enabled) {
    if (typeof enabled === 'boolean') {
        return enabled
            ? FilterInteractivityValues.all
            : FilterInteractivityValues.none;
    }
    return enabled;
}
function isFilterInteractivityEnabled(filterInteractivityOptions) {
    if (!filterInteractivityOptions)
        return false;
    const filterInteractivityValue = getFilterInteractivityValue(filterInteractivityOptions.enabled);
    switch (filterInteractivityValue) {
        case FilterInteractivityValues.some:
            return (filterInteractivityOptions.allowedFilters &&
                filterInteractivityOptions.allowedFilters.length > 0);
        case FilterInteractivityValues.all:
            return true;
        case FilterInteractivityValues.none:
            return false;
        default:
            return (0, assertUnreachable_1.default)(filterInteractivityValue, `Unknown FilterInteractivityValue ${filterInteractivityValue}`);
    }
}
function canAddDashboardFiltersInEmbed(filterInteractivityOptions) {
    return (!!isFilterInteractivityEnabled(filterInteractivityOptions) &&
        filterInteractivityOptions?.canAddFilters === true);
}
function isParameterInteractivityEnabled(parameterInteractivityOptions) {
    return parameterInteractivityOptions?.enabled ?? false;
}
//# sourceMappingURL=index.js.map