import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useState } from "react";
import { FormattedMessage, defineMessages } from "react-intl";
import { isValidNumberParameterValue } from "@gooddata/sdk-model";
import { ParameterControlDropdown } from "./ParameterControlDropdown.js";
const messages = defineMessages({
    errorNotANumber: { id: "parameter_filter.dropdown.error.notANumber" },
    errorOutOfRange: { id: "parameter_filter.dropdown.error.outOfRange" },
    errorOutOfRangeMin: { id: "parameter_filter.dropdown.error.outOfRange.min" },
    errorOutOfRangeMax: { id: "parameter_filter.dropdown.error.outOfRange.max" },
});
/**
 * Dropdown panel for editing a numeric parameter value. Owns the draft, inline validation,
 * preview, and (mode-aware) Reset via `resetValue`.
 *
 * @internal
 */
export function NumberParameterControlDropdown({ name, value, resetValue, constraints, inputId, ariaAttributes, onApply, onCancel, }) {
    const [draft, setDraft] = useState(String(value));
    const error = getDraftValidationError(draft, constraints);
    const effectiveValue = error ? value : parseDraft(draft);
    const showReset = resetValue !== undefined && effectiveValue !== resetValue;
    return (_jsx(ParameterControlDropdown, { name: name, draft: draft, onDraftChange: setDraft, inputProps: { type: "number", min: constraints?.min, max: constraints?.max }, inputId: inputId, ariaAttributes: ariaAttributes, errorMessage: error ? (_jsx(FormattedMessage, { ...error, values: { min: constraints?.min, max: constraints?.max } })) : undefined, previewValue: effectiveValue, onReset: showReset ? () => setDraft(String(resetValue)) : undefined, onApply: () => onApply(parseDraft(draft)), onCancel: onCancel }));
}
/**
 * Returns the message to show for an invalid draft, or `undefined` when the draft is a valid
 * in-range number. The single source of truth the dropdown derives its error row, input style,
 * and Apply-disabled state from.
 *
 * @internal
 */
export function getDraftValidationError(draft, constraints) {
    const value = parseDraft(draft);
    if (!Number.isFinite(value)) {
        return messages.errorNotANumber;
    }
    if (isValidNumberParameterValue(value, constraints)) {
        return undefined;
    }
    const { min, max } = constraints ?? {};
    if (min !== undefined && max === undefined) {
        return messages.errorOutOfRangeMin;
    }
    if (max !== undefined && min === undefined) {
        return messages.errorOutOfRangeMax;
    }
    return messages.errorOutOfRange;
}
function parseDraft(draft) {
    return draft.trim() === "" ? Number.NaN : Number(draft);
}
