/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_settings = require("../../../../../settings.js");
const require_getNodeTextContent = require("../../../../../global/js/utils/getNodeTextContent.js");
const require_useId = require("../../../../../global/js/utils/useId.js");
const require_common = require("./common.js");
const require_DraggableElement = require("../../DraggableElement.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
let _carbon_react = require("@carbon/react");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
let _dnd_kit_core = require("@dnd-kit/core");
let _dnd_kit_modifiers = require("@dnd-kit/modifiers");
//#region src/components/Datagrid/Datagrid/addons/CustomizeColumns/DraggableItemsList.jsx
/**
* Copyright IBM Corp. 2023, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const matchedColsById = (col1, col2) => col1 && col2 && col1.id === col2.id;
const DraggableItemsList = ({ columns, filterString, id, moveElement, onSelectColumn, setAriaRegionText }) => {
	const draggableClass = `${blockClass}__draggable-item`;
	const generatedId = require_useId.useId();
	const visibleCols = columns.filter((colDef) => require_getNodeTextContent.getNodeTextContent(colDef.Header).trim().length !== 0).filter(Boolean).filter((colDef) => !colDef.isAction).filter((colDef) => colDef.id !== "spacer").filter((colDef) => {
		return filterString.length === 0 || require_getNodeTextContent.getNodeTextContent(colDef.Header).toLowerCase().includes(filterString);
	});
	const getUpdatedDragCols = () => {
		const tempCols = [...visibleCols];
		tempCols.forEach((col) => {
			if (col.sticky) col.disabled = true;
		});
		return tempCols;
	};
	const updatedDragCols = getUpdatedDragCols();
	const handleDragEnd = (event) => {
		const { active, over } = event;
		const destOverCol = updatedDragCols.filter((item) => item.id === over.id);
		if (destOverCol?.length && destOverCol[0]?.disabled) return;
		const fromVisibleIndex = updatedDragCols.findIndex((col) => matchedColsById(col, active));
		const toVisibleIndex = updatedDragCols.findIndex((col) => matchedColsById(col, over));
		setAriaRegionText(`${require_getNodeTextContent.getNodeTextContent(updatedDragCols[fromVisibleIndex].Header)} dropped. New position ${toVisibleIndex + 1} of ${updatedDragCols.length}.`);
		moveElement(columns.findIndex((col) => matchedColsById(col, active)), columns.findIndex((col) => matchedColsById(col, over)));
	};
	const handleDragStart = (event) => {
		const { active } = event;
		const fromIndex = updatedDragCols.findIndex((col) => matchedColsById(col, active));
		setAriaRegionText(`${require_getNodeTextContent.getNodeTextContent(updatedDragCols[fromIndex].Header)} grabbed. Current position ${fromIndex + 1} of ${updatedDragCols.length}.`);
	};
	const handleDragUpdate = (event) => {
		const { active, over } = event;
		const fromIndex = updatedDragCols.findIndex((col) => matchedColsById(col, active));
		const toIndex = updatedDragCols.findIndex((col) => matchedColsById(col, over));
		setAriaRegionText(`${require_getNodeTextContent.getNodeTextContent(updatedDragCols[fromIndex].Header)} grabbed. Original position ${fromIndex + 1}, new position ${toIndex + 1} of ${updatedDragCols.length}.`);
	};
	const sensors = (0, _dnd_kit_core.useSensors)((0, _dnd_kit_core.useSensor)(_dnd_kit_core.PointerSensor, { activationConstraint: { distance: 4 } }), (0, _dnd_kit_core.useSensor)(_dnd_kit_core.KeyboardSensor, { coordinateGetter: (event, args) => {
		const { currentCoordinates } = args;
		let target = event.target;
		while (target && !target.classList.contains(draggableClass)) target = target.parentNode;
		const delta = target.offsetHeight;
		switch (event.code) {
			case "ArrowRight":
			case "ArrowLeft": return currentCoordinates;
			case "ArrowUp": return {
				...currentCoordinates,
				y: currentCoordinates.y - delta
			};
			case "ArrowDown": return {
				...currentCoordinates,
				y: currentCoordinates.y + delta
			};
			case "Space": break;
		}
	} }));
	const handleCheckboxKeydown = (event, colDef) => {
		if (event.code === "Space") {
			onSelectColumn(colDef, !require_common.isColumnVisible(colDef));
			event.preventDefault();
			event.stopPropagation();
			return false;
		}
	};
	return /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		collisionDetection: _dnd_kit_core.closestCenter,
		onDragEnd: handleDragEnd,
		onDragStart: handleDragStart,
		onDragMove: handleDragUpdate,
		sensors,
		modifiers: [_dnd_kit_modifiers.restrictToParentElement, _dnd_kit_modifiers.restrictToVerticalAxis]
	}, /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__draggable-underlay`,
		"aria-hidden": "true",
		key: `draggable-underlay-${id}`
	}, visibleCols.map((colDef) => /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__draggable-underlay-item`,
		key: colDef.id
	}))), /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		items: updatedDragCols,
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, visibleCols.map((colDef) => {
		const colHeaderTitle = require_getNodeTextContent.getNodeTextContent(colDef.Header);
		const isFrozenColumn = !!colDef.sticky;
		const isDisabled = colDef.disabled;
		const listContents = /* @__PURE__ */ react.default.createElement(_carbon_react.Checkbox, {
			checked: require_common.isColumnVisible(colDef),
			disabled: isDisabled || isFrozenColumn,
			onChange: (_, { checked }) => onSelectColumn(colDef, checked),
			id: `${blockClass}__customization-column-${colDef.id}`,
			labelText: colHeaderTitle,
			className: `${blockClass}__customize-columns-checkbox`,
			onKeyDown: (event) => handleCheckboxKeydown(event, colDef)
		});
		return /* @__PURE__ */ react.default.createElement(require_DraggableElement.default, {
			classList: draggableClass,
			key: colDef.id,
			id: colDef.id,
			elementId: `${colDef.id}-${generatedId}`,
			disabled: filterString.length > 0 || isFrozenColumn,
			ariaLabel: colHeaderTitle,
			isSticky: isFrozenColumn,
			selected: require_common.isColumnVisible(colDef)
		}, listContents);
	}))));
};
DraggableItemsList.propTypes = {
	columns: prop_types.PropTypes.array.isRequired,
	filterString: prop_types.PropTypes.string.isRequired,
	id: prop_types.PropTypes.string.isRequired,
	moveElement: prop_types.PropTypes.func.isRequired,
	onSelectColumn: prop_types.PropTypes.func.isRequired,
	setAriaRegionText: prop_types.PropTypes.func.isRequired
};
//#endregion
exports.DraggableItemsList = DraggableItemsList;
