/**
 * DevExtreme (esm/__internal/grids/grid_core/row_dragging/m_row_dragging.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../../../core/../core/renderer";
import {
    extend
} from "../../../../core/../core/utils/extend";
import {
    deferUpdate
} from "../../../../core/utils/common";
import {
    getWidth,
    setWidth
} from "../../../../core/utils/size";
import {
    isDefined
} from "../../../../core/utils/type";
import Sortable from "../../../../ui/sortable";
import gridCoreUtils from "../m_utils";
import {
    ATTRIBUTES,
    CLASSES
} from "./const";
import {
    GridCoreRowDraggingDom
} from "./dom";
var rowsView = Base => class extends Base {
    init() {
        super.init.apply(this, arguments);
        this._updateHandleColumn()
    }
    optionChanged(args) {
        if ("rowDragging" === args.name) {
            this._updateHandleColumn();
            this._invalidate(true, true);
            args.handled = true
        }
        super.optionChanged.apply(this, arguments)
    }
    _allowReordering() {
        var rowDragging = this.option("rowDragging");
        return !!(rowDragging && (rowDragging.allowReordering || rowDragging.allowDropInsideItem || rowDragging.group))
    }
    _updateHandleColumn() {
        var rowDragging = this.option("rowDragging");
        var allowReordering = this._allowReordering();
        var columnsController = this._columnsController;
        var isHandleColumnVisible = allowReordering && rowDragging.showDragIcons;
        null === columnsController || void 0 === columnsController ? void 0 : columnsController.addCommandColumn({
            type: "drag",
            command: "drag",
            visibleIndex: -2,
            alignment: "center",
            elementAttr: [{
                name: ATTRIBUTES.dragCell,
                value: ""
            }],
            cssClass: CLASSES.commandDrag,
            width: "auto",
            cellTemplate: this._getHandleTemplate(),
            visible: isHandleColumnVisible
        });
        null === columnsController || void 0 === columnsController ? void 0 : columnsController.columnOption("type:drag", "visible", isHandleColumnVisible)
    }
    _renderContent() {
        var rowDragging = this.option("rowDragging");
        var allowReordering = this._allowReordering();
        var $content = super._renderContent.apply(this, arguments);
        var isFixedTableRendering = this._isFixedTableRendering;
        var currentSortableName = isFixedTableRendering ? "_sortableFixed" : "_sortable";
        var anotherSortableName = isFixedTableRendering ? "_sortable" : "_sortableFixed";
        var togglePointerEventsStyle = toggle => {
            var _a;
            null === (_a = this._sortableFixed) || void 0 === _a ? void 0 : _a.$element().css("pointerEvents", toggle ? "auto" : "")
        };
        var rowSelector = ".dx-row:not(.dx-freespace-row):not(.dx-virtual-row):not(.dx-header-row):not(.dx-footer-row)";
        var filter = this.option("dataRowTemplate") ? "> table > tbody".concat(rowSelector) : "> table > tbody > ".concat(rowSelector);
        if ((allowReordering || this[currentSortableName]) && $content.length) {
            this[currentSortableName] = this._createComponent($content, Sortable, extend({
                component: this.component,
                contentTemplate: null,
                filter: filter,
                cursorOffset: options => {
                    var {
                        event: event
                    } = options;
                    var rowsViewOffset = $(this.element()).offset();
                    return {
                        x: event.pageX - rowsViewOffset.left
                    }
                },
                onDraggableElementShown: e => {
                    if (rowDragging.dragTemplate) {
                        return
                    }
                    var $dragElement = $(e.dragElement);
                    var gridInstance = $dragElement.children(".dx-widget").data(this.component.NAME);
                    this._synchronizeScrollLeftPosition(gridInstance)
                },
                dragTemplate: this._getDraggableRowTemplate(),
                handle: rowDragging.showDragIcons && ".".concat(CLASSES.commandDrag),
                dropFeedbackMode: "indicate"
            }, rowDragging, {
                onDragStart: e => {
                    var _a, _b;
                    null === (_a = this.getController("keyboardNavigation")) || void 0 === _a ? void 0 : _a._resetFocusedCell();
                    var row = e.component.getVisibleRows()[e.fromIndex];
                    e.itemData = row && row.data;
                    var isDataRow = row && "data" === row.rowType;
                    e.cancel = !allowReordering || !isDataRow;
                    null === (_b = rowDragging.onDragStart) || void 0 === _b ? void 0 : _b.call(rowDragging, e)
                },
                onDragEnter: () => {
                    togglePointerEventsStyle(true)
                },
                onDragLeave: () => {
                    togglePointerEventsStyle(false)
                },
                onDragEnd: e => {
                    var _a;
                    togglePointerEventsStyle(false);
                    null === (_a = rowDragging.onDragEnd) || void 0 === _a ? void 0 : _a.call(rowDragging, e)
                },
                onAdd: e => {
                    var _a;
                    togglePointerEventsStyle(false);
                    null === (_a = rowDragging.onAdd) || void 0 === _a ? void 0 : _a.call(rowDragging, e)
                },
                dropFeedbackMode: rowDragging.dropFeedbackMode,
                onOptionChanged: e => {
                    var hasFixedSortable = this._sortableFixed;
                    if (hasFixedSortable) {
                        if ("fromIndex" === e.name || "toIndex" === e.name) {
                            this[anotherSortableName].option(e.name, e.value)
                        }
                    }
                }
            }));
            $content.toggleClass("dx-scrollable-container", isFixedTableRendering);
            $content.toggleClass(CLASSES.sortableWithoutHandle, allowReordering && !rowDragging.showDragIcons)
        }
        return $content
    }
    _renderCore(e) {
        super._renderCore.apply(this, arguments);
        if (e && "update" === e.changeType && e.repaintChangesOnly && gridCoreUtils.isVirtualRowRendering(this)) {
            deferUpdate(() => {
                this._updateSortable()
            })
        }
    }
    _updateSortable() {
        var offset = this._dataController.getRowIndexOffset();
        var offsetDiff = offset - this._previousOffset;
        [this._sortable, this._sortableFixed].forEach(sortable => {
            var toIndex = null === sortable || void 0 === sortable ? void 0 : sortable.option("toIndex");
            if (isDefined(toIndex) && isDefined(this._previousOffset)) {
                null === sortable || void 0 === sortable ? void 0 : sortable.option("toIndex", toIndex - offsetDiff)
            }
            null === sortable || void 0 === sortable ? void 0 : sortable.option("offset", offset);
            null === sortable || void 0 === sortable ? void 0 : sortable.update()
        });
        this._previousOffset = offset
    }
    _resizeCore() {
        super._resizeCore.apply(this, arguments);
        this._updateSortable()
    }
    _getDraggableGridOptions(options) {
        var gridOptions = this.option();
        var columns = this.getColumns();
        var $rowElement = $(this.getRowElement(options.rowIndex));
        return {
            dataSource: [{
                id: 1,
                parentId: 0
            }],
            showBorders: true,
            showColumnHeaders: false,
            scrolling: {
                useNative: false,
                showScrollbar: "never"
            },
            pager: {
                visible: false
            },
            loadingTimeout: null,
            columnFixing: gridOptions.columnFixing,
            columnAutoWidth: gridOptions.columnAutoWidth,
            showColumnLines: gridOptions.showColumnLines,
            columns: columns.map(column => ({
                width: column.width || column.visibleWidth,
                fixed: column.fixed,
                fixedPosition: column.fixedPosition
            })),
            onRowPrepared: e => {
                var rowsView = e.component.getView("rowsView");
                $(e.rowElement).replaceWith($rowElement.eq(rowsView._isFixedTableRendering ? 1 : 0).clone())
            }
        }
    }
    _synchronizeScrollLeftPosition(gridInstance) {
        var scrollable = null === gridInstance || void 0 === gridInstance ? void 0 : gridInstance.getScrollable();
        null === scrollable || void 0 === scrollable ? void 0 : scrollable.scrollTo({
            x: this._scrollLeft
        })
    }
    _getDraggableRowTemplate() {
        return options => {
            var $rootElement = this.component.$element();
            var $dataGridContainer = $("<div>");
            setWidth($dataGridContainer, getWidth($rootElement));
            var items = this._dataController.items();
            var row = items && items[options.fromIndex];
            var gridOptions = this._getDraggableGridOptions(row);
            this._createComponent($dataGridContainer, this.component.NAME, gridOptions);
            $dataGridContainer.find(".dx-gridbase-container").children(":not(.".concat(this.addWidgetPrefix(CLASSES.rowsView), ")")).hide();
            $dataGridContainer.addClass(this.addWidgetPrefix(CLASSES.dragView));
            return $dataGridContainer
        }
    }
    _getHandleTemplate() {
        return GridCoreRowDraggingDom.createHandleTemplateFunc(string => this.addWidgetPrefix(string))
    }
};
export var rowDraggingModule = {
    defaultOptions: () => ({
        rowDragging: {
            showDragIcons: true,
            dropFeedbackMode: "indicate",
            allowReordering: false,
            allowDropInsideItem: false
        }
    }),
    extenders: {
        views: {
            rowsView: rowsView
        }
    }
};
