/**
 * 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 { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import { pkg } from "../../settings.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import uuidv4 from "../../global/js/utils/uuidv4.js";
import { Kinds, Themes } from "./Checklist.types.js";
import { ChecklistChart } from "./ChecklistChart.js";
import { ChecklistIcon } from "./ChecklistIcon.js";
import React, { useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Button, Heading, IconButton, Section } from "@carbon/react";
import { ChevronUp } from "@carbon/react/icons";

//#region src/components/Checklist/Checklist.tsx
/**
* 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.
*/
/**
* TODO: Breakdown titles, icons, clickable items into sub-components
* See
*   ModifiedTabs (ModifiedTabLabelNew, ModifiedTabLabelWithClose)
*   PageHeader (PageHeaderTitle, PageHeaderUtils)
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--checklist`;
const componentName = "Checklist";
const defaults = {
	checklistAriaLabel: "Checklist",
	checklistToggleAriaLabel: "Checklist toggle",
	onClickViewAll: () => {},
	onToggle: () => {},
	open: true,
	enableToggle: true,
	taskLists: [],
	theme: Themes.light,
	toggleLabel: "Toggle"
};
/**
* The Checklist tracks a user's progress much like Your Learning or
* WalkMe. Each item in the list can be clickable, and each item has
* an icon that defines the item's state as "not started", "in progress",
* and "complete".
*/
const Checklist = React.forwardRef(({ chartValue, chartLabel, checklistAriaLabel = defaults.checklistAriaLabel, checklistToggleAriaLabel = defaults.checklistToggleAriaLabel, className, onClickViewAll = defaults.onClickViewAll, onToggle = defaults.onToggle, open = defaults.open, enableToggle = defaults.enableToggle, taskLists = defaults.taskLists, theme = defaults.theme, title, toggleLabel = defaults.toggleLabel, toggleLabelAlign = "top", viewAllLabel, ...rest }, ref) => {
	const [isOpen, setIsOpen] = useState(open);
	const listContainerId = useRef(uuidv4()).current;
	const chartLabelId = useRef(uuidv4()).current;
	const chartLabelAndValue = typeof chartValue === "number" && chartLabel;
	const handleClickToggle = () => {
		setIsOpen((prevOpen) => !prevOpen);
	};
	const handleClickViewAll = () => {
		onClickViewAll();
	};
	useEffect(() => {
		onToggle(isOpen);
	}, [isOpen, onToggle]);
	useEffect(() => {
		setIsOpen(open);
	}, [open]);
	return /* @__PURE__ */ React.createElement(Section, {
		...rest,
		"aria-label": checklistAriaLabel,
		as: "aside",
		className: (0, import_classnames.default)(blockClass, className, { [`${blockClass}__closed`]: !isOpen }),
		ref,
		...getDevtoolsProps(componentName)
	}, (title || chartLabelAndValue) && /* @__PURE__ */ React.createElement("header", { className: `${blockClass}__header` }, chartLabelAndValue && /* @__PURE__ */ React.createElement(ChecklistChart, {
		"aria-labelledby": chartLabelId,
		theme,
		value: chartValue
	}), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__titles` }, title && /* @__PURE__ */ React.createElement(Heading, { className: `${blockClass}__title` }, title), chartLabelAndValue && /* @__PURE__ */ React.createElement("p", {
		id: chartLabelId,
		className: `${blockClass}__chart-label`
	}, chartLabel)), enableToggle && /* @__PURE__ */ React.createElement(IconButton, {
		align: toggleLabelAlign,
		"aria-controls": listContainerId,
		"aria-expanded": isOpen,
		"aria-label": checklistToggleAriaLabel,
		className: `${blockClass}__toggle`,
		kind: "ghost",
		label: toggleLabel,
		onClick: handleClickToggle,
		size: "sm"
	}, /* @__PURE__ */ React.createElement(ChevronUp, {
		size: 16,
		className: (0, import_classnames.default)(`${blockClass}__chevron`)
	}))), /* @__PURE__ */ React.createElement("div", {
		id: listContainerId,
		className: `${blockClass}__content-outer`
	}, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__content-inner` }, /* @__PURE__ */ React.createElement("div", { className: (0, import_classnames.default)(`${blockClass}__body`) }, taskLists.map((list, index) => {
		return /* @__PURE__ */ React.createElement(Section, {
			className: `${blockClass}__list-group`,
			key: `${list.title}-${index}`
		}, list.title && /* @__PURE__ */ React.createElement(Heading, {
			title: list.title,
			className: `${blockClass}__list-title`
		}, list.title), /* @__PURE__ */ React.createElement("ol", { className: `${blockClass}__list` }, list.tasks.map((item, index) => {
			return /* @__PURE__ */ React.createElement("li", {
				className: `${blockClass}__list-item`,
				key: `${item.label}-${index}`
			}, /* @__PURE__ */ React.createElement(ChecklistIcon, {
				kind: item.kind,
				theme
			}), typeof item.onClick === "function" ? /* @__PURE__ */ React.createElement(Button, {
				className: (0, import_classnames.default)(`${blockClass}__button`, { [`${blockClass}__button--error`]: item.kind === "error" }),
				onClick: () => {
					item.onClick?.(item);
				},
				size: "sm",
				title: item.label
			}, /* @__PURE__ */ React.createElement("div", null, item.label)) : /* @__PURE__ */ React.createElement("div", {
				className: (0, import_classnames.default)(`${blockClass}__label`, `${blockClass}__label--${item.kind}`),
				title: item.label
			}, item.label));
		})));
	})), viewAllLabel && /* @__PURE__ */ React.createElement("footer", { className: `${blockClass}__footer` }, /* @__PURE__ */ React.createElement(Button, {
		className: (0, import_classnames.default)(`${blockClass}__button`, `${blockClass}__view-all`),
		onClick: handleClickViewAll,
		size: "sm"
	}, /* @__PURE__ */ React.createElement("div", null, viewAllLabel))))));
});
Checklist.displayName = componentName;
Checklist.propTypes = {
	chartLabel: PropTypes.node,
	chartValue: PropTypes.number,
	checklistAriaLabel: PropTypes.string,
	checklistToggleAriaLabel: PropTypes.string,
	className: PropTypes.string,
	enableToggle: PropTypes.bool,
	onClickViewAll: PropTypes.func,
	onToggle: PropTypes.func,
	open: PropTypes.bool,
	taskLists: PropTypes.arrayOf(PropTypes.shape({
		title: PropTypes.string,
		tasks: PropTypes.arrayOf(PropTypes.shape({
			kind: PropTypes.oneOf([
				Kinds.unchecked,
				Kinds.indeterminate,
				Kinds.checked,
				Kinds.disabled,
				Kinds.error
			]).isRequired,
			label: PropTypes.string.isRequired,
			onClick: PropTypes.func
		})).isRequired
	})).isRequired,
	theme: PropTypes.oneOf([Themes.light, Themes.dark]),
	title: PropTypes.node,
	toggleLabel: PropTypes.string,
	toggleLabelAlign: PropTypes.oneOf([
		"top",
		"top-left",
		"top-right",
		"bottom",
		"bottom-left",
		"bottom-right",
		"left",
		"right"
	]),
	viewAllLabel: PropTypes.string
};

//#endregion
export { Checklist };