import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useState } from "react";
import { FormattedMessage, defineMessages } from "react-intl";
import { isValidStringParameterValue } from "@gooddata/sdk-model";
import { ParameterControlDropdown } from "./ParameterControlDropdown.js";
const messages = defineMessages({
    errorTooShort: { id: "parameter_filter.dropdown.error.tooShort" },
    errorTooLong: { id: "parameter_filter.dropdown.error.tooLong" },
});
/**
 * Dropdown panel for editing a string parameter value as free text. Owns the draft, inline
 * length validation, preview, and (mode-aware) Reset via `resetValue`.
 *
 * @internal
 */
export function StringParameterControlDropdown({ name, value, resetValue, constraints, inputId, ariaAttributes, onApply, onCancel, }) {
    const [draft, setDraft] = useState(value);
    const error = getStringDraftValidationError(draft, constraints);
    const effectiveValue = error ? value : draft;
    const showReset = resetValue !== undefined && effectiveValue !== resetValue;
    return (_jsx(ParameterControlDropdown, { name: name, draft: draft, onDraftChange: setDraft, inputProps: { type: "text" }, inputId: inputId, ariaAttributes: ariaAttributes, errorMessage: error ? (_jsx(FormattedMessage, { ...error, values: { minLength: constraints?.minLength, maxLength: constraints?.maxLength } })) : undefined, previewValue: effectiveValue, onReset: showReset ? () => setDraft(resetValue) : undefined, onApply: () => onApply(draft), onCancel: onCancel }));
}
function getStringDraftValidationError(draft, constraints) {
    if (isValidStringParameterValue(draft, constraints)) {
        return undefined;
    }
    const { minLength } = constraints ?? {};
    return minLength !== undefined && draft.length < minLength
        ? messages.errorTooShort
        : messages.errorTooLong;
}
