'use strict';

const index = require('../labels/index.cjs');
const labelWeekday = require('../labels/labelWeekday.cjs');
const labelWeekNumberHeader = require('../labels/labelWeekNumberHeader.cjs');
const labelNav = require('../labels/labelNav.cjs');
const labelGridcell = require('../labels/labelGridcell.cjs');
const labelGrid = require('../labels/labelGrid.cjs');
const labelYearDropdown = require('../labels/labelYearDropdown.cjs');
const labelWeekNumber = require('../labels/labelWeekNumber.cjs');
const labelPrevious = require('../labels/labelPrevious.cjs');
const labelNext = require('../labels/labelNext.cjs');
const labelMonthDropdown = require('../labels/labelMonthDropdown.cjs');
const labelDayButton = require('../labels/labelDayButton.cjs');

const resolveLabel = (defaultLabel, customLabel, localeLabel) => {
    if (customLabel)
        return customLabel;
    if (localeLabel) {
        return (typeof localeLabel === "function"
            ? localeLabel
            : (..._args) => localeLabel);
    }
    return defaultLabel;
};
/**
 * Merges custom labels from the props with the default labels.
 *
 * When available, uses the locale-provided translation for `labelNext`.
 *
 * @param customLabels The custom labels provided in the DayPicker props.
 * @param options Options from the date library, used to resolve locale
 *   translations.
 * @returns The merged labels object with locale-aware defaults.
 */
function getLabels(customLabels, options) {
    const localeLabels = options.locale?.labels ?? {};
    return {
        ...index,
        ...(customLabels ?? {}),
        labelDayButton: resolveLabel(labelDayButton.labelDayButton, customLabels?.labelDayButton, localeLabels.labelDayButton),
        labelMonthDropdown: resolveLabel(labelMonthDropdown.labelMonthDropdown, customLabels?.labelMonthDropdown, localeLabels.labelMonthDropdown),
        labelNext: resolveLabel(labelNext.labelNext, customLabels?.labelNext, localeLabels.labelNext),
        labelPrevious: resolveLabel(labelPrevious.labelPrevious, customLabels?.labelPrevious, localeLabels.labelPrevious),
        labelWeekNumber: resolveLabel(labelWeekNumber.labelWeekNumber, customLabels?.labelWeekNumber, localeLabels.labelWeekNumber),
        labelYearDropdown: resolveLabel(labelYearDropdown.labelYearDropdown, customLabels?.labelYearDropdown, localeLabels.labelYearDropdown),
        labelGrid: resolveLabel(labelGrid.labelGrid, customLabels?.labelGrid, localeLabels.labelGrid),
        labelGridcell: resolveLabel(labelGridcell.labelGridcell, customLabels?.labelGridcell, localeLabels.labelGridcell),
        labelNav: resolveLabel(labelNav.labelNav, customLabels?.labelNav, localeLabels.labelNav),
        labelWeekNumberHeader: resolveLabel(labelWeekNumberHeader.labelWeekNumberHeader, customLabels?.labelWeekNumberHeader, localeLabels.labelWeekNumberHeader),
        labelWeekday: resolveLabel(labelWeekday.labelWeekday, customLabels?.labelWeekday, localeLabels.labelWeekday),
    };
}

exports.getLabels = getLabels;
