Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

32.8 KB
Raw Blame
import { deepEqual as equal } from 'fast-equals'
import { ReactNode } from 'react'

import { IconWarning } from '@posthog/icons'
import { LemonButtonProps } from '@posthog/lemon-ui'

import { DataColorTheme, DataColorToken } from 'lib/colors'
import { dayjs } from 'lib/dayjs'
import { isEmptyObject, isObject } from 'lib/utils/guards'
import { humanFriendlyNumber } from 'lib/utils/numbers'
import { objectsEqual } from 'lib/utils/objects'
import { removeUndefinedAndNull } from 'lib/utils/objects'
import { ensureStringIsNotBlank } from 'lib/utils/strings'
import { queryUsesFiltersPlaceholder } from 'scenes/data-warehouse/editor/sql-utils'
import { isWarehouseSeriesNode } from 'scenes/insights/filters/ActionFilter/seriesNode'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { propertyFilterTypeToPropertyDefinitionType } from '~/lib/components/PropertyFilters/utils'
import { FormatPropertyValueForDisplayFunction } from '~/models/propertyDefinitionsModel'
import { examples } from '~/queries/examples'
import { cleanInsightQuery } from '~/queries/nodes/InsightViz/queryUtils'
import {
    containsHogQLQuery,
    isBIVisualizationNode,
    isDataTableNode,
    isEventsNode,
    isGroupNode,
    isHogQLQuery,
    isInsightVizNode,
    isNodeWithSource,
} from '~/queries/schema/node-guards'
import {
    AnyDataWarehouseNode,
    AnyEntityNode,
    ExperimentDataWarehouseNode,
    BreakdownFilter,
    DashboardFilter,
    FileSystemIconType,
    GroupNode,
    HogQLQuery,
    HogQLQueryModifiers,
    HogQLVariable,
    InsightQueryNode,
    InsightVizNode,
    Node,
    NodeKind,
    PathsFilter,
    ResultCustomization,
    ResultCustomizationBy,
    ResultCustomizationByPosition,
    ResultCustomizationByValue,
    TileFilters,
} from '~/queries/schema/schema-general'
import { getResultSeriesIndex } from '~/scenes/insights/utils/resultSeries'
import { CORE_FILTER_DEFINITIONS_BY_GROUP } from '~/taxonomy/taxonomy'
import {
    BreakdownKeyType,
    ChartDisplayType,
    CohortType,
    EventType,
    FlattenedFunnelStepByBreakdown,
    FunnelStepWithConversionMetrics,
    GroupTypeIndex,
    InsightType,
    PathType,
    PropertyFilterType,
    PropertyOperator,
} from '~/types'

import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'

export const getDisplayNameFromEntityNode = (
    node: AnyEntityNode<AnyDataWarehouseNode | ExperimentDataWarehouseNode> | GroupNode,
    isCustom = true
): string | null => {
    // Make sure names aren't blank strings
    const customName = ensureStringIsNotBlank(node?.custom_name)
    let name = ensureStringIsNotBlank(node?.name)

    // Handle GroupNode: format comma-separated event names
    if (isGroupNode(node) && name) {
        const eventNames = name.split(',').map((eventName) => {
            const trimmedName = eventName.trim()
            return CORE_FILTER_DEFINITIONS_BY_GROUP.events?.[trimmedName]?.label || trimmedName
        })
        name = eventNames.join(', ')
    } else if (name && name in CORE_FILTER_DEFINITIONS_BY_GROUP.events) {
        name = CORE_FILTER_DEFINITIONS_BY_GROUP.events[name].label
    }

    if (isEventsNode(node) && node.event === null) {
        name = 'All events'
    }

    const id = isWarehouseSeriesNode(node)
        ? node.table_name
        : isEventsNode(node)
          ? node.event
          : isGroupNode(node)
            ? undefined
            : node?.id

    // Return custom name. If that doesn't exist then the name, then the id, then just null.
    return (isCustom ? customName : null) ?? name ?? (id ? `${id}` : null)
}

