"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedTableUniDriver = NestedTableUniDriver;
exports.default = void 0;
var _ToolbarCollectionErrorStateUni = require("../ErrorState/ToolbarCollectionErrorState.uni.driver");
var _CollectionToolbarUni = require("../CollectionToolbar/CollectionToolbar.uni.driver");
var _AppliedFiltersTagListUni = require("../AppliedFiltersTagList/AppliedFiltersTagList.uni.driver");
var _TableUni = _interopRequireDefault(require("../Table/Table.uni.driver"));
var _NestedTableParentCellUni = require("./NestedTableParentCell.uni.driver");
var _NestedTableFlatModeMainCellUni = require("./NestedTableFlatModeMainCell.uni.driver");
var _NestedTableRowPlaceholderStatesUni = require("./NestedTableRowPlaceholderStates.uni.driver");
var _CollectionItemActionsUni = require("../CollectionItemActions/CollectionItemActions.uni.driver");
var _SummaryBarUni = require("../SummaryBar/SummaryBar.uni.driver");
var _DragAndDropUni = require("../DragAndDrop/DragAndDrop.uni.driver");
var _NestedTableGhostRowUni = require("../NestedTableDragAndDrop/NestedTableGhostRow.uni.driver");
var _NestedTableDragOverlayUni = require("../NestedTableDragAndDrop/NestedTableDragOverlay.uni.driver");
var _NestedTableRowUni = require("./NestedTableRow.uni.driver");
var _ExpandCollapseAllButtonUni = require("./ExpandCollapseAllButton.uni.driver");
function NestedTableUniDriver(base, body) {
  const toolbarBase = () => base.$('[data-hook="collection-toolbar"]');
  const toolbar = () => (0, _CollectionToolbarUni.CollectionToolbarUniDriver)(toolbarBase(), body);
  const table = (0, _TableUni.default)(base, body);
  const loadingStateExists = () => base.$('[data-hook="placeholder-states-loading-state"]').exists();
  const getSummaryBar = () => (0, _SummaryBarUni.SummaryBarUniDriver)(base, body);
  const dragAndDropFullFlow = async (sourceRowBase, targetRowBase, options = {}) => {
    const {
      horizontalPlacement = 'sibling',
      scrollDirectionToTarget
    } = options;
    const shiftLevel = horizontalPlacement === 'child' ? 1 : horizontalPlacement === 'parentSibling' ? -1 : 0;
    const dragAndDropDriver = (0, _DragAndDropUni.DragAndDropUniDriver)(base);
    const sourceNestedTableRow = (0, _NestedTableRowUni.NestedTableRowUniDriver)(sourceRowBase, body);
    const sourceLevel = await sourceNestedTableRow.getLevel();
    const sourceKey = await sourceNestedTableRow.getKey();
    const sourceIndex = await table.getRenderedItemIndexByKey(sourceKey);
    const indentationWidth = await sourceNestedTableRow.getIndentationWidth();
    await dragAndDropDriver.dragStart(sourceRowBase);
    if (targetRowBase == null) {
      await table.scrollToTop();
    } else if (scrollDirectionToTarget === 'down') {
      await table.scrollDownTo(targetRowBase);
    } else if (scrollDirectionToTarget === 'up') {
      await table.scrollUpTo(targetRowBase);
    }
    const getTarget = async targetRowBase => {
      const targetNestedTableRow = (0, _NestedTableRowUni.NestedTableRowUniDriver)(targetRowBase, body);
      const targetLevel = await targetNestedTableRow.getLevel();
      const targetKey = await targetNestedTableRow.getKey();
      const targetIndex = await table.getRenderedItemIndexByKey(targetKey);
      const indentationDiff = targetLevel - sourceLevel;
      const overIndex = targetIndex > sourceIndex ? targetIndex : targetIndex + 1;
      const deltaX = (indentationDiff + shiftLevel) * indentationWidth;
      return {
        targetLevel,
        targetIndex,
        indentationDiff,
        overIndex,
        deltaX
      };
    };
    const target = targetRowBase ? await getTarget(targetRowBase) : {
      overIndex: 0,
      deltaX: 0
    };
    const overRowBase = table.getRowByIndex(target.overIndex);
    await dragAndDropDriver.moveOver(overRowBase, {
      delta: {
        x: target.deltaX
      }
    });
    await dragAndDropDriver.dragEnd(overRowBase);
  };
  return {
    ...table,
    toolbar: () => toolbar(),
    loadingStateExists,
    getRootErrorStateByDataHook: (dataHook = 'placeholder-states-error-state') => (0, _ToolbarCollectionErrorStateUni.ToolbarCollectionErrorStateUniDriver)(base.$(`[data-hook="${dataHook}"]`), body),
    subToolbar: () => (0, _AppliedFiltersTagListUni.AppliedFiltersTagListUniDriver)(base.$('[data-hook="collection-table-sub-toolbar"]'), body),
    isRowExpandable: async rowKey => {
      var _await$table$_getRowB;
      return (_await$table$_getRowB = await table._getRowByKey(rowKey).attr('class')) == null ? void 0 : _await$table$_getRowB.includes('expandable');
    },
    getMainColumnAt: row => (0, _NestedTableParentCellUni.NestedTableParentCellUniDriver)(table._getCellByRowIndexAndCellKey(row, 'nested-mode-main-column'), body),
    _getDragAndDropDriver: () => (0, _DragAndDropUni.DragAndDropUniDriver)(base),
    /**
     * Drag and drop a row by its key to a target row by its key. Possible options:
     * - sourceKey: The ID of the item being moved.
     * - targetKey: The ID of the target item (the one the moved item will be placed vertically after). If null - move the item to the top of the table (i.e no "target" item)
     * - options.placement: Defines the position of the moved item relative to the target:
     *   - 'child': Places the moved item as a child of the target.
     *   - 'sibling': Places the moved item as a sibling immediately after the target. - This is the default if options.placement is not provided by the developer
     *   - 'parentSibling': Places the moved item as a sibling of the target's parent.
     * - options.scrollDirectionToTarget: Scroll to the target item before trying to drop. If not provided, no scroll will be done.
     *   - 'down'
     *   - 'up'
     */
    dragAndDropRowByKey: async (sourceKey, targetKey, options) => {
      return dragAndDropFullFlow(table._getRowByKey(sourceKey), targetKey ? table._getRowByKey(targetKey) : null, options);
    },
    /**
     * Drag and drop a row by its index to a target row by its index. Possible options:
     * - sourceKey: The ID of the item being moved.
     * - targetKey: The ID of the target item (the one the moved item will be placed vertically after). If null - move the item to the top of the table (i.e no "target" item)
     * - options.placement: Defines the position of the moved item relative to the target:
     *   - 'child': Places the moved item as a child of the target.
     *   - 'sibling': Places the moved item as a sibling immediately after the target. - This is the default if options.placement is not provided by the developer
     *   - 'parentSibling': Places the moved item as a sibling of the target's parent.
     * - options.scrollDirectionToTarget: Scroll to the target item before trying to drop. If not provided, will be determined by the indexes.
     *   - 'down'
     *   - 'up'
     */
    dragAndDropRowAt: async (sourceIndex, targetIndex, options) => {
      return dragAndDropFullFlow(table.getRow(sourceIndex), targetIndex ? table.getRow(targetIndex) : null, {
        ...options,
        scrollDirectionToTarget: options.scrollDirectionToTarget ?? (targetIndex != null ? targetIndex > sourceIndex ? 'down' : 'up' : undefined)
      });
    },
    getActionCellByKey: rowKey => (0, _CollectionItemActionsUni.CollectionItemActionsUniDriver)(table._getRowByKey(rowKey), body),
    getMainColumnByKey: rowKey => (0, _NestedTableParentCellUni.NestedTableParentCellUniDriver)(table._getCellByKey(rowKey, 'nested-mode-main-column'), body),
    getRenderedRowsByParentKey: parentId => (parentId ? base.$$(`tbody tr[data-table-row="dataTableRow"][data-parent-key="${parentId}"]`) : base.$$(`tbody tr[data-table-row="dataTableRow"]:not([data-parent-key])`)).map(async row => (0, _NestedTableRowUni.NestedTableRowUniDriver)(row, body)),
    getRenderedRowsByLevel: level => base.$$(`tbody tr[data-table-row="dataTableRow"][data-level="${level}"]`).map(async row => (0, _NestedTableRowUni.NestedTableRowUniDriver)(row, body)),
    getRenderedRowByKey: rowKey => (0, _NestedTableRowUni.NestedTableRowUniDriver)(table._getRowByKey(rowKey), body),
    getRowParentKey: rowKey => table._getRowByKey(rowKey).attr('data-parent-key'),
    getRowIndexInParent: async rowKey => Number(await table._getRowByKey(rowKey).attr('data-index-in-parent')),
    getFlatModeMainColumnByKey: rowKey => (0, _NestedTableFlatModeMainCellUni.NestedTableFlatModeMainCellUniDriver)(table._getCellByKey(rowKey, 'flat-mode-main-column'), body),
    getFlatModeMainColumnAt: row => (0, _NestedTableFlatModeMainCellUni.NestedTableFlatModeMainCellUniDriver)(table._getCellByRowIndexAndCellKey(row, 'flat-mode-main-column'), body),
    getPlaceholderStatesByRowKey: rowKey => (0, _NestedTableRowPlaceholderStatesUni.NestedTableRowPlaceholderStatesUniDriver)(table._getRowByKey(`placeholder-states-${rowKey}`), body),
    /**
     * Gets collection summary bar
     */
    getSummaryBar,
    getGhostRow: () => (0, _NestedTableGhostRowUni.NestedTableGhostRowUniDriver)(base.$('[data-hook="ghost-indentation-cell"]'), body),
    getDragOverlay: () => (0, _NestedTableDragOverlayUni.NestedTableDragOverlayUniDriver)(body.$('[data-hook="nested-table-dnd-overlay-wrapper"]'), body),
    getExpandCollapseAllButton: () => (0, _ExpandCollapseAllButtonUni.ExpandCollapseAllButtonUniDriver)(base, body)
  };
}
var _default = exports.default = NestedTableUniDriver;
//# sourceMappingURL=NestedTable.uni.driver.js.map