import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useMemo } from "react";
import cx from "classnames";
import { useIntl } from "react-intl";
import { DateGranularity, } from "@gooddata/sdk-model";
import { Dropdown, DropdownButton, SingleSelectListItem, UiListbox, } from "@gooddata/sdk-ui-kit";
import { AlertMetricComparatorType } from "../../types.js";
import { translateGranularity } from "../utils/granularity.js";
import { isChangeOrDifferenceOperator } from "../utils/guards.js";
export function AlertComparisonPeriodSelect({ alert, measure, overlayPositionType, selectedComparison, selectedGranularity, canManageComparison, onComparisonChange, id, closeOnParentScroll, }) {
    const intl = useIntl();
    const selectedOperator = useMemo(() => {
        return measure?.comparators.find((a) => a.comparator === selectedComparison &&
            (selectedGranularity ? selectedGranularity === a.granularity : true));
    }, [measure?.comparators, selectedComparison, selectedGranularity]);
    const comparisons = useMemo(() => {
        const sps = measure?.comparators.filter((a) => a.comparator === AlertMetricComparatorType.SamePeriodPreviousYear) ?? [];
        const pps = measure?.comparators.filter((a) => a.comparator === AlertMetricComparatorType.PreviousPeriod) ??
            [];
        const items = [];
        // Iterate over SamePeriodPreviousYear comparators
        sps.forEach((sp) => {
            const pp = pps.find((pp) => pp.granularity === sp.granularity);
            // Has granularity set and previous period is not year
            if (sp.granularity) {
                if (pp?.granularity === DateGranularity["year"]) {
                    items.push({
                        title: intl.formatMessage({ id: "insightAlert.config.compare_with_sp" }),
                        type: AlertMetricComparatorType.SamePeriodPreviousYear,
                        granularity: sp.granularity,
                    });
                }
                else {
                    items.push({
                        title: intl.formatMessage({ id: "insightAlert.config.compare_with_sp_granularity" }, {
                            period: translateGranularity(intl, sp.granularity),
                        }),
                        type: AlertMetricComparatorType.SamePeriodPreviousYear,
                        granularity: sp.granularity,
                    });
                }
            }
            else {
                items.push({
                    title: intl.formatMessage({ id: "insightAlert.config.compare_with_sp" }),
                    type: AlertMetricComparatorType.SamePeriodPreviousYear,
                    granularity: undefined,
                });
            }
        });
        // Iterate over PreviousPeriod comparators
        pps.forEach((pp) => {
            // Previous period has granularity set
            if (pp.granularity) {
                items.push({
                    title: intl.formatMessage({ id: "insightAlert.config.compare_with_pp_granularity" }, {
                        period: translateGranularity(intl, pp.granularity),
                    }),
                    type: AlertMetricComparatorType.PreviousPeriod,
                    granularity: pp.granularity,
                });
            }
            else {
                items.push({
                    title: intl.formatMessage({ id: "insightAlert.config.compare_with_pp" }),
                    type: AlertMetricComparatorType.PreviousPeriod,
                    granularity: undefined,
                });
            }
        });
        return items;
    }, [intl, measure?.comparators]);
    // If alert is not defined or the measure does not have any comparators, return null
    if (!alert || !isChangeOrDifferenceOperator(alert.alert)) {
        return null;
    }
    // If the measure has only one comparator, return the label with possibility to
    // select the comparison period
    // If feature flag is not enabled, return the label with the selected comparison period
    if (measure?.comparators.length === 1 || !canManageComparison) {
        return _jsx("div", { className: "gd-edit-alert__measure-info", children: getButtonValue(selectedOperator, intl) });
    }
    return (_jsx(Dropdown, { closeOnParentScroll: closeOnParentScroll, overlayPositionType: overlayPositionType, autofocusOnOpen: true, renderButton: ({ isOpen, toggleDropdown, buttonRef, dropdownId }) => {
            return (_jsx(DropdownButton, { id: id, value: getButtonValue(selectedOperator, intl), onClick: toggleDropdown, className: cx("gd-alert-comparison-select__button s-alert-comparison-select", "button-dropdown", "dropdown-button", {
                    "gd-alert-comparison-select__button--open": isOpen,
                    "is-active": isOpen,
                }), buttonRef: buttonRef, dropdownId: dropdownId, isOpen: isOpen, accessibilityConfig: {
                    ariaExpanded: isOpen,
                    popupType: "listbox",
                } }));
        }, renderBody: ({ closeDropdown, ariaAttributes }) => {
            const listboxItems = comparisons.map((comparison) => ({
                type: "interactive",
                id: `${comparison.type}-${comparison.granularity ?? "none"}`,
                stringTitle: comparison.title,
                data: comparison,
            }));
            return (_jsx(UiListbox, { shouldKeyboardActionStopPropagation: true, shouldKeyboardActionPreventDefault: true, dataTestId: "s-alert-comparison-select-list", items: listboxItems, selectedItemId: `${selectedComparison ?? "none"}-${selectedGranularity ?? "none"}`, onSelect: (item) => {
                    if (item.data.type !== selectedComparison ||
                        item.data.granularity !== selectedGranularity) {
                        onComparisonChange(item.data.type, item.data.granularity);
                    }
                }, onClose: closeDropdown, ariaAttributes: ariaAttributes, InteractiveItemComponent: ({ item, isSelected, onSelect, isFocused }) => {
                    return (_jsx(SingleSelectListItem, { title: item.stringTitle, isSelected: isSelected, isFocused: isFocused, onClick: onSelect, className: "gd-alert-comparison-select__list-item" }));
                } }));
        } }));
}
function getButtonValue(selectedOperator, intl) {
    if (selectedOperator?.comparator === AlertMetricComparatorType.SamePeriodPreviousYear) {
        if (selectedOperator.granularity && selectedOperator.granularity !== DateGranularity["year"]) {
            return intl.formatMessage({ id: "insightAlert.config.compare_with_sp_granularity" }, { period: translateGranularity(intl, selectedOperator.granularity) });
        }
        else {
            return intl.formatMessage({ id: "insightAlert.config.compare_with_sp" });
        }
    }
    if (selectedOperator?.comparator === AlertMetricComparatorType.PreviousPeriod) {
        if (selectedOperator.granularity) {
            return intl.formatMessage({ id: "insightAlert.config.compare_with_pp_granularity" }, { period: translateGranularity(intl, selectedOperator.granularity) });
        }
        else {
            return intl.formatMessage({ id: "insightAlert.config.compare_with_pp" });
        }
    }
    return " - ";
}