export function humanizePathsEventTypes(includeEventTypes: PathsFilter['includeEventTypes']): string[] {
    let humanEventTypes: string[] = []
    if (includeEventTypes) {
        if (includeEventTypes.includes(PathType.PageView)) {
            humanEventTypes.push('page views')
        }
        if (includeEventTypes.includes(PathType.Screen)) {
            humanEventTypes.push('screen views')
        }
        if (includeEventTypes.includes(PathType.CustomEvent)) {
            humanEventTypes.push('custom events')
        }
        if (
            (humanEventTypes.length === 0 && !includeEventTypes.includes(PathType.HogQL)) ||
            humanEventTypes.length === 3
        ) {
            humanEventTypes = ['all events']
        }
        if (includeEventTypes.includes(PathType.HogQL)) {
            humanEventTypes.push('SQL expression')
        }
    }
    return humanEventTypes
}

export function formatAggregationValue(
    property: string | undefined | null,
    propertyValue: number | null,
    renderCount: (value: number) => ReactNode = (x) => <>{humanFriendlyNumber(x)}</>,
    formatPropertyValueForDisplay?: FormatPropertyValueForDisplayFunction
): ReactNode {
    if (propertyValue === null) {
        return '-'
    }

    let formattedValue: ReactNode
    if (property && formatPropertyValueForDisplay) {
        formattedValue = formatPropertyValueForDisplay(property, propertyValue)
        // yes, double equals not triple equals  ¯\_(ツ)_/¯ let JS compare strings and numbers however it wants
        if (String(formattedValue) == String(propertyValue)) {
            // formatPropertyValueForDisplay didn't change the value...
            formattedValue = renderCount(propertyValue)
        }
    } else {
        formattedValue = renderCount(propertyValue)
    }

    // Since `propertyValue` is a number. `formatPropertyValueForDisplay` will only return a string
    // To make typescript happy we handle the possible but impossible string array inside this function
    return Array.isArray(formattedValue) ? formattedValue[0] : formattedValue
}

// NB! Sync this with breakdown_values.py and hogql_queries/utils/breakdowns.py
export const BREAKDOWN_OTHER_STRING_LABEL = '$$_posthog_breakdown_other_$$'
export const BREAKDOWN_OTHER_NUMERIC_LABEL = 9007199254740991 // pow(2, 53) - 1
export const BREAKDOWN_OTHER_DISPLAY = 'Other (i.e. all remaining values)'
export const BREAKDOWN_NULL_STRING_LABEL = '$$_posthog_breakdown_null_$$'
export const BREAKDOWN_NULL_NUMERIC_LABEL = 9007199254740990 // pow(2, 53) - 2
export const BREAKDOWN_NULL_DISPLAY = 'None (i.e. no value)'
export const BREAKDOWN_BASELINE_STRING_LABEL = '$$_posthog_breakdown_baseline_$$'

export function isOtherBreakdown(breakdown_value: string | number | bigint | null | undefined | ReactNode): boolean {
    return (
        breakdown_value === BREAKDOWN_OTHER_STRING_LABEL ||
        breakdown_value === BREAKDOWN_OTHER_NUMERIC_LABEL ||
        String(breakdown_value) === String(BREAKDOWN_OTHER_NUMERIC_LABEL)
    )
}

export function isNullBreakdown(breakdown_value: string | number | bigint | null | undefined): boolean {
    return (
        breakdown_value === BREAKDOWN_NULL_STRING_LABEL ||
        breakdown_value === BREAKDOWN_NULL_NUMERIC_LABEL ||
        String(breakdown_value) === String(BREAKDOWN_NULL_NUMERIC_LABEL)
    )
}

function isValidJsonArray(maybeJson: string): boolean {
    if (maybeJson.startsWith('[')) {
        try {
            const json = JSON.parse(maybeJson)
            return Array.isArray(json)
        } catch {
            return false
        }
    }

    return false
}

