var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { createDynamicSlimmer, InjectVegaSlimmer, MapToComponentField, MapToComponentMethod, methodPlaceholder, VegaSlimmer, } from 'vega-slimmer/core';
import { DraggableElementSlimmer } from '../../../../helpers/slimmers/draggable-element-slimmer';
/**
 * The row draggable controller for the vega-table
 */
export class VegaTableRowDraggableController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.draggableElementSlimmer = createDynamicSlimmer(VegaTableRowDraggableController, DraggableElementSlimmer, [{ mapFrom: 'rowDraggable', mapTo: 'isDraggable' }], [{ mapFrom: 'watchRowDraggable', mapTo: 'watchIsDraggable' }], [
      {
        container: this.getDragContainer.bind(this),
        itemSelector: 'vega-table-row',
        triggerElementSelector: 'td.vega-table-draggable-column',
        visualElementSelector: 'tr.vega-table-row',
        onDragDrop: this.onDragDrop.bind(this),
      },
    ]);
  }
  /**
   * Component lifecycle - [connectedCallback]{@link VegaPopover.connectedCallback}
   */
  connectedCallback() {
    methodPlaceholder();
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaPopover.disconnectedCallback}
   */
  disconnectedCallback() {
    methodPlaceholder();
  }
  /**
   * Component lifecycle - [componentDidLoad]{@link VegaPopover.componentDidLoad}
   */
  componentDidLoad() {
    methodPlaceholder();
  }
  /**
   * Watches for changes to the rowDraggable property and initializes or destroys the DraggableElement instance accordingly.
   * If rowDraggable is true, it calls setRowDraggableElement to initialize the instance;
   * if false, it calls destroyRowDraggableElement to clean up.
   */
  watchRowDraggable() {
    methodPlaceholder();
  }
  /**
   * Resolve the draggable container for current render mode.
   *
   * @returns {Nullable<HTMLElement>} The table body container used by drag behavior.
   */
  getDragContainer() {
    return this.isTemplateRenderMode
      ? this.host.querySelector('vega-table-body')
      : this.vegaTableBodyRenderer.getTableBodyRef();
  }
  /**
   * Handles the drag and drop event emitted by the DraggableElement instance, re-emitting it through the rowDragDropEventEmitter with additional details.
   *
   * @param {DragDropEventPayload} payload - An object containing payload about the drag event.
   */
  onDragDrop(payload) {
    const event = this.rowDragDropEventEmitter.emit(payload);
    if (!event.defaultPrevented) {
      payload.status === 'end' && this.onDragEnd(payload);
    }
  }
  /**
   * Handles the logic for reordering rows in the table when a drag and drop action is completed.
   *
   * @param {DragDropEventPayload} payload - An object containing details about the completed drag event, including fromIndex and toIndex.
   */
  onDragEnd(payload) {
    const fromIndex = payload.options.fromIndex;
    const toIndex = payload.options.toIndex;
    this.vegaTableSortController.clearSorter();
    if (this.isTemplateRenderMode) {
      const rows = Array.from(this.host.querySelectorAll('vega-table-row'));
      const container = this.host.querySelector('vega-table-body');
      fromIndex > toIndex
        ? container.insertBefore(rows[fromIndex], rows[toIndex])
        : container.insertBefore(rows[fromIndex], rows[toIndex].nextSibling);
    }
    else {
      const rows = this.vegaTableDataSourceController.getCurrentData();
      const copiedRows = [...rows];
      const [moved] = copiedRows.splice(fromIndex, 1);
      copiedRows.splice(toIndex, 0, moved);
      this.vegaTableDataSourceController.setCurrentData(copiedRows);
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "rowDraggable", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "vegaTableDataSourceController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "isTemplateRenderMode", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "vegaTableBodyRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "vegaTableSortController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowDraggableController.prototype, "rowDragDropEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableRowDraggableController.prototype, "draggableElementSlimmer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRowDraggableController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableRowDraggableController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaTableRowDraggableController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('watchRowDraggable')
], VegaTableRowDraggableController.prototype, "watchRowDraggable", null);
