import { z } from 'zod';
import assertUnreachable from '../../utils/assertUnreachable';
export var FilterInteractivityValues;
(function (FilterInteractivityValues) {
    FilterInteractivityValues["some"] = "some";
    FilterInteractivityValues["all"] = "all";
    FilterInteractivityValues["none"] = "none";
})(FilterInteractivityValues || (FilterInteractivityValues = {}));
export const FilterInteractivityValuesSchema = z.enum([
    FilterInteractivityValues.some,
    FilterInteractivityValues.all,
    FilterInteractivityValues.none,
]);
export const DashboardFilterInteractivityOptionsSchema = z.object({
    enabled: z.union([z.boolean(), FilterInteractivityValuesSchema]),
    // Nullish because we have python clients that serialize None to null
    allowedFilters: z.array(z.string()).nullish(),
    hidden: z.boolean().optional(),
    // Lets embed viewers add their own temporary filters via the filter bar.
    // Only effective when filter interactivity is enabled.
    canAddFilters: z.boolean().optional(),
});
export const ParameterInteractivityOptionsSchema = z.object({
    enabled: z.boolean(),
});
export const InteractivityOptionsSchema = z.object({
    dashboardFiltersInteractivity: DashboardFilterInteractivityOptionsSchema.optional(),
    parameterInteractivity: ParameterInteractivityOptionsSchema.optional(),
    canExportCsv: z.boolean().optional(),
    canExportImages: z.boolean().optional(),
    canExportPagePdf: z.boolean().optional(),
    canDateZoom: z.boolean().optional(),
    canExplore: z.boolean().optional(),
    canViewUnderlyingData: 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: z.boolean().optional(),
    // Pins tabs and the filter bar to the top while scrolling. Off by default.
    stickyHeader: z.boolean().optional(),
});
export const ChartInteractivityOptionsSchema = z.object({
    scopes: z.array(z.string()).optional(),
    canExportCsv: z.boolean().optional(),
    canExportImages: z.boolean().optional(),
    canViewUnderlyingData: z.boolean().optional(),
});
export const EmbedWriteActionsSchema = z
    .object({
    serviceAccountUserUuid: z.string().uuid().optional(),
    userUuid: z.string().uuid().optional(),
    spaceUuid: z.string().uuid(),
})
    .refine(({ serviceAccountUserUuid, userUuid }) => serviceAccountUserUuid !== undefined || userUuid !== undefined, {
    message: 'Either serviceAccountUserUuid or userUuid is required for write actions',
});
export const EmbedJwtSchema = z
    .object({
    userAttributes: z.record(z.unknown()).optional(),
    user: z
        .object({
        externalId: z.string().nullish(),
        email: z.string().nullish(),
    })
        .optional(),
    content: z.union([
        z
            .object({
            type: z.literal('dashboard'),
            projectUuid: z.string().optional(),
            dashboardUuid: z.string(),
            isPreview: z.boolean().optional(),
        })
            .merge(InteractivityOptionsSchema),
        z
            .object({
            type: z.literal('dashboard'),
            projectUuid: z.string().optional(),
            dashboardSlug: z.string(),
            isPreview: z.boolean().optional(),
        })
            .merge(InteractivityOptionsSchema),
        z
            .object({
            type: z.literal('chart'),
            projectUuid: z.string().optional(),
            contentId: z.string(),
            isPreview: z.boolean().optional(),
        })
            .merge(ChartInteractivityOptionsSchema),
        z.object({
            type: z.literal('dataApp'),
            projectUuid: z.string().optional(),
            appUuid: z.string(),
            isPreview: z.boolean().optional(),
        }),
        z.object({
            type: z.literal('aiAgent'),
            projectUuid: z.string().optional(),
            agentUuid: z.string(),
            canExplore: z.boolean().optional(),
        }),
        z.object({
            type: z.literal('metricsCatalog'),
            projectUuid: z.string().optional(),
            canExplore: z.boolean().optional(),
        }),
        z.object({
            type: z.literal('apiAccess'),
            projectUuid: z.string().optional(),
            serviceAccountUserUuid: z.string().uuid(),
        }),
    ]),
    writeActions: EmbedWriteActionsSchema.optional(),
    iat: z.number().optional(),
    exp: z.number(),
})
    .describe('Configuration file for generating a CSV file from a query with metrics and dimensions');
export function isDashboardUuidContent(content) {
    return content.type === 'dashboard' && 'dashboardUuid' in content;
}
export function isDashboardSlugContent(content) {
    return content.type === 'dashboard' && 'dashboardSlug' in content;
}
export function isChartContent(content) {
    return content.type === 'chart';
}
export function isAiAgentContent(content) {
    return content.type === 'aiAgent';
}
export function isDashboardContent(content) {
    return content.type === 'dashboard';
}
// allows for backwards compatibility with old filter interactivity boolean values
export function getFilterInteractivityValue(enabled) {
    if (typeof enabled === 'boolean') {
        return enabled
            ? FilterInteractivityValues.all
            : FilterInteractivityValues.none;
    }
    return enabled;
}
export 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 assertUnreachable(filterInteractivityValue, `Unknown FilterInteractivityValue ${filterInteractivityValue}`);
    }
}
export function canAddDashboardFiltersInEmbed(filterInteractivityOptions) {
    return (!!isFilterInteractivityEnabled(filterInteractivityOptions) &&
        filterInteractivityOptions?.canAddFilters === true);
}
export function isParameterInteractivityEnabled(parameterInteractivityOptions) {
    return parameterInteractivityOptions?.enabled ?? false;
}
//# sourceMappingURL=index.js.map