import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import Select, { Option } from '../../Select';
import { useConfiguration, useTestIds } from '../../../hooks';
import { getDayOfWeekInputTestIds } from '../DateTime.test-ids';
import { createClassName, withTestIds } from '../../../utils';
import { CALENDAR_TYPES } from './local';
import { getCalendarTypeFromLocale, getDayOfWeekNamesMap } from './utils';
// returns index of the first day of week according to given calendar type
// returned value is a position of the day in base ISO calendar
function getFirstWeekDayForCalendar(refCalendarType = CALENDAR_TYPES.ISO_8601) {
    switch (refCalendarType) {
        case CALENDAR_TYPES.ISO_8601:
            // start week with Monday
            return 0;
        case CALENDAR_TYPES.ARABIC:
            // start week with Saturday
            return 5;
        case CALENDAR_TYPES.HEBREW:
        case CALENDAR_TYPES.US:
            // start week with Sunday
            return 6;
        default:
            // ISO as default
            return 0;
    }
}
// returns array of Options for day names, ordered according to given calendar
function generateOrderedDays(dayOfWeekNames, refCalendarType) {
    const optionsArray = Object.keys(dayOfWeekNames).map(key => {
        return (_jsx(Option, { value: key.toString(), children: dayOfWeekNames[Number(key)] }, dayOfWeekNames[Number(key)]));
    });
    const startingDayIndex = getFirstWeekDayForCalendar(refCalendarType);
    return [...optionsArray.slice(startingDayIndex), ...optionsArray.slice(0, startingDayIndex)];
}
const DayOfWeekInput = (props) => {
    const { locale } = useConfiguration();
    const { testId, value, required, readOnly, disabled, onChange, className, status, ...restProps } = props;
    const testIds = useTestIds(testId, getDayOfWeekInputTestIds);
    // days ordered according to given calendar
    const dayOfWeekOptions = useMemo(() => generateOrderedDays(getDayOfWeekNamesMap(locale), getCalendarTypeFromLocale(locale)), [locale]);
    const onChangeWrapper = ev => {
        onChange?.(Number(ev.target.value));
    };
    return (_jsxs(Select, { "data-testid": testIds.control, required: required, value: value ? value.toString() : undefined, onChange: onChangeWrapper, readOnly: readOnly, disabled: disabled || readOnly, status: status, ...restProps, className: createClassName('day-of-week-input', className, { status }), children: [!required && _jsx(Option, { children: " " }), dayOfWeekOptions] }));
};
export default withTestIds(DayOfWeekInput, getDayOfWeekInputTestIds);
//# sourceMappingURL=DayOfWeekInput.js.map