"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.MonthPickerInput = exports.MonthPickerStandalone = exports.MonthPicker = void 0;
const react_1 = __importStar(require("react"));
const utils_external_1 = require("../../utils-external");
const helpers_1 = require("../../utils/helpers");
const consoleWarning_1 = require("../../utils/helpers/consoleWarning");
const hooks_1 = require("../../utils/hooks");
const i18n_hooks_1 = require("../../utils/i18n/i18n.hooks");
const Date_Dialog_1 = require("../Date.Dialog");
const Date_Input_1 = require("../Date.Input");
Object.defineProperty(exports, "MonthPickerInput", { enumerable: true, get: function () { return Date_Input_1.MonthPickerInput; } });
const Date_locale_1 = require("../Date.locale");
const MonthPicker_context_1 = require("./MonthPicker.context");
const MonthPicker_Caption_1 = require("./parts/MonthPicker.Caption");
const MonthPicker_Standalone_1 = require("./parts/MonthPicker.Standalone");
Object.defineProperty(exports, "MonthPickerStandalone", { enumerable: true, get: function () { return MonthPicker_Standalone_1.MonthPickerStandalone; } });
const MonthPicker_Table_1 = require("./parts/MonthPicker.Table");
/**
 * A component that displays a month picker.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/monthpicker)
 * @see 🏷️ {@link MonthPickerProps}
 *
 * @example
 * ```jsx
 *  const { inputProps, monthpickerProps } = useMonthpicker({
 *    onMonthChange: console.log,
 *  });
 *
 *  return (
 *     <MonthPicker {...monthpickerProps} dropdownCaption>
 *       <MonthPicker.Input
 *         {...inputProps}
 *         label="Velg måned"
 *       />
 *     </MonthPicker>
 *  );
 * ```
 */
exports.MonthPicker = (0, react_1.forwardRef)(({ children, dropdownCaption = false, fromDate, toDate, disabled = [], selected, open: _open, id, onClose, onOpenToggle, locale, translations, onMonthSelect, className, wrapperClassName, defaultSelected, year, onYearChange, strategy = "absolute", }, ref) => {
    const translate = (0, i18n_hooks_1.useI18n)("DatePicker", translations, (0, Date_locale_1.getTranslations)(locale));
    const langProviderLocale = (0, i18n_hooks_1.useDateLocale)();
    const ariaId = (0, utils_external_1.useId)(id);
    const popupLabelId = (0, utils_external_1.useId)();
    const [open, setOpen] = (0, hooks_1.useControllableState)({
        defaultValue: false,
        value: _open,
        onChange: () => {
            onOpenToggle === null || onOpenToggle === void 0 ? void 0 : onOpenToggle();
        },
    });
    /* We use state here to insure that anchor is defined if open is true on initial render */
    const [wrapperRef, setWrapperRef] = (0, react_1.useState)(null);
    const mergedRef = (0, hooks_1.useMergeRefs)(setWrapperRef, ref);
    const handleSelect = (month) => {
        onMonthSelect === null || onMonthSelect === void 0 ? void 0 : onMonthSelect(month);
        if (month) {
            onClose === null || onClose === void 0 ? void 0 : onClose();
            setOpen(false);
        }
    };
    if (dropdownCaption && (!fromDate || !toDate)) {
        (0, consoleWarning_1.consoleWarning)("MonthPicker: Using dropdownCaption requires `fromDate` and `toDate` props to be defined.");
        return null;
    }
    return (react_1.default.createElement(Date_locale_1.DateTranslationContextProvider, { translate: translate },
        react_1.default.createElement(Date_Input_1.DateInputContextProvider, { open: open, onOpen: () => setOpen((x) => !x), ariaId: ariaId, defined: true, caller: null },
            react_1.default.createElement(MonthPicker_context_1.MonthPickerProvider, { dropdownCaption: dropdownCaption, defaultSelected: defaultSelected, selected: selected, disabled: disabled, fromDate: fromDate, toDate: toDate, year: year, onYearChange: onYearChange, onMonthSelect: handleSelect, locale: locale ? (0, Date_locale_1.getLocaleFromString)(locale) : langProviderLocale },
                react_1.default.createElement("div", { ref: mergedRef, className: (0, helpers_1.cl)("aksel-date__wrapper", wrapperClassName) },
                    children,
                    react_1.default.createElement(Date_Dialog_1.DateDialog, { open: open, anchor: wrapperRef, onClose: () => {
                            onClose === null || onClose === void 0 ? void 0 : onClose();
                            open && setOpen(false);
                        }, locale: locale, translate: translate, variant: "month", popoverProps: {
                            id: ariaId,
                            strategy,
                        }, popupLabelId: popupLabelId },
                        react_1.default.createElement("div", { className: (0, helpers_1.cl)("rdp-month", className) },
                            react_1.default.createElement(MonthPicker_Caption_1.MonthPickerCaption, { popupLabelId: popupLabelId }),
                            react_1.default.createElement(MonthPicker_Table_1.MonthPickerTable, null))))))));
});
exports.MonthPicker.Standalone = MonthPicker_Standalone_1.MonthPickerStandalone;
exports.MonthPicker.Input = Date_Input_1.MonthPickerInput;
exports.default = exports.MonthPicker;
//# sourceMappingURL=MonthPicker.js.map