import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useCallback } from "react";
import { useIntl } from "react-intl";
import { ValidationContextStore, createInvalidDatapoint, createInvalidNode, useValidationContextValue, } from "@gooddata/sdk-ui";
import { Input, useIdPrefixed } from "@gooddata/sdk-ui-kit";
import { DASHBOARD_TITLE_MAX_LENGTH } from "../../../../../presentation/constants/dashboard.js";
import { ErrorWrapper } from "../ErrorWrapper/ErrorWrapper.js";
const MAX_SUBJECT_LENGTH = 255;
export function SubjectForm({ dashboardTitle, editedAutomation, isSubmitDisabled, onChange, onKeyDownSubmit, }) {
    const { formatMessage } = useIntl();
    const labelId = useIdPrefixed("label");
    const errorId = useIdPrefixed("error");
    const validationContextValue = useValidationContextValue(createInvalidNode({ id: "SubjectForm" }));
    const { setInvalidDatapoints, getInvalidDatapoints, isValid } = validationContextValue;
    const invalidDatapoint = getInvalidDatapoints()[0];
    const setHasError = useCallback((hasError) => {
        if (!hasError) {
            setInvalidDatapoints(() => []);
            return;
        }
        setInvalidDatapoints(() => [
            createInvalidDatapoint({
                id: errorId,
                message: formatMessage({ id: "dialogs.schedule.error.too_long" }, { value: MAX_SUBJECT_LENGTH }),
            }),
        ]);
    }, [errorId, formatMessage, setInvalidDatapoints]);
    const isValueValid = useCallback((value) => {
        return value.length <= MAX_SUBJECT_LENGTH;
    }, []);
    const handleOnChange = useCallback((value) => {
        const validationResult = isValueValid(value);
        if (!isValid) {
            setHasError(!validationResult);
        }
        onChange(value, validationResult);
    }, [isValueValid, isValid, onChange, setHasError]);
    const handleOnBlur = useCallback((e) => {
        setHasError(!isValueValid(e.target.value));
    }, [isValueValid, setHasError]);
    return (_jsx(ValidationContextStore, { value: validationContextValue, children: _jsx(ErrorWrapper, { errorId: errorId, errorMessage: invalidDatapoint?.message ?? null, label: formatMessage({ id: "dialogs.schedule.email.subject.label" }), labelId: labelId, className: "gd-input-component gd-notifications-channels-dialog-subject s-gd-notifications-channels-dialog-subject", labelWrapperClassName: "gd-notifications-channels-dialog-subject-wrapper", errorClassName: "gd-notifications-channels-dialog-subject-error", children: _jsx(Input, { id: labelId, hasError: !isValid, maxlength: 300, type: "text", placeholder: dashboardTitle.length > DASHBOARD_TITLE_MAX_LENGTH
                    ? dashboardTitle.substring(0, DASHBOARD_TITLE_MAX_LENGTH)
                    : dashboardTitle, value: editedAutomation.details?.subject ?? "", onChange: 
                // as any, the value will indeed always be string
                // TODO improve typings of Input in ui-kit to have properly typed the onChange related to the input type
                handleOnChange, onEnterKeyPress: () => {
                    if (!isSubmitDisabled) {
                        onKeyDownSubmit();
                    }
                }, autocomplete: "off", onBlur: handleOnBlur, accessibilityConfig: {
                    ariaDescribedBy: isValid ? undefined : errorId,
                } }) }) }));
}
//# sourceMappingURL=SubjectForm.js.map