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, useContext, } from "react";
import { BodyShort, ErrorMessage, Label } from "../../typography/index.js";
import { omit, useId } from "../../utils-external/index.js";
import { cl } from "../../utils/helpers/index.js";
import { ReadOnlyIcon, ReadOnlyIconWithTitle } from "../ReadOnlyIcon.js";
import { FieldsetContext } from "./context.js";
import { useFieldset } from "./useFieldset.js";
/**
 * Component for grouping form fields.
 *
 * **NB: Only for special use cases.** Form fields should not be grouped by default,
 * except for checkboxes and radio buttons, for which CheckboxGroup/RadioGroup should be used instead.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/fieldset)
 * @see 🏷️ {@link FieldsetProps}
 *
 * @example
 * ```jsx
 * <Fieldset legend="Telefonnummer">
 *   <TextField label="Landkode" />
 *   <TextField label="Nummer" />
 * </Fieldset>
 * ```
 */
export const Fieldset = forwardRef((props, ref) => {
    var _a, _b, _c;
    const legendId = useId();
    const { inputProps, errorId, showErrorMsg, hasError, size, readOnly, inputDescriptionId, readOnlyIconNeedsTitle, } = useFieldset(props, legendId);
    const fieldset = useContext(FieldsetContext);
    const { children, className, errorPropagation = true, legend, description, hideLegend } = props, rest = __rest(props, ["children", "className", "errorPropagation", "legend", "description", "hideLegend"]);
    return (React.createElement(FieldsetContext.Provider, { value: {
            error: errorPropagation
                ? ((_a = props.error) !== null && _a !== void 0 ? _a : fieldset === null || fieldset === void 0 ? void 0 : fieldset.error)
                : undefined,
            errorId: cl({
                [errorId]: showErrorMsg,
                [(_b = fieldset === null || fieldset === void 0 ? void 0 : fieldset.errorId) !== null && _b !== void 0 ? _b : ""]: !!(fieldset === null || fieldset === void 0 ? void 0 : fieldset.error),
            }),
            size,
            disabled: (_c = props.disabled) !== null && _c !== void 0 ? _c : false,
            readOnly,
        } },
        React.createElement("fieldset", Object.assign({}, omit(rest, ["errorId", "error", "size", "readOnly"]), inputProps, { ref: ref, className: cl(className, "aksel-fieldset", `aksel-fieldset--${size}`, {
                "aksel-fieldset--error": hasError,
                "aksel-fieldset--readonly": readOnly,
            }) }),
            React.createElement(Label, { id: legendId, size: size, as: "legend", className: "aksel-fieldset__legend", visuallyHidden: !!hideLegend },
                readOnly &&
                    (readOnlyIconNeedsTitle ? (React.createElement(ReadOnlyIconWithTitle, null)) : (React.createElement(ReadOnlyIcon, null))),
                legend),
            !!description && (React.createElement(BodyShort, { className: "aksel-fieldset__description", visuallyHidden: !!hideLegend, id: inputDescriptionId, size: size !== null && size !== void 0 ? size : "medium", as: "div" }, props.description)),
            children,
            React.createElement("div", { id: errorId, "aria-relevant": "additions removals", "aria-live": "polite", className: "aksel-fieldset__error" }, showErrorMsg && (React.createElement(ErrorMessage, { size: size, showIcon: true }, props.error))))));
});
export default Fieldset;
//# sourceMappingURL=Fieldset.js.map