/**
 * 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.
 */

import { pkg } from "../../../../../settings.js";
import { handleCheckboxChange } from "./handleCheckboxChange.js";
import React, { useState } from "react";
import PropTypes from "prop-types";
import { Button, Checkbox, FormGroup } from "@carbon/react";

//#region src/components/Datagrid/Datagrid/addons/Filtering/OverflowCheckboxes.jsx
/**
* Copyright IBM Corp. 2023, 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 componentClass = `${`${pkg.prefix}--datagrid`}-filter-panel`;
const OverflowCheckboxes = ({ components, filtersState, column, setFiltersState, type, applyFilters, viewAllLabel = "View all", viewLessLabel = "View less" }) => {
	const [showAllItems, setShowAllItems] = useState(false);
	const firstFiveItems = filtersState[column].value.slice(0, 5);
	const restOfTheItems = filtersState[column].value.slice(5, filtersState[column].value.length);
	const renderCheckbox = (option) => /* @__PURE__ */ React.createElement(Checkbox, {
		key: option.labelText,
		...option,
		onChange: (_, { checked }) => handleCheckboxChange({
			checked,
			filtersState,
			column,
			option,
			setFiltersState,
			applyFilters,
			type
		}),
		checked: option.selected,
		disabled: option.disabled
	});
	const renderViewAllButtonText = () => {
		/** This should render the text "View all ([amount of checkboxes])" or "View less" */
		return showAllItems ? viewLessLabel : `${viewAllLabel} (${filtersState[column].value.length})`;
	};
	return /* @__PURE__ */ React.createElement(FormGroup, components.FormGroup, firstFiveItems.map(renderCheckbox), showAllItems && restOfTheItems.map(renderCheckbox), /* @__PURE__ */ React.createElement(Button, {
		className: `${componentClass}__view-all-button`,
		kind: "ghost",
		size: "sm",
		onClick: () => setShowAllItems(!showAllItems)
	}, renderViewAllButtonText()));
};
OverflowCheckboxes.propTypes = {
	applyFilters: PropTypes.func,
	column: PropTypes.string,
	components: PropTypes.object,
	filtersState: PropTypes.object,
	setFiltersState: PropTypes.func,
	type: PropTypes.string,
	viewAllLabel: PropTypes.string,
	viewLessLabel: PropTypes.string
};

//#endregion
export { OverflowCheckboxes as default };