import { memo, useRef, useState } from "@wordpress/element";
import Toggle from "../toggle/toggle";
import { DndTitleIcon } from "../../icons/icons";
import { DndContext, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
import { restrictToVerticalAxis, restrictToParentElement } from "@dnd-kit/modifiers";
import { arrayMove, SortableContext, verticalListSortingStrategy, useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import "./dnd-select.scss";
const SortableItem = memo((props) => {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
id: props.id,
disabled: true,
});
const { item, toggleBtn, onChange, disabledItem } = props;
const style = {
transform: CSS.Transform.toString(transform),
transition: transition,
};
return (
<>
{/* Display the label of the item */}
{item?.label}
{item?.pro && (Pro) }
{/* Delete button */}
{toggleBtn && (
onChange(item)}/>
)}
>
);
});
const DragAndDropDnd = ({ label = "", items, toggleBtn = false, onChange, catTabCategoryPosition = "" }) => {
const [allOptions, setAllOptions] = useState(items);
const dndRef = useRef(null);
const containerRef = useRef(null);
const sensors = useSensors(
useSensor(PointerSensor, {
activationConstraint: { distance: 5 },
})
);
const handleDragEnd = (event) => {
const { active, over } = event;
if (active.id !== over.id) {
const oldIndex = allOptions.findIndex((i) => `${i.id}` === `${active.id}`);
const newIndex = allOptions.findIndex((i) => `${i.id}` === `${over.id}`);
const updateValues = arrayMove(allOptions, oldIndex, newIndex);
onChange(updateValues);
setAllOptions(updateValues);
// const taxonomyIndex = updateValues?.findIndex(
// ( item ) => item.value === 'taxonomy'
// );
// const titleIndex = updateValues.findIndex(
// ( item ) => item.value === 'title'
// );
// if (
// taxonomyIndex !== -1 &&
// titleIndex !== -1 &&
// taxonomyIndex !== titleIndex - 1 &&
// setAttributes
// ) {
// setAttributes( { catTabCategoryPosition: '' } );
// }
}
};
const onToggleControl = (item) => {
const updatedValues = allOptions?.map((val) => {
if (val.id === item.id) {
return { ...val, show: !val.show };
}
return val;
});
setAllOptions(updatedValues);
onChange(updatedValues);
};
return (
{label && (
)}
{!["", "above-title"].includes(catTabCategoryPosition) && (
)}
`${item.id}`)}
strategy={verticalListSortingStrategy}
>
{["", "above-title"].includes(catTabCategoryPosition)
? allOptions?.map((item, index) => (
))
: allOptions
?.filter((t) => t.value !== "taxonomy")
.map((item, index) => (
))}
);
};
export default memo(DragAndDropDnd);