"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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.DNDContainer = void 0;
var react_1 = __importStar(require("react"));
var react_dom_1 = require("react-dom");
var react_hooks_1 = require("@devesharp/react-hooks");
var core_1 = require("@dnd-kit/core");
var modifiers_1 = require("@dnd-kit/modifiers");
var sortable_1 = require("@dnd-kit/sortable");
var use_immer_1 = require("use-immer");
var S = __importStar(require("./DNDContainer.style"));
exports.DNDContainer = react_1.default.memo(function DNDContainer(props) {
    var _a = props.containerCardStyle, containerCardStyle = _a === void 0 ? {} : _a, _b = props.horizontal, horizontal = _b === void 0 ? false : _b, _c = props.vertical, vertical = _c === void 0 ? false : _c;
    var _d = use_immer_1.useImmer(props.items.map(function (el, i) { var _a; return (_a = el === null || el === void 0 ? void 0 : el.id.toString()) !== null && _a !== void 0 ? _a : i.toString(); })), items = _d[0], setItems = _d[1];
    react_hooks_1.useDeepEffect(function () {
        setItems(function () { return props.items.map(function (el, i) { var _a; return (_a = el === null || el === void 0 ? void 0 : el.id.toString()) !== null && _a !== void 0 ? _a : i.toString(); }); });
    }, [props.items]);
    var _e = react_1.useState(null), activeId = _e[0], setActiveId = _e[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) {
                    props.onChange(sortable_1.arrayMove(props.items, activeIndex, overIndex), overIndex, activeIndex, props.items[activeIndex]);
                }
            }
        }, onDragCancel: function () { return setActiveId(null); }, modifiers: [modifiers_1.restrictToWindowEdges] },
        react_1.default.createElement(S.Wrapper, { horizontal: horizontal, vertical: vertical },
            react_1.default.createElement(sortable_1.SortableContext, { items: items, strategy: sortable_1.rectSortingStrategy }, items.map(function (value, index) {
                var _a;
                if (!props.items[Number(index)])
                    return null;
                return (react_1.default.createElement(SortableItem, { key: (_a = props.items[Number(index)]) === null || _a === void 0 ? void 0 : _a.id, id: value, value: props.items[Number(index)], index: index, useDragOverlay: true, renderItem: function (_a) {
                        var data = _a.value, listeners = _a.listeners, dragging = _a.dragging, dragOverlay = _a.dragOverlay, disabled = _a.disabled, i = _a.index;
                        return (react_1.default.createElement("div", { style: __assign(__assign({}, containerCardStyle), { flex: 1 }), className: "dnd-item" },
                            react_1.default.createElement("div", __assign({}, listeners, { className: "dnd-item--inner" }), props.renderCard({ data: data, dragOverlay: dragOverlay, dragging: dragging, disabled: disabled, index: i }))));
                    } }));
            }))),
        react_dom_1.createPortal(react_1.default.createElement(core_1.DragOverlay, { adjustScale: true }, activeId ? (react_1.default.createElement(Item, { value: props.items[activeIndex], handle: false, dragOverlay: true, renderItem: function (_a) {
                var data = _a.value, listeners = _a.listeners, dragging = _a.dragging, disabled = _a.disabled, dragOverlay = _a.dragOverlay, index = _a.index;
                return (react_1.default.createElement("div", { style: __assign(__assign({}, containerCardStyle), { flex: 1 }), className: "dnd-item" }, props.renderCard({ data: data, dragOverlay: dragOverlay, dragging: dragging, disabled: disabled, index: index })));
            } })) : null), document.body)));
});
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(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) }));
}
var 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 })));
}));
//# sourceMappingURL=DNDContainer.js.map