function formatNumericBreakdownLabel(
    breakdown_value: number | bigint,
    breakdownFilter: BreakdownFilter | null | undefined,
    formatPropertyValueForDisplay: FormatPropertyValueForDisplayFunction | undefined,
    multipleBreakdownIndex: number | undefined
): string {
    if (isOtherBreakdown(breakdown_value)) {
        return BREAKDOWN_OTHER_DISPLAY
    }

    if (isNullBreakdown(breakdown_value)) {
        return BREAKDOWN_NULL_DISPLAY
    }

    const breakdownType = (
        typeof multipleBreakdownIndex === 'number'
            ? breakdownFilter?.breakdowns?.[multipleBreakdownIndex]?.type
            : breakdownFilter?.breakdown_type
    ) as string | undefined

    // Element breakdown values (tag_name, text, href) have no matching property definition — the type
    // falls back to Event, so a same-named DateTime/Duration event property would otherwise reformat
    // a numeric-looking element value into a date or duration.
    if (formatPropertyValueForDisplay && breakdownType !== 'element') {
        const nestedBreakdown =
            typeof multipleBreakdownIndex === 'number'
                ? breakdownFilter?.breakdowns?.[multipleBreakdownIndex]
                : undefined

        const groupIndex = (nestedBreakdown?.group_type_index ?? breakdownFilter?.breakdown_group_type_index) as
            | GroupTypeIndex
            | undefined

        return (
            formatPropertyValueForDisplay(
                nestedBreakdown?.property ?? breakdownFilter?.breakdown,
                breakdown_value,
                propertyFilterTypeToPropertyDefinitionType(nestedBreakdown?.type ?? breakdownFilter?.breakdown_type),
                groupIndex
            )?.toString() ?? 'None'
        )
    }

    return String(breakdown_value)
}

// Keep in sync with NOT_IN_COHORT_ID in posthog/hogql_queries/utils/breakdowns.py
export const NOT_IN_COHORT_ID = 2 ** 52

export function getCohortNameFromId(
    cohortId: string | number | null | undefined,
    cohorts: CohortType[] | null | undefined
): string {
    // :TRICKY: Different endpoints represent the all users cohort breakdown differently
    if (cohortId === 'all' || cohortId === 0 || cohortId === '0') {
        return 'All Users'
    }

    if (Number(cohortId) === NOT_IN_COHORT_ID) {
        return 'Not in cohort'
    }

    const cohortName = cohorts?.filter((c) => c.id == cohortId)[0]?.name
    if (cohortName) {
        return cohortName
    }

    // The cohorts list may not be loaded yet (or the cohort was deleted). Fall back to a
    // human-readable label rather than leaking a bare numeric id into pills/axis labels.
    // Keep in sync with BreakdownTag's `Cohort ${id}` convention.
    if (cohortId == null || cohortId === '') {
        return ''
    }
    return `Cohort ${cohortId}`
}

