"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SortableItem = exports.ImagesSortable = exports.Item = void 0;
var react_1 = __importStar(require("react"));
var react_dom_1 = require("react-dom");
var core_1 = require("@dnd-kit/core");
var modifiers_1 = require("@dnd-kit/modifiers");
var sortable_1 = require("@dnd-kit/sortable");
var S = __importStar(require("./ImagesSortable.style"));
exports.Item = react_1.default.memo(react_1.default.forwardRef(function Item(_a, ref) {
    var color = _a.color, dragOverlay = _a.dragOverlay, dragging = _a.dragging, handle = _a.handle, height = _a.height, index = _a.index, listeners = _a.listeners, onRemove = _a.onRemove, sorting = _a.sorting, transition = _a.transition, transform = _a.transform, value = _a.value, wrapperStyle = _a.wrapperStyle, disabled = _a.disabled, renderItem = _a.renderItem, props = __rest(_a, ["color", "dragOverlay", "dragging", "handle", "height", "index", "listeners", "onRemove", "sorting", "transition", "transform", "value", "wrapperStyle", "disabled", "renderItem"]);
    react_1.useEffect(function () {
        if (!dragOverlay) {
            return;
        }
        document.body.style.cursor = 'grabbing';
        return function () {
            document.body.style.cursor = '';
        };
    }, [dragOverlay]);
    return (react_1.default.createElement(S.ItemContainer, { style: {
            '--translate-x': transform ? Math.round(transform.x) + "px" : undefined,
            '--translate-y': transform ? Math.round(transform.y) + "px" : undefined,
            '--scale-x': (transform === null || transform === void 0 ? void 0 : transform.scaleX) ? "" + transform.scaleX : undefined,
            '--scale-y': (transform === null || transform === void 0 ? void 0 : transform.scaleY) ? "" + transform.scaleY : undefined,
            '--index': index,
            '--color': color,
        }, ref: ref }, renderItem({ value: value, dragging: dragging, dragOverlay: dragOverlay, disabled: disabled, listeners: listeners, index: index })));
}));
function ImagesSortable(_a) {
    var images = _a.images, onChange = _a.onChange, renderItem = _a.renderItem;
    var items = images.map(function (el, i) { var _a; return (_a = el === null || el === void 0 ? void 0 : el.id.toString()) !== null && _a !== void 0 ? _a : i; });
    var _b = react_1.useState(null), activeId = _b[0], setActiveId = _b[1];
    var sensors = core_1.useSensors(core_1.useSensor(core_1.MouseSensor, {}), core_1.useSensor(core_1.TouchSensor, {}), core_1.useSensor(core_1.KeyboardSensor, {
        coordinateGetter: sortable_1.sortableKeyboardCoordinates,
    }));
    var getIndex = items.indexOf.bind(items);
    var activeIndex = activeId ? getIndex(activeId) : -1;
    return (react_1.default.createElement(core_1.DndContext, { sensors: sensors, collisionDetection: core_1.closestCenter, onDragStart: function (_a) {
            var active = _a.active;
            if (!active) {
                return;
            }
            setActiveId(active.id);
        }, onDragEnd: function (_a) {
            var over = _a.over;
            setActiveId(null);
            if (over) {
                var overIndex = getIndex(over.id);
                if (activeIndex !== overIndex) {
                    onChange(sortable_1.arrayMove(images, activeIndex, overIndex));
                }
            }
        }, onDragCancel: function () { return setActiveId(null); }, modifiers: [modifiers_1.restrictToWindowEdges] },
        react_1.default.createElement(S.Wrapper, null,
            react_1.default.createElement(sortable_1.SortableContext, { items: items, strategy: sortable_1.rectSortingStrategy },
                react_1.default.createElement(S.GridContainer, null, items.map(function (value, index) {
                    var _a;
                    if (!images[Number(index)])
                        return null;
                    return (react_1.default.createElement(SortableItem, { key: (_a = images[Number(index)]) === null || _a === void 0 ? void 0 : _a.id, id: value, value: images[Number(index)], index: index, useDragOverlay: true, renderItem: renderItem }));
                })))),
        react_dom_1.createPortal(react_1.default.createElement(core_1.DragOverlay, { adjustScale: true }, activeId ? (react_1.default.createElement(exports.Item, { value: images[activeIndex], handle: false, dragOverlay: true, renderItem: renderItem })) : null), document.body)));
}
exports.ImagesSortable = ImagesSortable;
function SortableItem(_a) {
    var id = _a.id, index = _a.index, useDragOverlay = _a.useDragOverlay, value = _a.value, renderItem = _a.renderItem;
    var _b = sortable_1.useSortable({
        id: id,
        disabled: !!(value === null || value === void 0 ? void 0 : value.disabled),
    }), isDragging = _b.isDragging, isSorting = _b.isSorting, listeners = _b.listeners, setNodeRef = _b.setNodeRef, transform = _b.transform, transition = _b.transition;
    return (react_1.default.createElement(exports.Item, { ref: setNodeRef, value: value, dragging: isDragging, sorting: isSorting, index: index, transform: transform, transition: !useDragOverlay && isDragging ? 'none' : transition, listeners: listeners, dragOverlay: !useDragOverlay && isDragging, renderItem: renderItem, disabled: !!(value === null || value === void 0 ? void 0 : value.disabled) }));
}
exports.SortableItem = SortableItem;
//# sourceMappingURL=ImagesSortable.js.map