import { ParseError } from '../types/errors';
import { TimeFrames } from '../types/timeFrames';
const STANDARD_TIME_FRAMES = new Set(Object.values(TimeFrames));
const isStandardTimeFrame = (value) => STANDARD_TIME_FRAMES.has(value);
/** Standard time frames that are meaningless on a plain DATE dimension. */
const DATE_INVALID_TIME_FRAMES = new Set([
    TimeFrames.RAW,
    TimeFrames.MILLISECOND,
    TimeFrames.SECOND,
    TimeFrames.MINUTE,
    TimeFrames.HOUR,
]);
const validateOwner = (owner) => {
    if (typeof owner === 'string')
        return owner;
    if (owner !== undefined) {
        // eslint-disable-next-line no-console
        console.warn(`Invalid spotlight owner: expected string, got ${typeof owner}`);
    }
    return null;
};
/**
 * Get the spotlight configuration for a resource
 */
export const getSpotlightConfigurationForResource = ({ visibility, categories, filterBy, segmentBy, defaultSegment, defaultFilter, owner, }) => {
    if (visibility === undefined) {
        return {};
    }
    const validatedOwner = validateOwner(owner);
    return {
        spotlight: {
            visibility,
            categories,
            ...(filterBy ? { filterBy } : {}),
            ...(segmentBy ? { segmentBy } : {}),
            ...(defaultSegment ? { defaultSegment } : {}),
            ...(defaultFilter ? { defaultFilter } : {}),
            ...(validatedOwner ? { owner: validatedOwner } : {}),
        },
    };
};
/**
 * Get the categories from the resource and validate them against the project config
 * @param resourceType - The type of the resource
 * @param resourceName - The name of the resource
 * @param spotlightConfig - The spotlight config
 * @param resourceCategories - The categories from the resource
 * @returns The categories from the resource
 */
export const getCategoriesFromResource = (resourceType, resourceName, spotlightConfig, resourceCategories = []) => {
    // Get all valid category references from the global spotlight config
    const categoriesDefinedInProjectConfig = Object.keys(spotlightConfig?.categories || {}) || [];
    // Check if any metric categories aren't defined in the global config
    const invalidCategories = resourceCategories.filter((category) => !categoriesDefinedInProjectConfig.includes(category));
    if (invalidCategories.length > 0) {
        throw new ParseError(`Invalid spotlight categories found in ${resourceType} '${resourceName}': ${invalidCategories.join(', ')}. Categories must be defined in project config.`);
    }
    return resourceCategories;
};
const resolveAdditionalTimeIntervalList = (values, key, customGranularities) => (values ?? []).reduce((acc, raw) => {
    const upper = raw.toUpperCase();
    if (isStandardTimeFrame(upper)) {
        if (key === 'date' && DATE_INVALID_TIME_FRAMES.has(upper)) {
            // eslint-disable-next-line no-console
            console.warn(`Ignoring sub-day time interval "${raw}" in defaults.additional_time_intervals.date — not valid for DATE dimensions.`);
            return acc;
        }
        return [...acc, upper];
    }
    if (customGranularities?.[raw]) {
        return [...acc, raw];
    }
    // eslint-disable-next-line no-console
    console.warn(`Ignoring unknown time interval "${raw}" in defaults.additional_time_intervals.${key} — not a standard granularity or a defined custom_granularity.`);
    return acc;
}, []);
/**
 * Validate `defaults.additional_time_intervals` once: keep standard grains
 * (uppercased) and defined custom-granularity keys; drop sub-day grains under
 * `date` and unknown names (each with a single console.warn).
 */
export const resolveAdditionalTimeIntervals = (additionalTimeIntervals, customGranularities) => ({
    date: resolveAdditionalTimeIntervalList(additionalTimeIntervals?.date, 'date', customGranularities),
    timestamp: resolveAdditionalTimeIntervalList(additionalTimeIntervals?.timestamp, 'timestamp', customGranularities),
});
/**
 * Validate `defaults.granularity_labels` once: keep entries whose key is a
 * standard granularity (keyed by uppercased TimeFrames, label verbatim); drop
 * unknown keys with a single console.warn.
 */
export const resolveGranularityLabels = (granularityLabels) => Object.entries(granularityLabels ?? {}).reduce((acc, [rawKey, label]) => {
    const upper = rawKey.toUpperCase();
    if (isStandardTimeFrame(upper)) {
        return { ...acc, [upper]: label };
    }
    // eslint-disable-next-line no-console
    console.warn(`Ignoring unknown granularity "${rawKey}" in defaults.granularity_labels — not a standard granularity.`);
    return acc;
}, {});
//# sourceMappingURL=lightdashProjectConfig.js.map