/**
 * 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_index = require('../../node_modules/classnames/index.js');
const require_settings = require('../../settings.js');
const require_DatagridAiLabel = require('./Datagrid/addons/AiLabel/DatagridAiLabel.js');
const require_DatagridSelectAll = require('./Datagrid/DatagridSelectAll.js');
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@carbon/react");
let _carbon_react_icons = require("@carbon/react/icons");

//#region src/components/Datagrid/useSortableColumns.tsx
/**
* Copyright IBM Corp. 2020, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const getNewSortOrder = (currentOrder) => {
	const order = {
		newOrder: "NONE",
		newSortDesc: void 0
	};
	if (currentOrder === void 0) {
		order.newOrder = "ASC";
		order.newSortDesc = false;
	}
	if (currentOrder === false || currentOrder === "DESC") {
		order.newOrder = "DESC";
		order.newSortDesc = true;
	}
	if (currentOrder === true || currentOrder === "ASC") {
		order.newOrder = "NONE";
		order.newSortDesc = void 0;
	}
	return order;
};
const getAriaPressedValue = (col) => {
	if (!col) return;
	const { isSorted } = col;
	if (isSorted) return "true";
	return "false";
};
const useSortableColumns = (hooks) => {
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const sortableVisibleColumns = (visibleColumns, { instance }) => {
		const { onSort } = instance;
		const onSortClick = (event, column) => {
			if (event.target.classList.contains(`${carbonPrefix}--slug`) || event.target.closest(`.${carbonPrefix}--slug`)) {
				event.stopPropagation();
				return;
			}
			const key = column.id;
			const sortDesc = column.isSortedDesc;
			const { newSortDesc, newOrder } = getNewSortOrder(sortDesc);
			if (onSort) onSort(key, newOrder);
			instance.toggleSortBy(key, newSortDesc, false);
		};
		const sortableColumns = visibleColumns.map((column) => {
			const icon = (col, props) => {
				const iconProps = {
					size: 16,
					...props,
					className: `${blockClass}__sortable-icon ${carbonPrefix}--btn__icon`
				};
				if (col?.isSorted) switch (col.isSortedDesc) {
					case false: return /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ArrowUp, iconProps);
					case true: return /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ArrowDown, iconProps);
					default: return /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ArrowsVertical, iconProps);
				}
				return /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ArrowsVertical, iconProps);
			};
			const handleKey = (e, columnId) => {
				const { key } = e;
				if (key === "Enter") setTimeout(() => document.getElementById(columnId)?.focus(), 0);
			};
			const Header = (headerProp) => column.disableSortBy === true || column.id === "datagridSelection" || column.isAction ? column.disableSortBy || column.isAction ? column.Header : /* @__PURE__ */ react.default.createElement(require_DatagridSelectAll.SelectAll, instance) : /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
				"aria-pressed": getAriaPressedValue(headerProp?.column),
				onClick: (event) => onSortClick(event, headerProp?.column),
				kind: "ghost",
				renderIcon: (props) => {
					return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_DatagridAiLabel.DatagridAILabel, { aiLabel: headerProp?.column?.aiLabel || headerProp?.column?.slug }), icon(headerProp?.column, props));
				},
				id: column?.id,
				className: (0, import_classnames.default)(`${carbonPrefix}--table-sort ${blockClass}--table-sort`, {
					[`${blockClass}--table-sort--desc`]: headerProp?.column.isSortedDesc,
					[`${blockClass}--table-sort--asc`]: headerProp?.column.isSortedDesc === false
				}),
				onKeyDown: (event) => handleKey(event, column.id)
			}, column.Header);
			return {
				...column,
				Header,
				minWidth: column.minWidth ?? (column.disableSortBy ? 50 : column.isAction ? 50 : 90)
			};
		});
		return instance.customizeColumnsProps?.isTearsheetOpen ? visibleColumns : [...sortableColumns];
	};
	const sortInstanceProps = (instance) => {
		const { onSort } = instance;
		Object.assign(instance, {
			manualSortBy: !!onSort,
			isTableSortable: true
		});
	};
	hooks.visibleColumns.push(sortableVisibleColumns);
	hooks.useInstance.push(sortInstanceProps);
};

//#endregion
exports.default = useSortableColumns;
exports.getNewSortOrder = getNewSortOrder;