const MAX_USER_ATTRIBUTE_QUERY_TAGS = 20;
const MAX_QUERY_TAG_KEY_LENGTH = 60;
const MAX_QUERY_TAG_VALUE_LENGTH = 60;
const USER_ATTRIBUTE_QUERY_TAG_PREFIX = 'user_attribute_';
const sanitizeQueryTagString = (value, fallback, maxLength) => value
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9_-]/g, '_')
    .substring(0, maxLength) || fallback;
export const sanitizeQueryTagKey = (key) => sanitizeQueryTagString(key, 'empty_key', MAX_QUERY_TAG_KEY_LENGTH);
export const sanitizeQueryTagValue = (value) => sanitizeQueryTagString(value, 'empty_value', MAX_QUERY_TAG_VALUE_LENGTH);
export const getUserAttributeQueryTags = (userAttributes) => Object.fromEntries(Object.entries(userAttributes).reduce((acc, [name, values]) => {
    if (acc.length >= MAX_USER_ATTRIBUTE_QUERY_TAGS) {
        return acc;
    }
    const tagName = sanitizeQueryTagKey(`${USER_ATTRIBUTE_QUERY_TAG_PREFIX}${name}`);
    const value = values
        .map((attribute) => attribute.trim())
        .filter(Boolean)
        .join(',')
        .trim();
    if (value) {
        acc.push([
            tagName,
            sanitizeQueryTagValue(value),
        ]);
    }
    return acc;
}, []));
/**
 * Sparse sidecar of timestamp domains, keyed like the catalog itself. It
 * lives on the catalog under a reserved key next to the database keys, so it
 * survives the `cached_warehouse` JSON round-trip unchanged and is inert to
 * readers that only look up their own database names.
 */
export const WAREHOUSE_TIMESTAMP_DOMAINS_KEY = '__lightdashTimestampDomains';
export const getCatalogTimestampDomain = (catalog, database, schema, table, column) => catalog[WAREHOUSE_TIMESTAMP_DOMAINS_KEY]?.[database]?.[schema]?.[table]?.[column];
export const setCatalogTimestampDomain = (catalog, database, schema, table, column, timestampDomain) => {
    if (timestampDomain === undefined)
        return;
    const catalogWithDomains = catalog;
    const domains = catalogWithDomains[WAREHOUSE_TIMESTAMP_DOMAINS_KEY] ?? {};
    catalogWithDomains[WAREHOUSE_TIMESTAMP_DOMAINS_KEY] = domains;
    domains[database] = domains[database] ?? {};
    domains[database][schema] = domains[database][schema] ?? {};
    domains[database][schema][table] = domains[database][schema][table] ?? {};
    domains[database][schema][table][column] = timestampDomain;
};
/**
 * True when the catalog was produced by domain-aware code: the sidecar key is
 * present, even if empty. A missing key means a pre-domain cache that should
 * be refetched once so timestamp columns get classified.
 */
export const catalogHasTimestampDomains = (catalog) => WAREHOUSE_TIMESTAMP_DOMAINS_KEY in catalog;
/**
 * Stamps the (possibly empty) sidecar onto a freshly fetched catalog so
 * `catalogHasTimestampDomains` can tell it apart from a pre-domain cache —
 * clients only create the key when they classify at least one column.
 */
export const ensureCatalogTimestampDomainsKey = (catalog) => {
    const catalogWithDomains = catalog;
    catalogWithDomains[WAREHOUSE_TIMESTAMP_DOMAINS_KEY] =
        catalogWithDomains[WAREHOUSE_TIMESTAMP_DOMAINS_KEY] ?? {};
};
export var TimeIntervalUnit;
(function (TimeIntervalUnit) {
    TimeIntervalUnit["SECOND"] = "SECOND";
    TimeIntervalUnit["MINUTE"] = "MINUTE";
    TimeIntervalUnit["HOUR"] = "HOUR";
    TimeIntervalUnit["DAY"] = "DAY";
    TimeIntervalUnit["WEEK"] = "WEEK";
    TimeIntervalUnit["MONTH"] = "MONTH";
    TimeIntervalUnit["YEAR"] = "YEAR";
})(TimeIntervalUnit || (TimeIntervalUnit = {}));
export var PartitionType;
(function (PartitionType) {
    PartitionType["DATE"] = "DATE";
    PartitionType["RANGE"] = "RANGE";
})(PartitionType || (PartitionType = {}));
//# sourceMappingURL=warehouse.js.map