import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback, useRef } from "react";
import { useIntl } from "react-intl";
import { ValidationContextStore, createInvalidDatapoint, createInvalidNode, useValidationContextValue, } from "@gooddata/sdk-ui";
import { useIdPrefixed } from "@gooddata/sdk-ui-kit";
import { ErrorWrapper } from "../ErrorWrapper/ErrorWrapper.js";
import { Textarea } from "../Textarea.js";
const MAX_MESSAGE_LENGTH = 10000;
export function MessageForm({ value, onChange }) {
    const { formatMessage } = useIntl();
    const textareaRef = useRef(null);
    const validationContextValue = useValidationContextValue(createInvalidNode({ id: "MessageForm" }));
    const { setInvalidDatapoints, getInvalidDatapoints, isValid } = validationContextValue;
    const invalidDatapoint = getInvalidDatapoints()[0];
    const labelId = useIdPrefixed("label");
    const errorId = useIdPrefixed("error");
    const setHasError = useCallback((hasError) => {
        if (!hasError) {
            setInvalidDatapoints(() => []);
            return;
        }
        setInvalidDatapoints(() => [
            createInvalidDatapoint({
                id: errorId,
                message: formatMessage({ id: "dialogs.schedule.error.too_long" }, { value: MAX_MESSAGE_LENGTH }),
            }),
        ]);
    }, [errorId, formatMessage, setInvalidDatapoints]);
    const isValueValid = useCallback((value) => {
        return value.length <= MAX_MESSAGE_LENGTH;
    }, []);
    const handleOnTitleChange = useCallback((value) => {
        const validationResult = isValueValid(value);
        if (!isValid) {
            setHasError(!validationResult);
        }
        onChange(value, validationResult);
    }, [isValueValid, isValid, onChange, setHasError]);
    const handleFocus = useCallback((event) => {
        if (value !== "") {
            event.target.select(); // Selects all text on focus
        }
    }, [value]);
    const handleOnBlur = useCallback((value) => {
        setHasError(!isValueValid(value));
    }, [setHasError, isValueValid]);
    return (_jsx(ValidationContextStore, { value: validationContextValue, children: _jsx(ErrorWrapper, { errorId: errorId, errorMessage: invalidDatapoint?.message ?? null, label: formatMessage({ id: "dialogs.schedule.email.message.label" }), labelId: labelId, className: "gd-input-component gd-textarea-component gd-notifications-channels-dialog-message s-gd-notifications-channels-dialog-message", labelWrapperClassName: "gd-notifications-channels-dialog-message-content", errorClassName: "gd-notifications-channels-dialog-message-error", children: _jsx(Textarea, { id: labelId, ref: textareaRef, autocomplete: "off", placeholder: formatMessage({
                    id: "dialogs.schedule.email.message.placeholder",
                }), onFocus: handleFocus, rows: 3, onChange: handleOnTitleChange, value: value, onBlur: handleOnBlur, validationError: invalidDatapoint?.message ?? null, hasError: !isValid, accessibilityConfig: {
                    ariaDescribedBy: isValid ? undefined : errorId,
                } }) }) }));
}
//# sourceMappingURL=MessageForm.js.map