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 { isSameDay } from "date-fns";
import React, { forwardRef, useRef, useState } from "react";
import { useId } from "../../utils-external/index.js";
import { cl } from "../../utils/helpers/index.js";
import { useControllableState, useMergeRefs } from "../../utils/hooks/index.js";
import { useI18n } from "../../utils/i18n/i18n.hooks.js";
import { DateDialog } from "../Date.Dialog.js";
import { DateInputContextProvider, DatePickerInput, } from "../Date.Input.js";
import { DateTranslationContextProvider, getTranslations, } from "../Date.locale.js";
import { isDateRange } from "../Date.typeutils.js";
import { ReactDayPicker } from "./parts/DatePicker.RDP.js";
import DatePickerStandalone from "./parts/DatePicker.Standalone.js";
/**
 * A component that allows users to select a date from a calendar.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/datepicker)
 * @see 🏷️ {@link DatePickerProps}
 *
 * @example
 * ```jsx
 *  const { inputProps, datepickerProps } = useMonthpicker({
 *    onMonthChange: console.log,
 *  });
 *
 *  return (
 *     <DatePicker {...datepickerProps} dropdownCaption>
 *       <DatePicker.Input
 *         {...inputProps}
 *         label="Velg dato"
 *       />
 *     </DatePicker>
 *  );
 * ```
 */
export const DatePicker = forwardRef((_a, ref) => {
    var { children, locale, translations, selected, id, defaultSelected, wrapperClassName, open: _open, onClose, onOpenToggle, strategy, mode } = _a, rest = __rest(_a, ["children", "locale", "translations", "selected", "id", "defaultSelected", "wrapperClassName", "open", "onClose", "onOpenToggle", "strategy", "mode"]);
    const translate = useI18n("DatePicker", translations, getTranslations(locale));
    const ariaId = useId(id);
    const popupLabelId = useId();
    const [open, setOpen] = useControllableState({
        defaultValue: false,
        value: _open,
    });
    const datePickerOpener = useRef(null);
    /* We use state here to insure that anchor is defined if open is true on initial render */
    const [wrapperRef, setWrapperRef] = useState(null);
    const mergedRef = useMergeRefs(setWrapperRef, ref);
    const [value, setValue] = useControllableState({
        defaultValue: defaultSelected,
        value: selected,
        onChange: (newValue) => {
            var _a;
            let closeDialog = false;
            if (mode === "single" && newValue) {
                closeDialog = true;
            }
            else if (isDateRange(newValue) && newValue.from && newValue.to) {
                closeDialog = true;
                if (isSameDay(newValue.from, newValue.to)) {
                    closeDialog = false;
                }
            }
            if (closeDialog) {
                onClose === null || onClose === void 0 ? void 0 : onClose();
                setOpen(false);
            }
            (_a = rest === null || rest === void 0 ? void 0 : rest.onSelect) === null || _a === void 0 ? void 0 : _a.call(rest, newValue);
        },
    });
    return (React.createElement(DateTranslationContextProvider, { translate: translate },
        React.createElement(DateInputContextProvider, { open: open, onOpen: (caller) => {
                datePickerOpener.current = caller !== null && caller !== void 0 ? caller : null;
                setOpen((x) => !x);
                onOpenToggle === null || onOpenToggle === void 0 ? void 0 : onOpenToggle();
            }, ariaId: ariaId, defined: true, caller: datePickerOpener.current },
            React.createElement("div", { ref: mergedRef, className: cl("aksel-date__wrapper", wrapperClassName) },
                children,
                React.createElement(DateDialog, { open: open, anchor: wrapperRef, onClose: () => {
                        onClose === null || onClose === void 0 ? void 0 : onClose();
                        open && setOpen(false);
                    }, locale: locale, translate: translate, variant: mode !== null && mode !== void 0 ? mode : "single", popoverProps: {
                        id: ariaId,
                        strategy,
                    }, popupLabelId: popupLabelId },
                    React.createElement(ReactDayPicker, Object.assign({}, rest, { locale: locale, handleSelect: setValue, selected: value, mode: mode, popupLabelId: popupLabelId })))))));
});
DatePicker.Standalone = DatePickerStandalone;
DatePicker.Input = DatePickerInput;
export default DatePicker;
export { DatePickerInput, DatePickerStandalone };
//# sourceMappingURL=DatePicker.js.map