// (C) 2022-2025 GoodData Corporation
import { useCallback, useEffect, useState } from "react";
import { useIntl } from "react-intl";
import { getAlertThreshold, getMeasureFormat } from "../utils/getters.js";
import { convertThresholdValue } from "../utils/threshold.js";
function clearValueForRelativeOperator(changeValue, setValue) {
    changeValue(undefined);
    setValue(undefined);
}
function calculateAndSetComparisonValue(getMetricValue, changeValue, setValue, measure, attribute, selectedValue) {
    const val = getMetricValue(measure, attribute, selectedValue);
    if (val === undefined) {
        return;
    }
    changeValue(val);
    setValue(convertThresholdValue(String(val), measure?.measure.format));
}
function parseInputValue(e) {
    return e === "" ? undefined : parseFloat(String(e));
}
export function useThresholdValue(changeValue, getMetricValue, isNewAlert, alert, selectedRelativeOperator = [undefined, undefined], selectedMeasure, selectedAttribute, selectedValue) {
    const intl = useIntl();
    const [relativeOperator] = selectedRelativeOperator;
    const [value, setValue] = useState(getAlertThreshold(alert));
    const [touched, setTouched] = useState(false);
    const [hasValidationError, setHasValidationError] = useState(false);
    useEffect(() => {
        // If input is already touched, do not trigger current value calculation
        // If alert is not new, do not trigger current value calculation because it is already set by user
        if (touched || !isNewAlert) {
            return;
        }
        // If user selects a relative operator, clear the value if not touched, it not make sense
        if (relativeOperator) {
            clearValueForRelativeOperator(changeValue, setValue);
            return;
        }
        // If user select comparison operator try to calculate the value based on the selected measure and attribute
        calculateAndSetComparisonValue(getMetricValue, changeValue, setValue, selectedMeasure?.measure, selectedAttribute?.attribute, selectedValue);
    }, [
        isNewAlert,
        changeValue,
        getMetricValue,
        selectedAttribute?.attribute,
        selectedMeasure?.measure,
        relativeOperator,
        selectedValue,
        touched,
    ]);
    const onChange = useCallback((e, event) => {
        const val = parseInputValue(e);
        if (val) {
            setHasValidationError(false);
        }
        changeValue(val);
        setValue(val);
        // Set touched state when user changes the value
        if (event) {
            setTouched(true);
        }
    }, [changeValue]);
    const onBlur = useCallback((event) => {
        const val = convertThresholdValue(event.target.value, getMeasureFormat(selectedMeasure?.measure));
        changeValue(val);
        setValue(val);
        setTouched(true);
        const isEmpty = val === undefined || val === null;
        setHasValidationError(isEmpty);
    }, [changeValue, selectedMeasure?.measure]);
    const showValidationError = touched && hasValidationError;
    const errorMessage = showValidationError
        ? intl.formatMessage({ id: "insightAlert.config.missingThreshold" })
        : undefined;
    return {
        value,
        onChange,
        onBlur,
        errorMessage,
    };
}
//# sourceMappingURL=useThresholdValue.js.map