/**
 * 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_useClickOutside = require('../../../global/js/hooks/useClickOutside.js');
const require_index$1 = require('../../FeatureFlags/index.js');
const require_useMultipleKeyTracking = require('../../DataSpreadsheet/hooks/useMultipleKeyTracking.js');
const require_InlineEditContext = require('./addons/InlineEdit/InlineEditContext/InlineEditContext.js');
const require_constants = require('./addons/Filtering/constants.js');
const require_FilterProvider = require('./addons/Filtering/FilterProvider.js');
const require_useSubscribeToEventEmitter = require('./addons/Filtering/hooks/useSubscribeToEventEmitter.js');
const require_FilterPanel = require('./addons/Filtering/FilterPanel.js');
const require_DatagridHead = require('./DatagridHead.js');
const require_DatagridBody = require('./DatagridBody.js');
const require_DatagridToolbar = require('./DatagridToolbar.js');
const require_FilterSummary = require('../../FilterSummary/FilterSummary.js');
const require_handleGridFocus = require('./addons/InlineEdit/handleGridFocus.js');
const require_handleGridKeyPress = require('./addons/InlineEdit/handleGridKeyPress.js');
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
let _carbon_layout = require("@carbon/layout");

//#region src/components/Datagrid/Datagrid/DatagridContent.tsx
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const gcClass = `${blockClass}__grid-container`;
const DatagridContent = ({ datagridState, ariaToolbarLabel, title }) => {
	const { state: inlineEditState, dispatch } = (0, react.useContext)(require_InlineEditContext.InlineEditContext);
	const { filterTags, EventEmitter, panelOpen, tableId: contextTableId } = (0, react.useContext)(require_FilterProvider.FilterContext);
	const { activeCellId, gridActive, editId, featureFlags } = inlineEditState;
	const { getTableProps, getFilterFlyoutProps, withVirtualScroll, DatagridPagination, isFetching, CustomizeColumnsTearsheet, filterProps, fullHeightDatagrid, verticalAlign = "center", variableRowHeight, gridTitle, gridDescription, useDenseHeader, withInlineEdit, tableId, DatagridActions, totalColumnsWidth, gridRef, setAllFilters, state, page, rows } = datagridState;
	const { columnResizing } = state;
	const contentRows = DatagridPagination && page || rows;
	const gridAreaRef = (0, react.useRef)(null);
	const multiKeyTrackingRef = (0, react.useRef)(null);
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const enableEditableCell = require_index$1.useFeatureFlag("enable-datagrid-useEditableCell");
	const enableInlineEdit = require_index$1.useFeatureFlag("enable-datagrid-useInlineEdit");
	const enableCustomizeCols = require_index$1.useFeatureFlag("enable-datagrid-useCustomizeColumns");
	(0, react.useEffect)(() => {
		dispatch({
			type: "SET_FEATURE_FLAGS",
			payload: {
				"enable-datagrid-useEditableCell": enableEditableCell,
				"enable-datagrid-useInlineEdit": enableInlineEdit,
				"enable-datagrid-useCustomizeColumns": enableCustomizeCols
			}
		});
	}, [
		dispatch,
		enableEditableCell,
		enableCustomizeCols,
		enableInlineEdit
	]);
	(0, react.useEffect)(() => {
		if (featureFlags && (featureFlags?.["enable-datagrid-useEditableCell"] || featureFlags?.["enable-datagrid-useInlineEdit"])) console.error(`Datagrid useEditableCell/useInlineEdit extension has not been enabled via feature flag.`);
	}, [featureFlags]);
	require_useClickOutside.useClickOutside(gridAreaRef, (target) => {
		if (!withInlineEdit) return;
		if (target.closest(`.${blockClass}__inline-edit-button`) || target.closest(`.${blockClass}__inline-edit--select`)) return;
		dispatch({
			type: "REMOVE_GRID_ACTIVE_FOCUS",
			payload: activeCellId
		});
	});
	const renderTable = () => {
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_carbon_react.Table, {
			...getTableProps?.(),
			className: (0, import_classnames.default)(withVirtualScroll ? `${blockClass}__table-virtual-scroll` : `${blockClass}__table-simple`, `${blockClass}__vertical-align-${verticalAlign}`, { [`${blockClass}__variable-row-height`]: variableRowHeight }, { [`${blockClass}__table-with-inline-edit`]: withInlineEdit }, { [`${blockClass}__table-grid-active`]: gridActive }, { [`${blockClass}__table-is-resizing`]: typeof columnResizing?.isResizingColumn === "string" }, getTableProps?.().className),
			role: withInlineEdit ? "grid" : void 0,
			tabIndex: withInlineEdit ? 0 : -1,
			onKeyDown: withInlineEdit && ((event) => require_handleGridKeyPress.handleGridKeyPress({
				event,
				dispatch,
				instance: datagridState,
				keysPressedList,
				state: inlineEditState,
				usingMac,
				ref: multiKeyTrackingRef,
				carbonPrefix
			})),
			onFocus: withInlineEdit && (() => require_handleGridFocus.handleGridFocus(inlineEditState, dispatch)),
			title
		}, (!withVirtualScroll || withVirtualScroll && !isFetching && !contentRows.length) && /* @__PURE__ */ react.default.createElement(require_DatagridHead.default, datagridState), /* @__PURE__ */ react.default.createElement(require_DatagridBody.default, {
			...datagridState,
			rows: contentRows
		})), filterProps?.variation === "panel" && renderPagination());
	};
	const { keysPressedList, usingMac } = require_useMultipleKeyTracking.useMultipleKeyTracking({
		ref: withInlineEdit ? multiKeyTrackingRef : null,
		containerHasFocus: gridActive,
		isEditing: !!editId
	});
	(0, react.useEffect)(() => {
		if (!withInlineEdit) return;
		const gridElement = document.querySelector(`#${tableId}`);
		const tableHeader = gridElement?.querySelector(`.${carbonPrefix}--data-table-header`);
		gridElement?.style?.setProperty(`--${blockClass}--grid-width`, (0, _carbon_layout.px)((totalColumnsWidth || 0) + 32));
		if (gridActive) gridElement?.style.setProperty(`--${blockClass}--grid-header-height`, (0, _carbon_layout.px)(tableHeader?.clientHeight || 0));
	}, [
		withInlineEdit,
		tableId,
		totalColumnsWidth,
		datagridState,
		gridActive,
		carbonPrefix
	]);
	require_useSubscribeToEventEmitter.default(require_constants.CLEAR_SINGLE_FILTER, (id) => require_FilterProvider.clearSingleFilter(id, setAllFilters, state, contextTableId));
	const renderFilterSummary = () => state.filters.length > 0 && /* @__PURE__ */ react.default.createElement(require_FilterSummary.default, {
		className: `${blockClass}__filter-summary`,
		filters: filterTags,
		clearFilters: () => {
			EventEmitter.dispatch(require_constants.CLEAR_FILTERS, tableId);
			if (typeof filterProps?.onClearFilters === "function") filterProps.onClearFilters();
		},
		renderLabel: filterProps?.renderLabel,
		overflowType: "tag"
	});
	const renderPagination = () => {
		if (contentRows?.length > 0 && !isFetching && DatagridPagination) return /* @__PURE__ */ react.default.createElement(DatagridPagination, datagridState);
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_carbon_react.TableContainer, {
		className: (0, import_classnames.default)(`${gcClass}`, {
			[`${gcClass}-active`]: gridActive,
			[`${gcClass}-active--without-toolbar`]: withInlineEdit && !DatagridActions,
			[`${gcClass}-inline-edit`]: withInlineEdit,
			[`${blockClass}__full-height`]: withVirtualScroll || fullHeightDatagrid,
			[`${blockClass}__with-pagination`]: DatagridPagination,
			[`${blockClass}__dense-header`]: useDenseHeader
		}),
		title: gridTitle,
		description: gridDescription
	}, /* @__PURE__ */ react.default.createElement(require_DatagridToolbar.default, {
		...datagridState,
		ariaToolbarLabel
	}), /* @__PURE__ */ react.default.createElement("div", {
		className: (0, import_classnames.default)(`${blockClass}__table-container`, { [`${blockClass}__table-container--filter-open`]: panelOpen }),
		ref: gridAreaRef
	}, filterProps?.variation === "panel" && /* @__PURE__ */ react.default.createElement(require_FilterPanel.default, {
		updateMethod: void 0,
		setAllFilters: void 0,
		...getFilterFlyoutProps?.(),
		title: filterProps.panelTitle,
		filterSections: filterProps.sections,
		autoHideFilters: filterProps.autoHideFilters,
		isFetching
	}), /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__table-container-inner` }, renderFilterSummary(), withInlineEdit ? /* @__PURE__ */ react.default.createElement("div", { ref: multiKeyTrackingRef }, renderTable()) : withVirtualScroll ? /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__virtualScrollContainer`,
		ref: gridRef
	}, renderTable()) : renderTable()))), filterProps?.variation !== "panel" && renderPagination(), CustomizeColumnsTearsheet && /* @__PURE__ */ react.default.createElement(CustomizeColumnsTearsheet, { instance: datagridState }));
};
DatagridContent.propTypes = {
	ariaToolbarLabel: prop_types.default.string,
	datagridState: prop_types.default.shape({
		getTableProps: prop_types.default.func,
		getFilterFlyoutProps: prop_types.default.func,
		withVirtualScroll: prop_types.default.bool,
		DatagridActions: prop_types.default.oneOfType([prop_types.default.element, prop_types.default.func]),
		DatagridPagination: prop_types.default.oneOfType([prop_types.default.element, prop_types.default.func]),
		CustomizeColumnsTearsheet: prop_types.default.oneOfType([prop_types.default.element, prop_types.default.func]),
		isFetching: prop_types.default.bool,
		skeletonRowCount: prop_types.default.number,
		fullHeightDatagrid: prop_types.default.bool,
		filterProps: prop_types.default.object,
		variableRowHeight: prop_types.default.bool,
		useDenseHeader: prop_types.default.bool,
		withInlineEdit: prop_types.default.bool,
		verticalAlign: prop_types.default.string,
		gridTitle: prop_types.default.node,
		gridDescription: prop_types.default.node,
		page: prop_types.default.arrayOf(prop_types.default.object),
		rows: prop_types.default.arrayOf(prop_types.default.object),
		tableId: prop_types.default.string,
		totalColumnsWidth: prop_types.default.number,
		gridRef: prop_types.default.object,
		setAllFilters: prop_types.default.func,
		getFilterProps: prop_types.default.func,
		state: prop_types.default.object
	}),
	title: prop_types.default.string
};

//#endregion
exports.DatagridContent = DatagridContent;