/**
 * 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_uuidv4 = require("../../global/js/utils/uuidv4.js");
const require_index$1 = require("../../node_modules/@carbon/icons-react/lib/index.js");
const require_NavItemLink = require("./NavItemLink.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/Nav/NavItem.jsx
/**
* Copyright IBM Corp. 2024, 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.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
var import_lib = require_index$1.default;
const blockClass = `${require_settings.pkg.prefix}--nav-item`;
const componentName = "NavItem";
const defaults = {
	activeHref: "#",
	disabled: false,
	element: "a",
	onClick: () => {},
	tabIndex: 0
};
/**
* Navigation item component.
*/
let NavItem = ({ activeHref = defaults.activeHref, children = defaults.children, className, current, disabled = defaults.disabled, element = defaults.element, href, id, label, onClick = defaults.onClick, tabIndex = defaults.tabIndex, ...rest }) => {
	const [hrefHasDifferentHost, setHrefHasDifferentHost] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		if (href?.indexOf(window.location.host) === -1) setHrefHasDifferentHost(true);
	}, [href]);
	const instanceId = `${blockClass}__${(0, react.useRef)(require_uuidv4.default()).current}`;
	const navItemId = id || instanceId;
	const externalLink = (/* @__PURE__ */ new RegExp("^([a-z]+://|//)", "i")).test(href) && hrefHasDifferentHost;
	const linkClassName = `${blockClass}__link`;
	const handleDisabled = (action, defaultValue = null) => {
		return !disabled ? action : defaultValue;
	};
	const navItemTabIndex = handleDisabled(tabIndex, -1);
	const externalLinkProps = externalLink && {
		rel: "noopener noreferrer",
		target: "_blank"
	};
	return /* @__PURE__ */ react.default.createElement("li", {
		...rest,
		className: (0, import_classnames.default)(blockClass, className, {
			[`${blockClass}--active`]: current !== null && current === navItemId || activeHref !== void 0 && activeHref === href && !externalLink,
			[`${blockClass}--disabled`]: disabled
		}),
		label,
		onClick: (event) => handleDisabled(onClick(event, href)),
		onKeyDown: (event) => handleDisabled(onClick(event, href)),
		role: "menuitem"
	}, /* @__PURE__ */ react.default.createElement(require_NavItemLink.default, {
		id: navItemId,
		className: (0, import_classnames.default)(linkClassName, { [`${blockClass}__link--external`]: externalLink }),
		element,
		href,
		tabIndex: navItemTabIndex,
		...rest,
		...externalLinkProps
	}, children, externalLink && /* @__PURE__ */ react.default.createElement(import_lib.Launch, { className: `${blockClass}__link--external__icon` })));
};
NavItem.displayName = componentName;
NavItem.propTypes = {
	/**
	* Hypertext reference for active page.
	*/
	activeHref: prop_types.default.string,
	/**
	* Provide the contents of the Nav.
	*/
	children: prop_types.default.node.isRequired,
	/**
	* Provide an optional class to be applied to the containing node.
	*/
	className: prop_types.default.string,
	/**
	* Currently selected item.
	*/
	current: prop_types.default.string,
	/**
	* Whether the item is disabled.
	*/
	disabled: prop_types.default.bool,
	/**
	* The base element to use to build the link. Defaults to `a`, can also accept alternative tag names or custom components like `Link` from `react-router`.
	*/
	element: prop_types.default.elementType,
	/**
	* The href of the nav item.
	*/
	href: prop_types.default.string,
	/**
	* Identifier.
	*/
	id: prop_types.default.string,
	/**
	* Label of an item.
	*/
	label: prop_types.default.string,
	/**
	* Whether the item is a link.
	*/
	link: prop_types.default.bool,
	/**
	* Click handler of an item.
	*/
	onClick: prop_types.default.func,
	/**
	* `tabindex` of an item.
	*/
	tabIndex: prop_types.default.number
};
NavItem = require_settings.pkg.checkComponentEnabled(NavItem, componentName);
var NavItem_default = NavItem;
//#endregion
Object.defineProperty(exports, "NavItem", {
	enumerable: true,
	get: function() {
		return NavItem;
	}
});
exports.blockClass = blockClass;
exports.default = NavItem_default;
