// (C) 2022-2025 GoodData Corporation
import { isEmpty } from "lodash-es";
import { areObjRefsEqual, insightMeasures, isDateFilter, isSimpleMeasure, measureFilters, } from "@gooddata/sdk-model";
import { sortDateDatasets, unrelatedHeader, } from "@gooddata/sdk-ui-kit";
const DATE_DROPDOWN_BODY_MARGIN = 6;
const UNRELATED_HEIGHT = 37;
const PADDING = 10;
const DROPDOWN_MAX_HEIGHT = 400;
export function getUnrelatedDateDataset(relatedDateDataSets, selectedDateDataSet, selectedDateDataSetHidden) {
    if (!selectedDateDataSet || selectedDateDataSetHidden) {
        return undefined;
    }
    const idx = relatedDateDataSets.findIndex((dateDataSet) => areObjRefsEqual(dateDataSet.dataSet.ref, selectedDateDataSet.dataSet.ref));
    return idx < 0 ? selectedDateDataSet : undefined;
}
function isDateFiltered(measure) {
    if (isSimpleMeasure(measure)) {
        const filters = measureFilters(measure);
        return !!filters?.some(isDateFilter);
    }
    return true;
}
export function getDateOptionsDisabledForInsight(insight) {
    const measures = insightMeasures(insight);
    return isEmpty(measures) ? false : measures.every(isDateFiltered);
}
export function removeDateFromTitle(title) {
    return title.trim().replace(/^Date \((.*)\)$/, "$1");
}
export function getAttributeByDisplayForm(attributes, displayForm) {
    return attributes.find((attribute) => areObjRefsEqual(attribute.ref, displayForm));
}
export function getUnrelatedDateDatasets(allDateDataSets, relatedDateDataSets) {
    if (!relatedDateDataSets) {
        return allDateDataSets;
    }
    return allDateDataSets.filter((unrelatedItem) => !relatedDateDataSets.some((relatedItem) => areObjRefsEqual(relatedItem.dataSet.ref, unrelatedItem.dataSet.ref)));
}
export function getDateConfigurationDropdownHeight(dropdownButtonTop, dropdownButtonHeight, dropdownBodyHeight, hasFooter = false) {
    const footerHeight = hasFooter ? UNRELATED_HEIGHT : 0;
    const dropdownHeight = 2 * DATE_DROPDOWN_BODY_MARGIN + PADDING + footerHeight;
    const nodeAboveHeight = dropdownButtonTop - dropdownHeight;
    const nodeBelowHeight = window.innerHeight - (dropdownButtonTop + dropdownButtonHeight + dropdownHeight);
    const maxHeight = Math.max(nodeAboveHeight, nodeBelowHeight);
    return Math.min(dropdownBodyHeight, maxHeight, DROPDOWN_MAX_HEIGHT);
}
function catalogDateDatasetToDateDataset(ds) {
    return {
        id: ds.dataSet.id,
        title: ds.dataSet.title,
        relevance: ds.relevance,
    };
}
export function getSortedDateDatasetsItems(relatedDateDatasets, recommendedDateDataSet, unrelatedDateDataset, unrelatedDateDatasets, shouldDisplayUnrelatedDatasets = false) {
    const sortedUnrelatedDateDataset = sortDateDatasets((unrelatedDateDatasets || []).map(catalogDateDatasetToDateDataset));
    const withUnrelatedHeader = [unrelatedHeader, ...sortedUnrelatedDateDataset];
    if (shouldDisplayUnrelatedDatasets) {
        return [
            ...sortDateDatasets(relatedDateDatasets.map(catalogDateDatasetToDateDataset), recommendedDateDataSet ? catalogDateDatasetToDateDataset(recommendedDateDataSet) : undefined),
            ...withUnrelatedHeader,
        ];
    }
    return sortDateDatasets(relatedDateDatasets.map(catalogDateDatasetToDateDataset), recommendedDateDataSet ? catalogDateDatasetToDateDataset(recommendedDateDataSet) : undefined, unrelatedDateDataset ? catalogDateDatasetToDateDataset(unrelatedDateDataset) : undefined);
}
//# sourceMappingURL=utils.js.map