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 { CalendarIcon } from "@navikt/aksel-icons";
import { Button } from "../button/index.js";
import { ReadOnlyIcon } from "../form/ReadOnlyIcon.js";
import { useFormField } from "../form/useFormField.js";
import { BodyShort, ErrorMessage, Label } from "../typography/index.js";
import { omit } from "../utils-external/index.js";
import { cl, createStrictContext } from "../utils/helpers/index.js";
import { useDateTranslationContext } from "./Date.locale.js";
export const { Provider: DateInputContextProvider, useContext: useDateInputContext, } = createStrictContext({
    name: "DateInputContext",
    errorMessage: "useDateInputContext must be used with DateInputContext",
});
const DateInput = forwardRef((props, ref) => {
    const { className, hideLabel = false, label, description, variant = "datepicker", setAnchorRef, "data-color": dataColor, callerName } = props, rest = __rest(props, ["className", "hideLabel", "label", "description", "variant", "setAnchorRef", "data-color", "callerName"]);
    const translate = useDateTranslationContext().translate;
    const isDatepickerVariant = variant === "datepicker";
    const conditionalVariables = {
        prefix: isDatepickerVariant ? "datepicker-input" : "monthpicker-input",
        iconTitle: {
            open: isDatepickerVariant ? "openDatePicker" : "openMonthPicker",
            close: isDatepickerVariant ? "closeDatePicker" : "closeMonthPicker",
        },
    };
    const context = useDateInputContext();
    const { inputProps, size = "medium", inputDescriptionId, errorId, showErrorMsg, hasError, readOnly, } = useFormField(props, conditionalVariables.prefix);
    return (React.createElement("div", { className: cl(className, "aksel-form-field", `aksel-form-field--${size}`, "aksel-date__field", {
            "aksel-text-field--error": hasError,
            "aksel-date__field--error": hasError,
            "aksel-form-field--disabled": !!inputProps.disabled,
            "aksel-text-field--disabled": !!inputProps.disabled,
            "aksel-form-field--readonly": readOnly,
            "aksel-text-field--readonly": readOnly,
            "aksel-date__field--readonly": readOnly,
        }), "data-color": dataColor },
        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, { as: "div", className: cl("aksel-form-field__description", {
                "aksel-sr-only": hideLabel,
            }), id: inputDescriptionId, size: size }, description)),
        React.createElement("div", { className: "aksel-date__field-wrapper" },
            React.createElement("input", Object.assign({ ref: ref }, omit(rest, ["error", "errorId", "size"]), inputProps, { autoComplete: "off", "aria-controls": context.open ? context.ariaId : undefined, readOnly: readOnly, className: cl("aksel-date__field-input", "aksel-text-field__input", "aksel-body-short", `aksel-body-short--${size}`), size: isDatepickerVariant ? 11 : 14 })),
            React.createElement(Button, { disabled: inputProps.disabled || readOnly, tabIndex: readOnly ? -1 : undefined, onClick: () => {
                    context.onOpen(callerName);
                }, type: "button", className: "aksel-date__field-button", ref: setAnchorRef, icon: React.createElement(CalendarIcon, { "aria-hidden": true }), 
                // If we have the title on the icon, NVDA will read "close" when focusing
                // the button after closing the popup, even if we wait a tick.
                title: translate(conditionalVariables.iconTitle[context.open ? "close" : "open"]), variant: "secondary", size: size })),
        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 const DatePickerInput = forwardRef((props, ref) => React.createElement(DateInput, Object.assign({}, props, { ref: ref })));
export const MonthPickerInput = forwardRef((props, ref) => React.createElement(DateInput, Object.assign({}, props, { variant: "monthpicker", ref: ref })));
//# sourceMappingURL=Date.Input.js.map