export function formatBreakdownLabel(
    breakdown_value: BreakdownKeyType | undefined,
    breakdownFilter: BreakdownFilter | null | undefined,
    cohorts: CohortType[] | undefined,
    formatPropertyValueForDisplay: FormatPropertyValueForDisplayFunction | undefined,
    multipleBreakdownIndex?: number,
    itemLabel?: string,
    truncateLabel: boolean = true
): string {
    if (Array.isArray(breakdown_value)) {
        return breakdown_value
            .map((v, index) =>
                formatBreakdownLabel(
                    v,
                    breakdownFilter,
                    cohorts,
                    formatPropertyValueForDisplay,
                    index,
                    undefined,
                    truncateLabel
                )
            )
            .join('::')
    }

    if (typeof breakdown_value === 'string' && breakdown_value.length > 0 && isValidJsonArray(breakdown_value)) {
        // replace nan with null
        const bucketValues = breakdown_value.replace(/\bnan\b/g, 'null')
        const [bucketStart, bucketEnd] = JSON.parse(bucketValues)
        const formattedBucketStart = formatBreakdownLabel(
            bucketStart,
            breakdownFilter,
            cohorts,
            formatPropertyValueForDisplay,
            multipleBreakdownIndex,
            undefined,
            truncateLabel
        )
        const formattedBucketEnd = formatBreakdownLabel(
            bucketEnd,
            breakdownFilter,
            cohorts,
            formatPropertyValueForDisplay,
            multipleBreakdownIndex,
            undefined,
            truncateLabel
        )
        if (formattedBucketStart === formattedBucketEnd) {
            return formattedBucketStart
        }
        return `${formattedBucketStart} – ${formattedBucketEnd}`
    }

    const breakdownType =
        multipleBreakdownIndex != null ? breakdownFilter?.breakdowns?.[multipleBreakdownIndex]?.type : null

    if (breakdownFilter?.breakdown_type === 'cohort' || breakdownType === 'cohort') {
        if (breakdown_value === 'all' || breakdown_value === 0) {
            return 'All Users'
        }
        if (Number(breakdown_value) === NOT_IN_COHORT_ID) {
            const selectedCohorts = Array.isArray(breakdownFilter?.breakdown) ? breakdownFilter.breakdown : []
            const selectedCohortId = selectedCohorts.find((id) => id !== 'all' && Number(id) !== NOT_IN_COHORT_ID)
            if (selectedCohortId != null && cohorts) {
                const cohortName = cohorts.find((c) => c.id == selectedCohortId)?.name
                if (cohortName) {
                    return `Not in ${cohortName}`
                }
            }
            return 'Not in cohort'
        }
        if (cohorts == null || cohorts.length === 0) {
            if (itemLabel != null) {
                return itemLabel
            }
        }
        return getCohortNameFromId(breakdown_value, cohorts)
    }

    if (typeof breakdown_value == 'number') {
        return formatNumericBreakdownLabel(
            breakdown_value,
            breakdownFilter,
            formatPropertyValueForDisplay,
            multipleBreakdownIndex
        )
    }

    // stringified numbers
    if (breakdown_value && /^\d+$/.test(breakdown_value)) {
        const numericValue =
            Number.isInteger(Number(breakdown_value)) && !Number.isSafeInteger(Number(breakdown_value))
                ? BigInt(breakdown_value)
                : Number(breakdown_value)
        return formatNumericBreakdownLabel(
            numericValue,
            breakdownFilter,
            formatPropertyValueForDisplay,
            multipleBreakdownIndex
        )
    }

    if (typeof breakdown_value == 'string') {
        const label =
            isOtherBreakdown(breakdown_value) || breakdown_value === 'nan'
                ? BREAKDOWN_OTHER_DISPLAY
                : isNullBreakdown(breakdown_value) || breakdown_value === ''
                  ? BREAKDOWN_NULL_DISPLAY
                  : breakdown_value

        if (truncateLabel && label.length > 200) {
            return label.slice(0, 200) + '…'
        }
        return label
    }

    return ''
}

export function formatBreakdownType(breakdownFilter: BreakdownFilter): string {
    if (breakdownFilter.breakdown_type === 'cohort') {
        return 'Cohort'
    }
    return breakdownFilter?.breakdown?.toString() || 'Breakdown Value'
}

export function sortCohorts(
    cohortIdA: string | number | null | undefined,
    cohortIdB: string | number | null | undefined,
    cohorts: CohortType[] | null | undefined
): number {
    const nameA = getCohortNameFromId(cohortIdA, cohorts)
    const nameB = getCohortNameFromId(cohortIdB, cohorts)
    return nameA.localeCompare(nameB)
}

export function sortDates(dates: Array<string | null>): Array<string | null> {
    return dates.sort((a, b) => (dayjs(a).isAfter(dayjs(b)) ? 1 : -1))
}

export function sortDayJsDates(dates: Array<dayjs.Dayjs>): Array<dayjs.Dayjs> {
    return dates.sort((a, b) => (a.isAfter(b) ? 1 : -1))
}

// Gets content-length header from a fetch Response
export function getResponseBytes(apiResponse: Response): number {
    return parseInt(apiResponse.headers.get('Content-Length') ?? '0')
}

export const INSIGHT_TYPE_URLS: Record<InsightType | string, string> = {
    [InsightType.TRENDS]: urls.insightNew({ type: InsightType.TRENDS }),
    [InsightType.STICKINESS]: urls.insightNew({ type: InsightType.STICKINESS }),
    [InsightType.LIFECYCLE]: urls.insightNew({ type: InsightType.LIFECYCLE }),
    [InsightType.FUNNELS]: urls.insightNew({ type: InsightType.FUNNELS }),
    [InsightType.RETENTION]: urls.insightNew({ type: InsightType.RETENTION }),
    [InsightType.PATHS]: urls.insightNew({ type: InsightType.PATHS }),
    [InsightType.JOURNEYS]: urls.insightNew({ type: InsightType.JOURNEYS }),
    [InsightType.WEB_ANALYTICS]: urls.insightNew({ type: InsightType.WEB_ANALYTICS }),
    [InsightType.METRICS]: urls.insightNew({ type: InsightType.METRICS }),
    JSON: urls.insightNew({ query: examples.EventsTableFull }),
    HOG: urls.insightNew({ query: examples.Hoggonacci }),
    SQL: urls.sqlEditor({ query: (examples.HogQLForDataVisualization as HogQLQuery)['query'] }),
}

