import { baseUniDriverFactory, findByHook, } from '../utils/test-utils/unidriver';
import { calendarUniDriverFactory } from '../Calendar/Calendar.uni.driver.js';
import { testkit as inputUniDriverFactory } from '../Input/Input.uni.driver';
import { dataHooks } from './constants';
import { popoverNextUniDriverFactory } from '../PopoverNext/PopoverNext.uni.driver';
export const datePickerUniDriverFactory = (base, body) => {
    /**
     * The input element is selected using [data-input-root] attribute instead of a dataHook
     * because the inputDataHook can be overridden by DatePicker prop, making it unreliable for testing
     */
    const inputDriver = inputUniDriverFactory(base.$('[data-input-root]'), body);
    const popoverDriver = popoverNextUniDriverFactory(findByHook(base, dataHooks.datePickerPopover), body);
    const driver = {
        exists: () => baseUniDriverFactory(base).exists(),
        open: async () => {
            await inputDriver.click();
            await inputDriver.trigger('keyDown', {
                key: 'ArrowUp',
                keyCode: 38,
            });
        },
        getWidth: () => base._prop('style').then(style => style.width),
        clickOutside: () => popoverDriver.clickOutside(),
        focusCalendar: () => inputDriver.trigger('keyDown', { key: 'ArrowUp', keyCode: 38 }),
        /**
         * Returns the element rendered inside the footer.
         */
        getFooter: () => base.$(`[data-hook="${dataHooks.datePickerFooter}"] > *`),
    };
    // TODO: needs to be fixed, autodocs doesn't generate the docs below.
    return {
        /**
         * Input Unidriver methods
         */
        inputDriver,
        /**
         * Calender Unidriver methods
         */
        calendarDriver: calendarDriverProxy(base, body),
        /**
         * Checks whether the component found with the given data hook.
         * @returns {Promise<boolean>}
         */
        exists: driver.exists,
        /**
         * Contains 'exists', 'open' and 'getWidth' methods
         */
        driver,
    };
};
// Finds the correct calendar element no matter where the popover is rendered (window, viewport, parent, or scrollParent).
const calendarDriverProxy = (base, body) => ({
    ...Object.keys(calendarUniDriverFactory(base)).reduce((testkit, method) => {
        return {
            ...testkit,
            [method]: async (...args) => {
                const id = await base.attr('data-date-picker-id');
                const calendarDriver = calendarUniDriverFactory(body.$(`[data-calendar-id="${id}"]`));
                return calendarDriver[method](...args);
            },
        };
    }, {}),
});
//# sourceMappingURL=DatePicker.uni.driver.js.map