"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFormField = void 0;
exports.containsReadMore = containsReadMore;
const react_1 = __importStar(require("react"));
const ReadMore_1 = require("../read-more/ReadMore");
const utils_external_1 = require("../utils-external");
const helpers_1 = require("../utils/helpers");
const consoleWarning_1 = require("../utils/helpers/consoleWarning");
const context_1 = require("./fieldset/context");
/**
 * Handles props and their state for various form-fields in context with Fieldset
 */
const useFormField = (props, prefix) => {
    var _a, _b, _c;
    const { size, error, errorId: propErrorId } = props;
    const fieldset = (0, react_1.useContext)(context_1.FieldsetContext);
    const genId = (0, utils_external_1.useId)();
    const id = (_a = props.id) !== null && _a !== void 0 ? _a : `${prefix}-${genId}`;
    const errorId = propErrorId !== null && propErrorId !== void 0 ? propErrorId : `${prefix}-error-${genId}`;
    const inputDescriptionId = `${prefix}-description-${genId}`;
    const disabled = (fieldset === null || fieldset === void 0 ? void 0 : fieldset.disabled) || props.disabled;
    const readOnly = (((fieldset === null || fieldset === void 0 ? void 0 : fieldset.readOnly) || props.readOnly) && !disabled) || undefined;
    const hasError = !disabled && !readOnly && !!(error || (fieldset === null || fieldset === void 0 ? void 0 : fieldset.error));
    const showErrorMsg = !disabled && !readOnly && !!error && typeof error !== "boolean";
    const ariaInvalid = Object.assign({}, (hasError ? { "aria-invalid": true } : {}));
    if (props === null || props === void 0 ? void 0 : props.required) {
        (0, consoleWarning_1.consoleWarning)("Use of 'required' in form elements is heavily discouraged. Docs about why here:\nhttps://aksel.nav.no/god-praksis/artikler/obligatoriske-og-valgfrie-skjemafelter#dc7a536235fa");
    }
    return {
        showErrorMsg,
        hasError,
        errorId,
        inputDescriptionId,
        size: (_b = size !== null && size !== void 0 ? size : fieldset === null || fieldset === void 0 ? void 0 : fieldset.size) !== null && _b !== void 0 ? _b : "medium",
        readOnly,
        inputProps: Object.assign(Object.assign({ id }, ariaInvalid), { "aria-describedby": (0, helpers_1.cl)(props["aria-describedby"], {
                [inputDescriptionId]: props.description && !containsReadMore(props.description),
                [errorId]: showErrorMsg,
                [(_c = fieldset === null || fieldset === void 0 ? void 0 : fieldset.errorId) !== null && _c !== void 0 ? _c : ""]: hasError && (fieldset === null || fieldset === void 0 ? void 0 : fieldset.error),
            }) || undefined, disabled }),
    };
};
exports.useFormField = useFormField;
function containsReadMore(children, checkNested = true) {
    if (react_1.default.isValidElement(children)) {
        if (children.type === ReadMore_1.ReadMore) {
            return true;
        }
        if (children.props.children && checkNested) {
            return containsReadMore(children.props.children, false);
        }
    }
    else if (Array.isArray(children)) {
        return children.some((child) => containsReadMore(child, checkNested));
    }
    return false;
}
//# sourceMappingURL=useFormField.js.map