"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReorderDraggable = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const dnd_1 = require("../../../components/dnd");
const icons_1 = require("../../../components/icons");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../../Utilities/Extensions/ArrayExtensions"));
function ReorderDraggable(props) {
    const { onChange, order, toIdentifier, isOptionDraggable, disabled, } = props;
    const baseClassName = 'ab-ReorderDraggable';
    const renderOption = (option, index) => {
        const identifier = toIdentifier(option);
        const renderNode = (flexProps, dragHandleProps) => {
            const reorderable = isOptionDraggable ? isOptionDraggable(option) : true;
            return (React.createElement(rebass_1.Flex, { className: `${baseClassName}__option`, alignItems: "center", mt: index ? 1 : 0, key: identifier ?? index, backgroundColor: 'primary', padding: 2, "data-index": index, "data-id": identifier, "data-name": "option", ...flexProps },
                React.createElement(rebass_1.Flex, { flex: 1, flexDirection: "row", alignItems: "center" },
                    reorderable ? (React.createElement(rebass_1.Box, { mr: 3, ...dragHandleProps },
                        React.createElement(icons_1.Icon, { name: "drag", style: { cursor: 'grab' } }))) : (React.createElement(rebass_1.Box, { mr: 3 },
                        React.createElement(icons_1.Icon, { name: "drag", style: { cursor: 'not-allowed', opacity: 0.3 } }))),
                    props.renderOption(option, index))));
        };
        const reorderable = isOptionDraggable ? isOptionDraggable(option) : true;
        return (React.createElement(dnd_1.Draggable, { key: identifier, index: index, draggableId: `${identifier}`, isDragDisabled: !reorderable || disabled }, (draggableProvided) => {
            return renderNode({
                ref: draggableProvided.innerRef,
                ...draggableProvided.draggableProps,
                style: draggableProvided.draggableProps.style,
            }, draggableProvided.dragHandleProps);
        }));
    };
    return (React.createElement(rebass_1.Flex, { style: props.style, className: baseClassName, flexDirection: "column", flex: 1 },
        React.createElement(dnd_1.DragDropContext, { onDragEnd: (result) => {
                const { source, destination } = result;
                const newOrder = ArrayExtensions_1.default.reorderArray(props.order, source.index, destination.index);
                onChange(newOrder);
            } },
            React.createElement(rebass_1.Flex, { className: `${baseClassName}__body`, flexDirection: "column", flex: 1, style: { overflow: 'auto' } },
                React.createElement(dnd_1.Droppable, { droppableId: "droppable" }, (droppableProvided) => {
                    return (React.createElement("div", { ref: droppableProvided.innerRef, ...droppableProvided.droppableProps },
                        order.map(renderOption),
                        droppableProvided.placeholder));
                })))));
}
exports.ReorderDraggable = ReorderDraggable;
