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 } from "react";
import { ChevronDownIcon } from "@navikt/aksel-icons";
import { BodyShort, ErrorMessage, Label } from "../../typography/index.js";
import { omit } from "../../utils-external/index.js";
import { cl } from "../../utils/helpers/index.js";
import { ReadOnlyIconWithTitle } from "../ReadOnlyIcon.js";
import { useFormField } from "../useFormField.js";
/**
 * A component that displays a select input field.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/select)
 * @see 🏷️ {@link SelectProps}
 *
 * @example
 * ```jsx
 * <Select label="Hvilket land har du bosted i.">
 *   <option value="">Velg land</option>
 *   <option value="norge">Norge</option>
 *   <option value="sverige">Sverige</option>
 *   <option value="danmark">Danmark</option>
 * </Select>
 * ```
 */
export const Select = forwardRef((props, ref) => {
    const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, readOnly, } = useFormField(props, "select");
    const { children, label, className, description, hideLabel = false, style } = props, rest = __rest(props, ["children", "label", "className", "description", "hideLabel", "style"]);
    const readOnlyEventHandlers = {
        onMouseDown: (evt) => {
            // NOTE: does not prevent click
            if (readOnly) {
                evt.preventDefault();
                // focus on the element as per readonly input behavior
                evt.target.focus();
            }
        },
        onKeyDown: (evt) => {
            if (readOnly &&
                ["ArrowDown", "ArrowUp", "ArrowRight", "ArrowLeft", " "].includes(evt.key)) {
                evt.preventDefault();
            }
        },
    };
    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-select--error": hasError,
            "aksel-select--readonly": readOnly,
        }) },
        React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cl("aksel-form-field__label", {
                "aksel-sr-only": hideLabel,
            }) },
            readOnly && React.createElement(ReadOnlyIconWithTitle, 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("div", { className: "aksel-select__container", style: style },
            React.createElement("select", Object.assign({}, omit(rest, ["error", "errorId", "size", "readOnly"]), inputProps, readOnlyEventHandlers, { ref: ref, className: cl("aksel-select__input", "aksel-body-short", `aksel-body-short--${size !== null && size !== void 0 ? size : "medium"}`) }), children),
            React.createElement(ChevronDownIcon, { className: "aksel-select__chevron", "aria-hidden": true })),
        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 Select;
//# sourceMappingURL=Select.js.map