/**
 * DevExtreme (esm/__internal/ui/list/m_list.edit.decorator.swipe.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import fx from "../../../animation/fx";
import {
    move
} from "../../../animation/translator";
import {
    Deferred
} from "../../../core/utils/deferred";
import {
    getWidth
} from "../../../core/utils/size";
import EditDecorator from "./m_list.edit.decorator";
import {
    register as registerDecorator
} from "./m_list.edit.decorator_registry";
registerDecorator("delete", "swipe", EditDecorator.inherit({
    _shouldHandleSwipe: true,
    _renderItemPosition($itemElement, offset, animate) {
        const deferred = Deferred();
        const itemOffset = offset * this._itemElementWidth;
        if (animate) {
            fx.animate($itemElement, {
                to: {
                    left: itemOffset
                },
                type: "slide",
                complete() {
                    deferred.resolve($itemElement, offset)
                }
            })
        } else {
            move($itemElement, {
                left: itemOffset
            });
            deferred.resolve()
        }
        return deferred.promise()
    },
    _swipeStartHandler($itemElement) {
        this._itemElementWidth = getWidth($itemElement);
        return true
    },
    _swipeUpdateHandler($itemElement, args) {
        this._renderItemPosition($itemElement, args.offset);
        return true
    },
    _swipeEndHandler($itemElement, args) {
        const offset = args.targetOffset;
        this._renderItemPosition($itemElement, offset, true).done((($itemElement, offset) => {
            if (Math.abs(offset)) {
                this._list.deleteItem($itemElement).fail((() => {
                    this._renderItemPosition($itemElement, 0, true)
                }))
            }
        }));
        return true
    }
}));
