// (C) 2019-2025 GoodData Corporation
import { isEmpty, reduce } from "lodash-es";
import { BucketNames } from "@gooddata/sdk-ui";
import { getAllAttributeItems, getAllItemsByType, getAttributeItemsWithoutStacks, getBucketItems, getItemsCount, getMeasureItems, getStackItems, isDateBucketItem, isMeasureValueFilter, isRankingFilter, } from "./bucketHelper.js";
import { ALL_TIME, ATTRIBUTE, DATE, METRIC } from "../constants/bucket.js";
import { INCREASE_MAX_TABLE_ATTRIBUTES_ITEMS_LIMIT, INCREASE_MAX_TABLE_MEASURE_ITEMS_LIMIT, MAX_METRICS_COUNT, MAX_TABLE_CATEGORIES_COUNT, } from "../constants/uiConfig.js";
export function hasOneMeasure(buckets) {
    return getItemsCount(buckets, BucketNames.MEASURES) === 1;
}
function hasOneMasterMeasure(buckets) {
    return getMasterMeasuresCount(buckets, BucketNames.MEASURES) === 1;
}
export function getMasterMeasuresCount(buckets, bucketLocalIdentifier) {
    const items = getBucketItems(buckets, bucketLocalIdentifier);
    return reduce(items, (acc, item) => (item.masterLocalIdentifier ? acc : acc + 1), 0);
}
export function hasOneMasterMeasureInBucket(buckets, bucketLocalIdentifier) {
    return getMasterMeasuresCount(buckets, bucketLocalIdentifier) === 1;
}
export function hasOneMeasureOrAlsoLineStyleControlMeasure(buckets) {
    const measures = getBucketItems(buckets, BucketNames.MEASURES);
    const lineStyleControlMetrics = measures.filter((measure) => measure.isThresholdMeasure);
    return measures.length === 1 || (measures.length === 2 && lineStyleControlMetrics.length === 1);
}
export function filteredByDerivedMeasure(buckets, filters) {
    const measures = getAllItemsByType(buckets, [METRIC]);
    const derivedMeasuresLocalIdentifiers = measures.reduce((acc, measure) => {
        if (measure.masterLocalIdentifier) {
            acc.push(measure.localIdentifier);
        }
        return acc;
    }, []);
    const allBucketFilters = filters.items.flatMap((filterItem) => filterItem.filters);
    return allBucketFilters
        .filter(isMeasureValueFilter)
        .some((measureValueFilter) => derivedMeasuresLocalIdentifiers.includes(measureValueFilter.measureLocalIdentifier));
}
export function hasNoMeasures(buckets) {
    return getItemsCount(buckets, BucketNames.MEASURES) === 0;
}
export function hasNoSecondaryMeasures(buckets) {
    return getItemsCount(buckets, BucketNames.SECONDARY_MEASURES) === 0;
}
export function hasNoAttribute(buckets) {
    return getItemsCount(buckets, BucketNames.ATTRIBUTE) === 0;
}
export function hasMeasuresOrRowsUnderLowerLimit(buckets) {
    return (getItemsCount(buckets, BucketNames.ATTRIBUTE) <= MAX_TABLE_CATEGORIES_COUNT &&
        getItemsCount(buckets, BucketNames.MEASURES) <= MAX_METRICS_COUNT);
}
export function hasNoColumns(buckets) {
    return getItemsCount(buckets, BucketNames.COLUMNS) === 0;
}
export function hasSomeSegmentByItems(buckets) {
    return getItemsCount(buckets, BucketNames.SEGMENT) !== 0;
}
export function hasMoreThanOneCategory(buckets) {
    return getAllAttributeItems(buckets).length > 1;
}
export function hasMoreThanOneMasterMeasure(buckets, bucketLocalIdentifier) {
    return getMasterMeasuresCount(buckets, bucketLocalIdentifier) > 1;
}
function hasSomeCategories(buckets) {
    return getAttributeItemsWithoutStacks(buckets).length > 0;
}
function hasNoCategories(buckets) {
    return getAttributeItemsWithoutStacks(buckets).length === 0;
}
function allRulesMet(rules, buckets, filters) {
    return rules.every((rule) => rule(buckets, filters));
}
function hasDateInCategories(buckets) {
    return getAllAttributeItems(buckets).some(isDateBucketItem);
}
export function hasGlobalDateFilterIgnoreAllTime(filters) {
    if (filters) {
        const filterBucketItems = filters?.items ?? [];
        const dateFilter = filterBucketItems.find((filter) => {
            return filter?.attribute === "attr.datedataset";
        });
        const interval = dateFilter?.filters?.[0]?.interval ?? null;
        return interval && interval.name !== ALL_TIME;
    }
    return false;
}
export function hasGlobalDateFilter(filters) {
    if (filters) {
        return filters.items.some((item) => item?.filters?.[0]?.interval);
    }
    return false;
}
export function hasUsedDateIgnoreAllTime(buckets, filters) {
    return hasDateInCategories(buckets) || hasGlobalDateFilterIgnoreAllTime(filters);
}
export function hasUsedDate(buckets, filters) {
    return hasDateInCategories(buckets) || hasGlobalDateFilter(filters);
}
function hasNoMeasureDateFilter(buckets) {
    return !getMeasureItems(buckets).some((item) => {
        const filters = item?.filters;
        return filters?.some((filter) => isDateBucketItem(filter));
    });
}
export function hasNoStacks(buckets) {
    return getStackItems(buckets).length === 0;
}
export function hasNoStacksWithDate(buckets) {
    return getStackItems(buckets, [ATTRIBUTE, DATE]).length === 0;
}
export function hasOneCategory(buckets) {
    return getAttributeItemsWithoutStacks(buckets, [ATTRIBUTE, DATE]).length === 1;
}
function isShowPercentageUnselected(buckets) {
    return !getBucketItems(buckets, BucketNames.MEASURES)?.[0]?.showInPercent;
}
export function noDerivedMeasurePresent(buckets) {
    const measures = getAllItemsByType(buckets, [METRIC]);
    return !measures.some((measure) => measure.masterLocalIdentifier);
}
function hasFirstDate(buckets) {
    const firstAttributeItem = getAllAttributeItems(buckets)?.[0];
    return firstAttributeItem && isDateBucketItem(firstAttributeItem);
}
function hasNotFirstDate(buckets) {
    return !hasFirstDate(buckets);
}
export function hasNonAllTimeFilter(filters) {
    const filterBucketItems = filters?.items ?? [];
    const dateFilter = filterBucketItems.find((filter) => {
        return filter?.attribute === "attr.datedataset";
    });
    const filterInterval = dateFilter?.filters?.[0]?.interval?.interval ?? [];
    return !isEmpty(filterInterval);
}
function hasNoRankingFilter(_, filters) {
    const allBucketFilters = filters?.items.flatMap((filterItem) => filterItem.filters);
    return !allBucketFilters?.some(isRankingFilter);
}
export function isShowInPercentAllowed(buckets, filters, bucketLocalIdentifier) {
    const rules = [hasNoStacks, hasSomeCategories, hasNoRankingFilter];
    return (allRulesMet(rules, buckets, filters) &&
        hasOneMasterMeasureInBucket(buckets, bucketLocalIdentifier) &&
        !filteredByDerivedMeasure(buckets, filters));
}
export function isComparisonOverTimeAllowed(buckets, filters) {
    return allRulesMet([hasNoStacksWithDate], buckets, filters) && hasGlobalDateFilter(filters);
}
export function overTimeComparisonRecommendationEnabled(referencePoint) {
    const rules = [
        noDerivedMeasurePresent,
        hasOneMeasure,
        hasFirstDate,
        hasNoStacksWithDate,
        hasOneCategory,
        hasNoMeasureDateFilter,
    ];
    return (allRulesMet(rules, referencePoint?.buckets ?? []) &&
        hasGlobalDateFilterIgnoreAllTime(referencePoint?.filters));
}
export function comparisonAndTrendingRecommendationEnabled(buckets) {
    const rules = [hasOneMeasure, noDerivedMeasurePresent, hasNoCategories];
    return allRulesMet(rules, buckets);
}
export function percentRecommendationEnabled(buckets, filters) {
    const rules = [
        isShowPercentageUnselected,
        hasNotFirstDate,
        hasOneMasterMeasure,
        hasOneCategory,
        hasNoStacks,
        hasNoRankingFilter,
    ];
    return allRulesMet(rules, buckets, filters) && !filteredByDerivedMeasure(buckets, filters);
}
export function previousPeriodRecommendationEnabled(buckets) {
    const rules = [
        hasOneMeasure,
        hasOneCategory,
        hasNotFirstDate,
        hasNoStacks,
        noDerivedMeasurePresent,
        isShowPercentageUnselected,
        hasNoMeasureDateFilter,
    ];
    return allRulesMet(rules, buckets);
}
export function canIncreasedTableMeasuresAddMoreItems(buckets) {
    const itemsCount = getItemsCount(buckets, BucketNames.MEASURES);
    const limit = hasNoColumns(buckets) ? INCREASE_MAX_TABLE_MEASURE_ITEMS_LIMIT : MAX_METRICS_COUNT;
    return itemsCount < limit;
}
export function canIncreasedTableAttributesAddMoreItems(buckets) {
    const itemsCount = getItemsCount(buckets, BucketNames.ATTRIBUTE);
    const limit = hasNoColumns(buckets)
        ? INCREASE_MAX_TABLE_ATTRIBUTES_ITEMS_LIMIT
        : MAX_TABLE_CATEGORIES_COUNT;
    return itemsCount < limit;
}
//# sourceMappingURL=bucketRules.js.map