/**
 * 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_TearsheetNarrow = require("../../../../Tearsheet/TearsheetNarrow.js");
const require_Columns = require("./Columns.js");
const require_Actions = require("./Actions.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
//#region src/components/Datagrid/Datagrid/addons/CustomizeColumns/CustomizeColumnsTearsheet.jsx
/**
* Copyright IBM Corp. 2022, 2024
*
* 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 CustomizeColumnsTearsheet = ({ isOpen, setIsTearsheetOpen, launcherButtonRef, onSaveColumnPrefs, columnDefinitions, originalColumnDefinitions, customizeTearsheetHeadingLabel = "Customize columns", primaryButtonTextLabel = "Save", secondaryButtonTextLabel = "Cancel", instructionsLabel = "Select columns to display them. Click and drag the box to reorder the columns. These specifications will be saved and persist if you leave and return to the data table.", findColumnPlaceholderLabel = "Find column", resetToDefaultLabel = "Reset to default", assistiveTextInstructionsLabel = "Press space bar to toggle drag drop mode, use arrow keys to move selected elements.", assistiveTextDisabledInstructionsLabel = "Reordering columns are disabled because they are filtered currently.", selectAllLabel = "Column name" }) => {
	const [visibleColumnsCount, setVisibleColumnsCount] = (0, react.useState)("");
	const [totalColumns, setTotalColumns] = (0, react.useState)("");
	const [searchText, setSearchText] = (0, react.useState)("");
	const [columnObjects, setColumnObjects] = (0, react.useState)(columnDefinitions);
	const [isDirty, setIsDirty] = (0, react.useState)(false);
	const prevColumnDefinitions = (0, react.useRef)(void 0);
	const onRequestClose = () => {
		setColumnObjects(columnDefinitions);
		setIsTearsheetOpen(false);
	};
	const onRequestSubmit = () => {
		setIsTearsheetOpen(false);
		onSaveColumnPrefs(columnObjects.map((colDef) => ({
			id: colDef.id,
			isVisible: colDef.isVisible
		})));
	};
	const onCheckboxCheck = (col, value) => {
		const changedDefinitions = columnObjects.map((definition) => {
			if (Array.isArray(col)) return col.includes(definition) && definition.canFilter && !definition.disabled ? {
				...definition,
				isVisible: value
			} : definition;
			return col.id === definition.id ? {
				...definition,
				isVisible: value
			} : definition;
		});
		const selectedColumnsCount = changedDefinitions.filter((definition) => definition.isVisible && !["datagridSelection", "actions"].includes(definition.id)).length;
		setColumnObjects(changedDefinitions.map((definition) => {
			if (selectedColumnsCount > 0) return ["datagridSelection", "actions"].includes(definition.id) ? {
				...definition,
				isVisible: true
			} : definition;
			return ["datagridSelection", "actions"].includes(definition.id) ? {
				...definition,
				isVisible: false
			} : definition;
		}));
		setIsDirty(selectedColumnsCount !== 0);
	};
	const getVisibleColumnsCount = (0, react.useCallback)(() => {
		return columnObjects.filter((col) => col.isVisible).length;
	}, [columnObjects]);
	const string = searchText.trim().toLowerCase();
	(0, react.useEffect)(() => {
		if (!isOpen) return;
		if (prevColumnDefinitions.current !== columnDefinitions) setColumnObjects(columnDefinitions);
		const actionCount = columnObjects.filter((col) => col.id === "actions").length;
		const datagridSelectionCount = columnObjects.filter((col) => col.id === "datagridSelection").length;
		setVisibleColumnsCount(getVisibleColumnsCount() - actionCount - datagridSelectionCount < 0 ? 0 : getVisibleColumnsCount() - actionCount - datagridSelectionCount);
		setTotalColumns(columnObjects.length - actionCount - datagridSelectionCount);
		prevColumnDefinitions.current = columnDefinitions;
	}, [
		getVisibleColumnsCount,
		columnObjects,
		columnDefinitions
	]);
	return /* @__PURE__ */ react.default.createElement(require_TearsheetNarrow.TearsheetNarrow, {
		className: `${blockClass}__customize-columns-tearsheet`,
		open: isOpen,
		title: `${customizeTearsheetHeadingLabel} (${visibleColumnsCount}/${totalColumns})`,
		description: instructionsLabel,
		launcherButtonRef,
		actions: [{
			kind: "secondary",
			label: secondaryButtonTextLabel,
			onClick: onRequestClose
		}, {
			kind: "primary",
			label: primaryButtonTextLabel,
			onClick: onRequestSubmit,
			disabled: !isDirty
		}]
	}, /* @__PURE__ */ react.default.createElement(require_Actions.default, {
		columns: columnObjects,
		originalColumnDefinitions,
		searchText,
		setColumnsObject: (cols) => {
			setColumnObjects(cols);
			setIsDirty(true);
		},
		setSearchText,
		findColumnPlaceholderLabel,
		resetToDefaultLabel
	}), isOpen && /* @__PURE__ */ react.default.createElement(require_Columns.default, {
		assistiveTextInstructionsLabel,
		assistiveTextDisabledInstructionsLabel,
		getVisibleColumnsCount,
		columns: columnObjects,
		filterString: string,
		onSelectColumn: onCheckboxCheck,
		setColumnsObject: (cols) => {
			setColumnObjects(cols);
			setIsDirty(getVisibleColumnsCount() !== 0);
		},
		selectAllLabel,
		customizeTearsheetHeadingLabel
	}));
};
CustomizeColumnsTearsheet.propTypes = {
	assistiveTextDisabledInstructionsLabel: prop_types.default.string,
	assistiveTextInstructionsLabel: prop_types.default.string,
	columnDefinitions: prop_types.default.array.isRequired,
	customizeTearsheetHeadingLabel: prop_types.default.string,
	findColumnPlaceholderLabel: prop_types.default.string,
	instructionsLabel: prop_types.default.string,
	isOpen: prop_types.default.bool.isRequired,
	onSaveColumnPrefs: prop_types.default.func.isRequired,
	originalColumnDefinitions: prop_types.default.array.isRequired,
	primaryButtonTextLabel: prop_types.default.string,
	resetToDefaultLabel: prop_types.default.string,
	secondaryButtonTextLabel: prop_types.default.string,
	selectAllLabel: prop_types.default.string,
	setIsTearsheetOpen: prop_types.default.func.isRequired
};
//#endregion
exports.default = CustomizeColumnsTearsheet;
