var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { forwardRef, useState } from "react";
import { BodyShort, ErrorMessage, Label } from "../../typography/index.js";
import { omit, useId } from "../../utils-external/index.js";
import TextareaAutosize from "../../utils/components/textarea-autosize/TextareaAutoSize.js";
import { cl, composeEventHandlers } from "../../utils/helpers/index.js";
import { ReadOnlyIcon } from "../ReadOnlyIcon.js";
import { useFormField } from "./../useFormField.js";
import Counter from "./TextareaCounter.js";
/**
 * A component that displays a textarea input field with a label.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/textarea)
 * @see 🏷️ {@link TextareaProps}
 *
 * @example
 * ```jsx
 * <Textarea label="Har du noen tilbakemeldinger?" />
 * ```
 */
export const Textarea = forwardRef((props, ref) => {
    var _a, _b, _c;
    const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, } = useFormField(props, "textarea");
    const { label, className, description, maxLength, hideLabel = false, resize, UNSAFE_autoScrollbar, i18n, readOnly } = props, rest = __rest(props, ["label", "className", "description", "maxLength", "hideLabel", "resize", "UNSAFE_autoScrollbar", "i18n", "readOnly"]);
    const maxLengthId = useId();
    const hasMaxLength = maxLength !== undefined && maxLength > 0;
    const [uncontrolledValue, setUncontrolledValue] = useState((_a = props === null || props === void 0 ? void 0 : props.defaultValue) !== null && _a !== void 0 ? _a : "");
    const describedBy = cl(inputProps["aria-describedby"], {
        [maxLengthId !== null && maxLengthId !== void 0 ? maxLengthId : ""]: hasMaxLength,
    });
    return (React.createElement("div", { className: cl(className, "aksel-form-field", `aksel-form-field--${size}`, {
            "aksel-form-field--disabled": !!inputProps.disabled,
            "aksel-form-field--readonly": readOnly,
            "aksel-textarea--readonly": readOnly,
            "aksel-textarea--error": hasError,
            "aksel-textarea--autoscrollbar": UNSAFE_autoScrollbar,
            [`aksel-textarea--resize-${resize === true ? "both" : resize}`]: resize,
        }) },
        React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cl("aksel-form-field__label", {
                "aksel-sr-only": hideLabel,
            }) },
            readOnly && React.createElement(ReadOnlyIcon, null),
            label),
        !!description && (React.createElement(BodyShort, { className: cl("aksel-form-field__description", {
                "aksel-sr-only": hideLabel,
            }), id: inputDescriptionId, size: size, as: "div" }, description)),
        React.createElement(TextareaAutosize, Object.assign({}, omit(rest, ["error", "errorId", "size"]), inputProps, { onChange: composeEventHandlers(props.onChange, props.value === undefined
                ? (e) => setUncontrolledValue(e.target.value)
                : undefined), minRows: rest.minRows || (size === "small" ? 2 : 3), autoScrollbar: UNSAFE_autoScrollbar, ref: ref, readOnly: readOnly, className: cl("aksel-textarea__input", "aksel-body-short", `aksel-body-short--${size !== null && size !== void 0 ? size : "medium"}`) }, (describedBy ? { "aria-describedby": describedBy } : {}))),
        hasMaxLength && !readOnly && !inputProps.disabled && (React.createElement(Counter, { maxLengthId: maxLengthId, maxLength: maxLength, currentLength: (_c = (_b = props.value) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : uncontrolledValue.length, size: size, i18n: i18n })),
        React.createElement("div", { className: "aksel-form-field__error", id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && (React.createElement(ErrorMessage, { size: size, showIcon: true }, props.error)))));
});
export default Textarea;
//# sourceMappingURL=Textarea.js.map