"use strict";

exports.__esModule = true;
exports.KanbanUniDriver = KanbanUniDriver;
exports.default = void 0;
var _CollectionToolbarUni = require("../CollectionToolbar/CollectionToolbar.uni.driver");
var _baseUniDriverFactory = require("../../unidriver/baseUniDriverFactory");
var _unidriver = require("@wix/design-system/dist/testkit/unidriver");
var _DragAndDropUni = require("../DragAndDrop/DragAndDrop.uni.driver");
var _ManageTagsModalUni = require("../Tags/ManageTags/ManageTagsModal/ManageTagsModal.uni.driver");
var _CustomFieldPanelUni = require("../CustomFieldsPanel/CustomFieldPanel.uni.driver");
function KanbanUniDriver(base, body) {
  const toolbarBase = () => base.$('[data-hook="collection-toolbar"]');
  const toolbar = () => (0, _CollectionToolbarUni.CollectionToolbarUniDriver)(toolbarBase(), body);
  const dragAndDrop = () => (0, _DragAndDropUni.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 {
    ...(0, _baseUniDriverFactory.baseUniDriverFactory)(base),
    toolbar: () => toolbar(),
    enterSearchText: text => toolbar().enterSearchText(text),
    clearSearchText: () => toolbar().clearSearchText(),
    isKanbanLoading: () => base.$('[data-hook="kanban-stages-skeleton"]').exists(),
    getCustomFieldPanel: () => (0, _CustomFieldPanelUni.CustomFieldPanelUniDriver)(body, body),
    getManageTagsModal: () => (0, _ManageTagsModalUni.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 (0, _unidriver.TextUniDriver)(base.$(`[data-hook^="kanban-stage-title-${stageId}"]`), body);
      },
      stageSummary: stageId => {
        const counterBadge = (0, _unidriver.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 != null && 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 = (0, _unidriver.CounterBadgeUniDriver)(base.$(`[data-hook^="kanban-stage-title-total-${stageId}"]`), body);
        return {
          ...counterBadge,
          exists: () => counterBadge.exists(),
          totalItems: () => counterBadge.getContentText()
        };
      },
      addCardButton: stageKey => (0, _unidriver.AddItemUniDriver)(base.$(`[data-hook="kanban-stage-add-card-${stageKey}"]`), body),
      stagesSkeleton: () => (0, _unidriver.BoxUniDriver)(base.$('[data-hook="kanban-stages-skeleton"]'), body),
      noResultsBox: stageKey => (0, _unidriver.BoxUniDriver)(base.$(`[data-hook="kanban-stage-no-results-${stageKey}"]`), body),
      cardsSkeleton: stageKey => (0, _unidriver.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 (0, _unidriver.TextUniDriver)(base.$(`[data-hook^="kanban-card-title-${stageKey}-${cardKey}"]`), body);
      },
      getPrimaryAction: ({
        stageKey,
        cardKey
      }) => {
        const tooltip = (0, _unidriver.TooltipUniDriver)(base.$(`[data-hook="kanban-card-primary-action-tooltip-${stageKey}-${cardKey}"]`), body);
        const button = (0, _unidriver.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 = (0, _unidriver.PopoverMenuUniDriver)(base.$(`[data-hook="kanban-card-more-actions-${stageKey}-${cardKey}"]`), body);
        const triggerButton = popoverMenu.getTriggerElement(`kanban-card-more-actions-trigger-${stageKey}-${cardKey}`);
        const iconButton = (0, _unidriver.IconButtonUniDriver)(triggerButton, base);
        return {
          ...popoverMenu,
          clickTriggerElement: () => iconButton.click()
        };
      },
      getActionModal: () => (0, _unidriver.MessageModalLayoutUniDriver)(body.$('[data-hook="collection-action-modal"]'), body),
      dragCardToStage
    }
  };
}
var _default = exports.default = KanbanUniDriver;
//# sourceMappingURL=Kanban.uni.driver.js.map