import { CollectionToolbarUniDriver } from '../CollectionToolbar/CollectionToolbar.uni.driver';
import { baseUniDriverFactory } from '../../unidriver/baseUniDriverFactory';
import { AddItemUniDriver, BoxUniDriver, ButtonUniDriver, CounterBadgeUniDriver, IconButtonUniDriver, MessageModalLayoutUniDriver, PopoverMenuUniDriver, TextUniDriver, TooltipUniDriver, } from '@wix/design-system/dist/testkit/unidriver';
import { DragAndDropUniDriver } from '../DragAndDrop/DragAndDrop.uni.driver';
import { ManageTagsModalUniDriver } from '../Tags/ManageTags/ManageTagsModal/ManageTagsModal.uni.driver';
import { CustomFieldPanelUniDriver } from '../CustomFieldsPanel/CustomFieldPanel.uni.driver';
export function KanbanUniDriver(base, body) {
    const toolbarBase = () => base.$('[data-hook="collection-toolbar"]');
    const toolbar = () => CollectionToolbarUniDriver(toolbarBase(), body);
    const dragAndDrop = () => DragAndDropUniDriver(base);
    const dragCardToStage = async ({ sourceStageKey, cardKey, targetStageKey, }) => {
        const targetStageElement = base.$(`[data-hook^="kanban-stage-container-${targetStageKey}"]`);
        if (!(await targetStageElement.exists())) {
            throw new Error(`Target stage with key ${targetStageKey} not found`);
        }
        const sourceStageElement = base.$(`[data-hook^="kanban-stage-container-${sourceStageKey}"]`);
        if (!(await sourceStageElement.exists())) {
            throw new Error(`Source stage with key ${sourceStageKey} not found`);
        }
        const cardElement = base.$(`[data-hook^="kanban-card-drag-item-${sourceStageKey}-##-${cardKey}"]`);
        if (!(await cardElement.exists())) {
            throw new Error(`Card with key ${cardKey} not found in stage ${sourceStageKey}`);
        }
        const driverDragAndDrop = dragAndDrop();
        await driverDragAndDrop.dragStart(cardElement);
        await driverDragAndDrop.dragEnd(targetStageElement);
    };
    return {
        ...baseUniDriverFactory(base),
        toolbar: () => toolbar(),
        enterSearchText: (text) => toolbar().enterSearchText(text),
        clearSearchText: () => toolbar().clearSearchText(),
        isKanbanLoading: () => base.$('[data-hook="kanban-stages-skeleton"]').exists(),
        getCustomFieldPanel: () => CustomFieldPanelUniDriver(body, body),
        getManageTagsModal: () => ManageTagsModalUniDriver(base, body),
        stages: {
            count: () => base.$$('[data-hook^="kanban-stage-container"]').count(),
            doneDividerExists: async () => {
                const count = await base
                    .$$(`[data-hook^="kanban-stages-done-divider"]`)
                    .count();
                return count > 0;
            },
            stageTitle: (stageId) => {
                return TextUniDriver(base.$(`[data-hook^="kanban-stage-title-${stageId}"]`), body);
            },
            stageSummary: (stageId) => {
                const counterBadge = CounterBadgeUniDriver(base.$(`[data-hook^="kanban-stage-title-total-${stageId}"]`), body);
                return {
                    ...counterBadge,
                    exists: () => counterBadge.exists(),
                    totalItems: () => counterBadge.getContentText(),
                };
            },
            scrollToEndOfStageCardsContainer: async (stageKey) => {
                const scrollableElementDataHook = `kanban-stage-cards-container-${stageKey}`;
                const { page } = (await body
                    .$(`[data-hook="${scrollableElementDataHook}"]`)
                    .getNative());
                // puppeteer
                if (page?.evaluate) {
                    await page.evaluate((_dataHook) => {
                        const scrollableElement = document.querySelector(`[data-hook="${_dataHook}"]`);
                        scrollableElement.scrollBy({
                            behavior: 'smooth',
                            top: scrollableElement.scrollHeight,
                        });
                    }, scrollableElementDataHook);
                    await page.waitForTimeout(1000); // delay scroll resolve by 1 sec
                }
                // jsdom
                else {
                    const pageScrollContent = body.$(`[data-hook="${scrollableElementDataHook}"]`);
                    if (await pageScrollContent.exists()) {
                        // Scroll to the end of pageScrollContent by setting scrollTop to (scrollHeight - clientHeight)
                        const nativeEl = await pageScrollContent.getNative();
                        nativeEl.scrollTop = nativeEl.scrollHeight - nativeEl.clientHeight;
                        await nativeEl.dispatchEvent(new Event('scroll'));
                    }
                }
            },
            stageSummaryBadge: (stageId) => {
                const counterBadge = CounterBadgeUniDriver(base.$(`[data-hook^="kanban-stage-title-total-${stageId}"]`), body);
                return {
                    ...counterBadge,
                    exists: () => counterBadge.exists(),
                    totalItems: () => counterBadge.getContentText(),
                };
            },
            addCardButton: (stageKey) => AddItemUniDriver(base.$(`[data-hook="kanban-stage-add-card-${stageKey}"]`), body),
            stagesSkeleton: () => BoxUniDriver(base.$('[data-hook="kanban-stages-skeleton"]'), body),
            noResultsBox: (stageKey) => BoxUniDriver(base.$(`[data-hook="kanban-stage-no-results-${stageKey}"]`), body),
            cardsSkeleton: (stageKey) => BoxUniDriver(base.$(`[data-hook="kanban-cards-skeleton-${stageKey}"]`), body),
        },
        cards: {
            count: (stageKey) => base.$$(`[data-hook^="kanban-card-${stageKey}-"]`).count(),
            exists: ({ stageKey, cardKey }) => base.$(`[data-hook^="kanban-card-${stageKey}-${cardKey}"]`).exists(),
            getTitle: ({ stageKey, cardKey, }) => {
                return TextUniDriver(base.$(`[data-hook^="kanban-card-title-${stageKey}-${cardKey}"]`), body);
            },
            getPrimaryAction: ({ stageKey, cardKey, }) => {
                const tooltip = TooltipUniDriver(base.$(`[data-hook="kanban-card-primary-action-tooltip-${stageKey}-${cardKey}"]`), body);
                const button = ButtonUniDriver(base.$(`[data-hook="kanban-card-primary-action-${stageKey}-${cardKey}"]`), body);
                return {
                    ...button,
                    exists: () => button.exists(),
                    text: () => button.getButtonTextContent(),
                    tooltip: () => tooltip,
                };
            },
            getSecondaryActions: ({ stageKey, cardKey, }) => {
                const popoverMenu = PopoverMenuUniDriver(base.$(`[data-hook="kanban-card-more-actions-${stageKey}-${cardKey}"]`), body);
                const triggerButton = popoverMenu.getTriggerElement(`kanban-card-more-actions-trigger-${stageKey}-${cardKey}`);
                const iconButton = IconButtonUniDriver(triggerButton, base);
                return {
                    ...popoverMenu,
                    clickTriggerElement: () => iconButton.click(),
                };
            },
            getActionModal: () => MessageModalLayoutUniDriver(body.$('[data-hook="collection-action-modal"]'), body),
            dragCardToStage,
        },
    };
}
export default KanbanUniDriver;
//# sourceMappingURL=Kanban.uni.driver.js.map