/** Combines a list of words, separating with the correct punctuation. For example: [a, b, c, d] -> "a, b, c, and d"  */
export function concatWithPunctuation(phrases: string[]): string {
    if (phrases === null || phrases.length === 0) {
        return ''
    } else if (phrases.length === 1) {
        return phrases[0]
    } else if (phrases.length === 2) {
        return `${phrases[0]} and ${phrases[1]}`
    }
    return `${phrases.slice(0, phrases.length - 1).join(', ')}, and ${phrases[phrases.length - 1]}`
}

export function insightUrlForEvent(event: Pick<EventType, 'event' | 'properties'>): string | undefined {
    let query: InsightVizNode | undefined
    if (event.event === '$pageview') {
        query = {
            kind: NodeKind.InsightVizNode,
            source: {
                kind: NodeKind.TrendsQuery,
                interval: 'day',
                series: [
                    {
                        event: '$pageview',
                        name: '$pageview',
                        kind: NodeKind.EventsNode,
                        properties: [
                            {
                                key: '$current_url',
                                value: event.properties.$current_url,
                                type: PropertyFilterType.Event,
                                operator: PropertyOperator.Exact,
                            },
                        ],
                    },
                ],
                trendsFilter: { display: ChartDisplayType.ActionsLineGraph },
            },
        }
    } else if (event.event !== '$autocapture') {
        query = {
            kind: NodeKind.InsightVizNode,
            source: {
                kind: NodeKind.TrendsQuery,
                interval: 'day',
                series: [
                    {
                        event: event.event,
                        name: event.event,
                        kind: NodeKind.EventsNode,
                    },
                ],
                trendsFilter: { display: ChartDisplayType.ActionsLineGraph },
            },
        }
    }

    return query ? urls.insightNew({ query }) : undefined
}

export function getFunnelDatasetKey(dataset: { breakdown_value?: BreakdownKeyType }): string {
    const breakdown_value =
        Array.isArray(dataset.breakdown_value) && dataset.breakdown_value.length == 1
            ? dataset.breakdown_value[0]
            : dataset.breakdown_value
    const payload = { breakdown_value }

    return JSON.stringify(payload)
}

/** Identifies the base series — deliberately excludes `compare_label`, so that current and
 * previous period datasets share a single result customization. */
export function getTrendDatasetKey(
    dataset: IndexedTrendResult,
    querySource: InsightQueryNode | null | undefined
): string {
    // for formulas, the position (not seriesIndex) identifies the base series: with compare
    // enabled, previous-period datasets have offset series indexes, but share the position
    // with their current-period counterpart
    const formulaPosition = getTrendDatasetPosition(dataset)
    const payload = {
        series:
            getResultSeriesIndex(querySource, dataset) ??
            (formulaPosition > 0 ? `formula${formulaPosition + 1}` : 'formula'),
        breakdown_value:
            dataset.breakdown_value !== undefined && !Array.isArray(dataset.breakdown_value)
                ? [dataset.breakdown_value]
                : dataset.breakdown_value,
    }

    return JSON.stringify(payload)
}

export function getTrendDatasetPosition(dataset: IndexedTrendResult): number {
    // colorIndex is shared by current/previous period pairs, so position-based
    // customizations apply to the base series rather than each period separately
    return dataset.colorIndex ?? dataset.seriesIndex ?? ((dataset as any).index as number)
}

/** Type guard to determine whether we have a FunnelStepWithConversionMetrics or a FlattenedFunnelStepByBreakdown */
function isFunnelStepWithConversionMetrics(
    dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics
): dataset is FunnelStepWithConversionMetrics {
    return (dataset as FlattenedFunnelStepByBreakdown).breakdownIndex == null
}

