"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;
    };
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatePickerMonths = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importStar(require("react"));
const react_day_picker_1 = require("react-day-picker");
const aksel_icons_1 = require("@navikt/aksel-icons");
const button_1 = require("../../../button");
const select_1 = require("../../../form/select");
const typography_1 = require("../../../typography");
const utils_external_1 = require("../../../utils-external");
const Date_locale_1 = require("../../Date.locale");
const date_utils_1 = require("../../date-utils");
const DatePicker_WeekRow_1 = require("./DatePicker.WeekRow");
const DatePickerMonths = (_a) => {
    var { children, calendarMonth, locale, onWeekNumberClick, popupLabelId } = _a, rest = __rest(_a, ["children", "calendarMonth", "locale", "onWeekNumberClick", "popupLabelId"]);
    const { dayPickerProps, goToMonth, previousMonth, nextMonth } = (0, react_day_picker_1.useDayPicker)();
    const { captionLayout } = dayPickerProps;
    const translate = (0, Date_locale_1.useDateTranslationContext)().translate;
    const handleMonthChange = (0, react_1.useCallback)((date, e) => {
        const selectedMonth = Number(e.target.value);
        const newMonth = (0, date_fns_1.setMonth)((0, date_fns_1.startOfMonth)(date), selectedMonth);
        goToMonth(newMonth);
    }, [goToMonth]);
    const handleYearChange = (0, react_1.useCallback)((date, e) => {
        const selectedYear = Number(e.target.value);
        const newMonth = (0, date_fns_1.setYear)((0, date_fns_1.startOfMonth)(date), selectedYear);
        goToMonth(newMonth);
    }, [goToMonth]);
    const [navStart, navEnd] = (0, date_utils_1.calendarRange)({
        captionLayout: captionLayout === "dropdown" ? "dropdown" : "label",
        startMonth: dayPickerProps.startMonth,
        endMonth: dayPickerProps.endMonth,
        today: dayPickerProps.today,
    });
    const months = (0, date_utils_1.getMonthOptions)({
        displayMonth: calendarMonth.date,
        navStart,
        navEnd,
        locale,
    });
    const dropdownYears = (0, date_utils_1.getYearOptions)({ navStart, navEnd, locale });
    return (react_1.default.createElement("div", Object.assign({}, (0, utils_external_1.omit)(rest, ["displayIndex"])),
        react_1.default.createElement("div", { className: "aksel-date__caption" },
            (captionLayout === null || captionLayout === void 0 ? void 0 : captionLayout.startsWith("dropdown")) && (react_1.default.createElement("span", { "aria-live": "polite", "aria-atomic": "true", className: "aksel-sr-only", id: popupLabelId }, (0, date_fns_1.format)(calendarMonth.date, "LLLL y", { locale }))),
            react_1.default.createElement(button_1.Button, { variant: "tertiary-neutral", disabled: !previousMonth, onClick: () => previousMonth && goToMonth(previousMonth), icon: react_1.default.createElement(aksel_icons_1.ArrowLeftIcon, { title: translate("goToPreviousMonth") }), className: "aksel-date__caption-button", type: "button" }),
            (captionLayout === null || captionLayout === void 0 ? void 0 : captionLayout.startsWith("dropdown")) ? (react_1.default.createElement("div", { className: "aksel-date__caption" },
                react_1.default.createElement(select_1.Select, { label: translate("month"), hideLabel: true, className: "aksel-date__caption__month", onChange: (event) => handleMonthChange(calendarMonth.date, event), value: (0, date_fns_1.getMonth)(calendarMonth.date) }, months === null || months === void 0 ? void 0 : months.map(({ value, label, disabled }) => (react_1.default.createElement("option", { key: value, value: value, disabled: disabled }, label)))),
                react_1.default.createElement(select_1.Select, { label: translate("year"), hideLabel: true, className: "aksel-date__caption__year", onChange: (event) => handleYearChange(calendarMonth.date, event), value: (0, date_fns_1.getYear)(calendarMonth.date) }, dropdownYears === null || dropdownYears === void 0 ? void 0 : dropdownYears.map(({ value, label, disabled }) => (react_1.default.createElement("option", { key: value, value: value, disabled: disabled }, label)))))) : (react_1.default.createElement(typography_1.BodyShort, { weight: "semibold", as: "span", "aria-live": "polite", role: "status", className: "aksel-date__caption-label", id: popupLabelId }, (0, date_fns_1.format)(calendarMonth.date, "LLLL y", { locale }))),
            react_1.default.createElement(button_1.Button, { variant: "tertiary-neutral", icon: react_1.default.createElement(aksel_icons_1.ArrowRightIcon, { title: translate("goToNextMonth") }), onClick: () => nextMonth && goToMonth(nextMonth), disabled: !nextMonth, className: "aksel-date__caption-button", type: "button" })),
        react_1.default.createElement(DatePicker_WeekRow_1.DatePickerWeekRow, { weeks: calendarMonth.weeks, onWeekNumberClick: onWeekNumberClick }),
        children));
};
exports.DatePickerMonths = DatePickerMonths;
//# sourceMappingURL=DatePicker.Months.js.map