import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import cx from "classnames";
import { FormattedMessage, useIntl } from "react-intl";
import { isInsightWidget } from "@gooddata/sdk-model";
import { ShortenedText } from "@gooddata/sdk-ui-kit";
import { useCurrentDateFilterConfig } from "../../../dragAndDrop/useCurrentDateFilterConfig.js";
import { getUnrelatedDateDataset } from "./utils.js";
const tooltipAlignPoints = [{ align: "cl cr", offset: { x: -20, y: 0 } }];
export function DateFilterCheckbox({ isDropdownLoading, isFilterLoading, dateFilterEnabled, selectedDateDataset, selectedDateDatasetHidden, dateFilterCheckboxDisabled, relatedDateDatasets, widget, onDateDatasetFilterEnabled, enableUnrelatedItemsVisibility, }) {
    const unrelatedDateDataset = relatedDateDatasets &&
        getUnrelatedDateDataset(relatedDateDatasets, selectedDateDataset, selectedDateDatasetHidden);
    const hasRelatedDateDataSets = !!relatedDateDatasets?.length;
    const showNoRelatedDate = !hasRelatedDateDataSets &&
        !selectedDateDatasetHidden &&
        dateFilterEnabled &&
        !isDropdownLoading &&
        !dateFilterCheckboxDisabled &&
        !enableUnrelatedItemsVisibility;
    const showError = (!!unrelatedDateDataset || showNoRelatedDate) &&
        !isDropdownLoading &&
        !isFilterLoading &&
        dateFilterEnabled &&
        !selectedDateDatasetHidden &&
        !dateFilterCheckboxDisabled;
    const classes = cx("s-date-filter-by-item", "input-checkbox-label", "filter-by-item", {
        "date-filter-error": showError,
    });
    const handleChange = useCallback((e) => {
        const { checked } = e.target;
        const dateDataSetRef = selectedDateDataset ? selectedDateDataset.dataSet.ref : undefined;
        // convert to non-immutable
        onDateDatasetFilterEnabled(checked, dateDataSetRef);
    }, [onDateDatasetFilterEnabled, selectedDateDataset]);
    const intl = useIntl();
    const defaultDateFilterName = intl.formatMessage({ id: "dateFilterDropdown.title" });
    const { title } = useCurrentDateFilterConfig(undefined, defaultDateFilterName);
    return (_jsxs("div", { children: [
            _jsxs("label", { className: classes, htmlFor: "configurationPanel.date.input", children: [
                    _jsx("input", { className: "input-checkbox s-date-filter-checkbox", id: "configurationPanel.date.input", type: "checkbox", checked: !dateFilterCheckboxDisabled && dateFilterEnabled, disabled: dateFilterCheckboxDisabled, onChange: handleChange }), _jsx("span", { className: "input-label-text title", children: _jsx(ShortenedText, { tooltipAlignPoints: tooltipAlignPoints, tagName: "span", className: "title", children: title }) }), isFilterLoading ? _jsx("div", { className: "gd-spinner small" }) : null] }), !isFilterLoading && showNoRelatedDate ? (_jsx("div", { className: "gd-message error s-no-related-date", children: isInsightWidget(widget) ? (_jsx(FormattedMessage, { id: "configurationPanel.vizCantBeFilteredByDate" })) : (_jsx(FormattedMessage, { id: "configurationPanel.kpiCantBeFilteredByDate" })) })) : null] }));
}