export function getFunnelDatasetPosition(
    dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics,
    disableFunnelBreakdownBaseline?: boolean
): number {
    if (isFunnelStepWithConversionMetrics(dataset)) {
        // increment the minimum order for funnels where there baseline is hidden
        // i.e. funnels for experiments where only the respective variants matter
        return disableFunnelBreakdownBaseline ? (dataset.order ?? 0) + 1 : (dataset.order ?? 0)
    }

    return dataset?.colorIndex ?? dataset?.breakdownIndex ?? 0
}

export function getTrendResultCustomizationKey(
    resultCustomizationBy: ResultCustomizationBy | null | undefined,
    dataset: IndexedTrendResult,
    querySource: InsightQueryNode | null | undefined
): string {
    const assignmentByValue = resultCustomizationBy == null || resultCustomizationBy === ResultCustomizationBy.Value
    return assignmentByValue ? getTrendDatasetKey(dataset, querySource) : getTrendDatasetPosition(dataset).toString()
}

export function getTrendResultCustomization(
    resultCustomizationBy: ResultCustomizationBy | null | undefined,
    dataset: IndexedTrendResult,
    resultCustomizations:
        | Record<string, ResultCustomizationByValue>
        | Record<number, ResultCustomizationByPosition>
        | null
        | undefined,
    querySource: InsightQueryNode | null | undefined
): ResultCustomization | undefined {
    const resultCustomizationKey = getTrendResultCustomizationKey(resultCustomizationBy, dataset, querySource)
    return resultCustomizations && Object.keys(resultCustomizations).includes(resultCustomizationKey)
        ? (resultCustomizations as any)[resultCustomizationKey]
        : undefined
}

export function getFunnelResultCustomization(
    dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics,
    resultCustomizations: Record<string, ResultCustomizationByValue> | null | undefined
): ResultCustomization | undefined {
    const resultCustomizationKey = getFunnelDatasetKey(dataset)
    return resultCustomizations && Object.keys(resultCustomizations).includes(resultCustomizationKey)
        ? resultCustomizations[resultCustomizationKey]
        : undefined
}

export function getTrendResultCustomizationColorToken(
    resultCustomizationBy: ResultCustomizationBy | null | undefined,
    resultCustomizations:
        | Record<string, ResultCustomizationByValue>
        | Record<number, ResultCustomizationByPosition>
        | null
        | undefined,
    theme: DataColorTheme,
    dataset: IndexedTrendResult,
    querySource: InsightQueryNode | null | undefined
): DataColorToken {
    const resultCustomization = getTrendResultCustomization(
        resultCustomizationBy,
        dataset,
        resultCustomizations,
        querySource
    )

    // for result customizations without a configuration, the color is determined
    // by the position in the dataset (shared by current/previous period pairs).
    // colors repeat after all options have been exhausted.
    const datasetPosition = getTrendDatasetPosition(dataset)
    const tokenIndex = (datasetPosition % Object.keys(theme).length) + 1

    return resultCustomization && resultCustomization.color
        ? resultCustomization.color
        : (`preset-${tokenIndex}` as DataColorToken)
}

export function getFunnelResultCustomizationColorToken(
    resultCustomizations: Record<string, ResultCustomizationByValue> | null | undefined,
    theme: DataColorTheme,
    dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics,
    disableFunnelBreakdownBaseline?: boolean
): DataColorToken {
    const resultCustomization = getFunnelResultCustomization(dataset, resultCustomizations)

    const datasetPosition = getFunnelDatasetPosition(dataset, disableFunnelBreakdownBaseline)
    const tokenIndex = (datasetPosition % Object.keys(theme).length) + 1

    return resultCustomization && resultCustomization.color
        ? resultCustomization.color
        : (`preset-${tokenIndex}` as DataColorToken)
}

export function isQueryTooLarge(query: Node<Record<string, any>>): boolean {
    // Chrome has a 2MB limit for the HASH params, limit ours at 1MB
    const queryLength = encodeURI(JSON.stringify(query)).split(/%..|./).length - 1
    return queryLength > 1024 * 1024
}

function parseQuery<T>(query: string): T | null {
    try {
        return JSON.parse(query)
    } catch (e) {
        console.error('Error parsing query', e)
        return null
    }
}

