import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2019-2025 GoodData Corporation
import { useEffect, useState } from "react";
import { useIntl } from "react-intl";
import { Message } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../locales.js";
import { maxInputValidateAndPushData, minInputValidateAndPushData } from "../../utils/controlsHelper.js";
import { ConfigSubsection } from "../configurationControls/ConfigSubsection.js";
import { InputControl } from "../configurationControls/InputControl.js";
const defaultMinMaxControlState = {
    minScale: {
        hasWarning: false,
        warningMessage: "",
        incorrectValue: "",
    },
    maxScale: {
        hasWarning: false,
        warningMessage: "",
        incorrectValue: "",
    },
};
export function MinMaxControl(props) {
    const intl = useIntl();
    const [state, setState] = useState(defaultMinMaxControlState);
    // Handle getDerivedStateFromProps logic
    useEffect(() => {
        if (props.propertiesMeta?.undoApplied) {
            setState(defaultMinMaxControlState);
        }
    }, [props.propertiesMeta?.undoApplied]);
    const minInputValidateAndPushDataCallback = (data) => {
        minInputValidateAndPushData(data, state, { ...props, intl }, (newState) => setState((prevState) => ({ ...prevState, ...newState })), defaultMinMaxControlState);
    };
    const maxInputValidateAndPushDataCallback = (data) => {
        maxInputValidateAndPushData(data, state, { ...props, intl }, (newState) => setState((prevState) => ({ ...prevState, ...newState })), defaultMinMaxControlState);
    };
    const minScaleHasIncorrectValue = () => {
        return (state.minScale?.incorrectValue ?? "") !== "";
    };
    const maxScaleHasIncorrectValue = () => {
        return (state.maxScale?.incorrectValue ?? "") !== "";
    };
    const minScaleHasWarning = () => {
        return state.minScale?.hasWarning ?? false;
    };
    const maxScaleHasWarning = () => {
        return state.maxScale?.hasWarning ?? false;
    };
    const renderMinErrorMessage = () => {
        const minScaleWarningMessage = state.minScale?.warningMessage ?? "";
        if (!minScaleHasWarning() || minScaleWarningMessage === "") {
            return false;
        }
        return (_jsx(Message, { type: "warning", className: "adi-input-warning", children: minScaleWarningMessage }));
    };
    const renderMaxErrorMessage = () => {
        const maxScaleWarningMessage = state.maxScale?.warningMessage ?? "";
        if (!maxScaleHasWarning() || maxScaleWarningMessage === "") {
            return false;
        }
        return (_jsx(Message, { type: "warning", className: "adi-input-warning", children: maxScaleWarningMessage }));
    };
    const { properties, basePath, isDisabled } = props;
    const basePathPropertiesControls = properties?.controls?.[basePath];
    const axisScaleMin = basePathPropertiesControls?.min ?? "";
    const axisScaleMax = basePathPropertiesControls?.max ?? "";
    const axisVisible = basePathPropertiesControls?.visible ?? true;
    return (_jsxs(ConfigSubsection, { title: messages["axisScale"].id, children: [_jsx(InputControl, { valuePath: `${basePath}.min`, labelText: messages["axisMin"].id, placeholder: messages["autoPlaceholder"].id, type: "number", hasWarning: minScaleHasWarning(), value: minScaleHasIncorrectValue() ? state.minScale?.incorrectValue : axisScaleMin, disabled: isDisabled || !axisVisible, properties: properties, pushData: minInputValidateAndPushDataCallback }), renderMinErrorMessage(), _jsx(InputControl, { valuePath: `${basePath}.max`, labelText: messages["axisMax"].id, placeholder: messages["autoPlaceholder"].id, type: "number", hasWarning: maxScaleHasWarning(), value: maxScaleHasIncorrectValue() ? state.maxScale?.incorrectValue : axisScaleMax, disabled: isDisabled || !axisVisible, properties: properties, pushData: maxInputValidateAndPushDataCallback }), renderMaxErrorMessage()] }));
}
//# sourceMappingURL=MinMaxControl.js.map