import { baseUniDriverFactory, findByHook, } from '../utils/test-utils/unidriver';
import { dropdownLayoutDriverFactory } from '../DropdownLayout/DropdownLayout.uni.driver';
import { popoverNextUniDriverFactory } from '../PopoverNext/PopoverNext.uni.driver';
import { DATA_HOOKS } from '../DropdownLayout/DataAttr';
export const dropdownBaseDriverFactory = (base, body) => {
    const byDataHook = (dataHook) => base.$(`[data-hook="${dataHook}"]`);
    const getTargetElement = (dataHook) => byDataHook(dataHook);
    const getContentElement = async () => popoverNextUniDriverFactory(base, body).getContentElementUniDriver();
    const createDropdownLayoutDriver = async () => {
        const contentElement = await getContentElement();
        if (!contentElement) {
            throw new Error('Content element not found');
        }
        return dropdownLayoutDriverFactory(contentElement.$(`[data-hook="dropdown-base-dropdownlayout"]`), body);
    };
    return {
        ...baseUniDriverFactory(base),
        /** Returns the list type prop value */
        getListType: async (dataHook) => {
            const dropdownBaseRootElement = await body.$(`[data-hook="${dataHook}"]`);
            return dropdownBaseRootElement.attr('data-list-type');
        },
        /** Returns the target element */
        clickTargetElement: (dataHook) => getTargetElement(dataHook).click(),
        /** Hover the target element */
        hoverTargetElement: async (dataHook) => await getTargetElement(dataHook).mouse.hover(),
        /** Returns `true` if the dropdown is being shown */
        isDropdownShown: () => popoverNextUniDriverFactory(base, body).isContentElementExists(),
        /** Select a specific option (requires the DropdownBase to be opened) */
        selectOption: async (index) => (await createDropdownLayoutDriver()).clickAtOption(index),
        /** Select a specific option by its data hook (requires the DropdownBase to be opened) */
        selectOptionByDataHook: async (dataHook) => (await createDropdownLayoutDriver()).clickAtOptionByDataHook(dataHook),
        /** Click outside of the component */
        clickOutside: () => popoverNextUniDriverFactory(base, body).clickOutside(),
        /** Options count (requires the DropdownBase to be opened) */
        optionsCount: async () => (await createDropdownLayoutDriver()).optionsLength(),
        optionContentAt: async (position) => {
            const dropdownLayoutDriver = await createDropdownLayoutDriver();
            const optionsDrivers = await dropdownLayoutDriver.options();
            const optionElement = await optionsDrivers[position].element();
            const option = await findByHook(optionElement, DATA_HOOKS.OPTION);
            /*
               Option content can be
               1. node - <div>some text</div>
               2. text - some text
            */
            const nodeContent = option.$$(':first-child');
            const contentIsNode = (await nodeContent.count()) > 0;
            if (contentIsNode) {
                return await nodeContent.get(0).getNative();
            }
            else {
                return option.text();
            }
        },
        /** Returns the selected option (requires the DropdownBase to be opened)*/
        getSelectedOptionId: async () => (await createDropdownLayoutDriver()).getSelectedOptionId(),
        /** Returns the marked option (requires the DropdownBase to be opened)*/
        getMarkedOption: async () => (await createDropdownLayoutDriver()).markedOption(),
        mouseEnter: () => popoverNextUniDriverFactory(base, body).mouseEnter(),
        mouseLeave: () => popoverNextUniDriverFactory(base, body).mouseLeave(),
    };
};
//# sourceMappingURL=DropdownBase.uni.driver.js.map