export function parseDraftQueryFromLocalStorage(
    query: string
): { query: Node<Record<string, any>>; timestamp: number } | null {
    return parseQuery(query)
}

export function crushDraftQueryForLocalStorage(query: Node<Record<string, any>>, timestamp: number): string {
    return JSON.stringify({ query, timestamp })
}

export function parseDraftQueryFromURL(query: string): Node<Record<string, any>> | null {
    return parseQuery(query)
}

export function crushDraftQueryForURL(query: Node<Record<string, any>>): string {
    return JSON.stringify(query)
}

/**
 * Query plumbing rather than editor sections: the schema version stamp, the query log tags, and
 * the typing-only response. A suggested query arrives without them, so comparing them reports a
 * change nobody made. `modifiers` are handled separately below.
 */
const IGNORED_SOURCE_FIELDS = ['version', 'tags', 'response']

const SOURCE_FIELD_LABELS: Record<string, string> = {
    breakdownFilter: 'Breakdowns',
    compareFilter: 'Compare filter',
    dateRange: 'Date range',
    filterTestAccounts: 'Test account filtering',
    interval: 'Interval',
    kind: 'Insight type',
    properties: 'Global property filters',
    samplingFactor: 'Sampling',
    series: 'Series',
    trendsFilter: 'Display options',
    modifiers: 'Query modifiers',
}

function arraysEqual(arr1: any[], arr2: any[]): boolean {
    if (arr1.length !== arr2.length) {
        return false
    }

    // Create copies and sort them for order-independent comparison
    const sorted1 = [...arr1].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)))
    const sorted2 = [...arr2].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)))

    // Compare each element
    for (let i = 0; i < sorted1.length; i++) {
        if (!equal(sorted1[i], sorted2[i])) {
            return false
        }
    }
    return true
}

function deepEqual(val1: any, val2: any): boolean {
    if (Array.isArray(val1) && Array.isArray(val2)) {
        return arraysEqual(val1, val2)
    }
    return equal(val1, val2)
}

/**
 * Resolve a query's modifiers the way the backend does: explicit query modifiers win, then the
 * team overrides, then the PostHog defaults. Absent and default-equal modifiers both resolve to
 * the team's effective set, so comparing resolved modifiers skips changes nobody made.
 */
function resolveModifiersForComparison(
    modifiers: HogQLQueryModifiers | undefined,
    teamModifiers: HogQLQueryModifiers | undefined
): HogQLQueryModifiers {
    return { ...teamModifiers, ...removeUndefinedAndNull(modifiers ?? {}) }
}

/**
 * Clean the source of an insight query for comparison. Handles both InsightVizNode (with source)
 * and InsightQueryNode (without source). `cleanInsightQuery` only strips empty values one level
 * deep, so it must run on the source — given the wrapping node it leaves `source.trendsFilter: {}`
 * behind, which then reads as a changed section.
 */
function cleanSourceForComparison(node: any, teamModifiers: HogQLQueryModifiers | undefined): Record<string, any> {
    const withoutNullish = removeUndefinedAndNull(node)
    const source = withoutNullish?.source ?? withoutNullish
    if (!source || typeof source !== 'object') {
        return {}
    }
    const cleaned = cleanInsightQuery(source) as Record<string, any>
    for (const field of IGNORED_SOURCE_FIELDS) {
        delete cleaned[field]
    }
    const resolvedModifiers = resolveModifiersForComparison(cleaned.modifiers, teamModifiers)
    if (Object.keys(resolvedModifiers).length > 0) {
        cleaned.modifiers = resolvedModifiers
    } else {
        delete cleaned.modifiers
    }
    return cleaned
}

export function compareInsightTopLevelSections(obj1: any, obj2: any): string[] {
    const changedLabels = new Set<string>()

    const currentTeam = teamLogic.findMounted()?.values.currentTeam
    const teamModifiers = { ...currentTeam?.default_modifiers, ...currentTeam?.modifiers }

    const source1 = cleanSourceForComparison(obj1, teamModifiers)
    const source2 = cleanSourceForComparison(obj2, teamModifiers)

    if (!objectsEqual(source1, source2)) {
        const keys = new Set([...Object.keys(source1 || {}), ...Object.keys(source2 || {})])

        for (const key of keys) {
            const val1 = source1?.[key]
            const val2 = source2?.[key]

            // Check if the property exists in both objects and has different values
            // Also check if property exists in one but not the other
            if (!deepEqual(val1, val2) || key in source1 !== key in source2) {
                const label = SOURCE_FIELD_LABELS[key] || key
                changedLabels.add(label)
            }
        }
    }

    return Array.from(changedLabels).sort()
}

