"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fieldset = void 0;
const react_1 = __importStar(require("react"));
const typography_1 = require("../../typography");
const utils_external_1 = require("../../utils-external");
const helpers_1 = require("../../utils/helpers");
const ReadOnlyIcon_1 = require("../ReadOnlyIcon");
const context_1 = require("./context");
const useFieldset_1 = require("./useFieldset");
/**
 * 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>
 * ```
 */
exports.Fieldset = (0, react_1.forwardRef)((props, ref) => {
    var _a, _b, _c;
    const legendId = (0, utils_external_1.useId)();
    const { inputProps, errorId, showErrorMsg, hasError, size, readOnly, inputDescriptionId, readOnlyIconNeedsTitle, } = (0, useFieldset_1.useFieldset)(props, legendId);
    const fieldset = (0, react_1.useContext)(context_1.FieldsetContext);
    const { children, className, errorPropagation = true, legend, description, hideLegend } = props, rest = __rest(props, ["children", "className", "errorPropagation", "legend", "description", "hideLegend"]);
    return (react_1.default.createElement(context_1.FieldsetContext.Provider, { value: {
            error: errorPropagation
                ? ((_a = props.error) !== null && _a !== void 0 ? _a : fieldset === null || fieldset === void 0 ? void 0 : fieldset.error)
                : undefined,
            errorId: (0, helpers_1.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_1.default.createElement("fieldset", Object.assign({}, (0, utils_external_1.omit)(rest, ["errorId", "error", "size", "readOnly"]), inputProps, { ref: ref, className: (0, helpers_1.cl)(className, "aksel-fieldset", `aksel-fieldset--${size}`, {
                "aksel-fieldset--error": hasError,
                "aksel-fieldset--readonly": readOnly,
            }) }),
            react_1.default.createElement(typography_1.Label, { id: legendId, size: size, as: "legend", className: "aksel-fieldset__legend", visuallyHidden: !!hideLegend },
                readOnly &&
                    (readOnlyIconNeedsTitle ? (react_1.default.createElement(ReadOnlyIcon_1.ReadOnlyIconWithTitle, null)) : (react_1.default.createElement(ReadOnlyIcon_1.ReadOnlyIcon, null))),
                legend),
            !!description && (react_1.default.createElement(typography_1.BodyShort, { className: "aksel-fieldset__description", visuallyHidden: !!hideLegend, id: inputDescriptionId, size: size !== null && size !== void 0 ? size : "medium", as: "div" }, props.description)),
            children,
            react_1.default.createElement("div", { id: errorId, "aria-relevant": "additions removals", "aria-live": "polite", className: "aksel-fieldset__error" }, showErrorMsg && (react_1.default.createElement(typography_1.ErrorMessage, { size: size, showIcon: true }, props.error))))));
});
exports.default = exports.Fieldset;
//# sourceMappingURL=Fieldset.js.map