export function getInsightIconTypeFromQuery(query: any): FileSystemIconType {
    if (isBIVisualizationNode(query)) {
        return 'business_intelligence'
    }
    if (!query?.kind) {
        return 'product_analytics'
    }

    let nodeKind: NodeKind
    if ((isDataTableNode(query) && containsHogQLQuery(query)) || isInsightVizNode(query)) {
        nodeKind = query.source.kind
    } else {
        nodeKind = query.kind
    }

    // Map NodeKind to the fileSystemType color names
    const nodeKindToColor: Partial<Record<NodeKind, FileSystemIconType>> = {
        [NodeKind.TrendsQuery]: 'insight/trends',
        [NodeKind.FunnelsQuery]: 'insight/funnels',
        [NodeKind.RetentionQuery]: 'insight/retention',
        [NodeKind.PathsQuery]: 'insight/paths',
        [NodeKind.StickinessQuery]: 'insight/stickiness',
        [NodeKind.LifecycleQuery]: 'insight/lifecycle',
        [NodeKind.HogQuery]: 'insight/hog',
        [NodeKind.HogQLQuery]: 'insight/hog',
        [NodeKind.DataVisualizationNode]: 'insight/hog',
        [NodeKind.DataTableNode]: 'insight/hog',
    }

    const mappedIconType: FileSystemIconType = nodeKindToColor[nodeKind] || 'product_analytics'

    return mappedIconType
}

export const getOverrideWarningPropsForButton = (
    query: Node | null | undefined,
    filtersOverride: DashboardFilter | null | undefined,
    variablesOverride: Record<string, HogQLVariable> | null | undefined
): Pick<LemonButtonProps, 'icon' | 'tooltip'> => {
    // The backend applies dashboard filters to a SQL query only through a {filters} placeholder, and
    // dashboard variables only to the variables the query declares. Other overrides do not change
    // the result, so editing discards nothing.
    const hogQLSource = isHogQLQuery(query)
        ? query
        : isNodeWithSource(query) && isHogQLQuery(query.source)
          ? query.source
          : null
    const filterOverridesExist =
        isObject(filtersOverride) &&
        Object.values(filtersOverride).some((value) => value !== null && (!Array.isArray(value) || value.length > 0)) &&
        (!hogQLSource || queryUsesFiltersPlaceholder(hogQLSource.query))
    const variableOverridesExist =
        isObject(variablesOverride) &&
        (hogQLSource
            ? Object.keys(variablesOverride).some((variableId) => !!hogQLSource.variables?.[variableId])
            : !isEmptyObject(variablesOverride))

    const overrideType =
        filterOverridesExist && variableOverridesExist ? 'overrides' : filterOverridesExist ? 'filters' : 'variables'

    return filterOverridesExist || variableOverridesExist
        ? {
              icon: <IconWarning />,
              tooltip: `This insight is being viewed with dashboard ${overrideType}. These will be discarded on edit.`,
          }
        : {}
}

/** Checks for breakdown features that are unsupported by trend insights with a
 * data warehouse series. Mirrors backend `ValidateDataWarehouseBreakdown`. */
export const hasUnsupportedBreakdownForDataWarehouseTrends = (
    filtersOverride: DashboardFilter | TileFilters | null | undefined
): boolean => {
    const breakdownFilter = filtersOverride?.breakdown_filter

    if (!breakdownFilter) {
        return false
    }

    const supportedTypes = new Set(['data_warehouse', 'hogql'])

    if (breakdownFilter.breakdowns?.length) {
        return breakdownFilter.breakdowns.some((b) => !b.type || !supportedTypes.has(b.type))
    }

    return !!(
        !breakdownFilter.breakdown ||
        Array.isArray(breakdownFilter.breakdown) ||
        !breakdownFilter.breakdown_type ||
        !supportedTypes.has(breakdownFilter.breakdown_type)
    )
}