/**
 * 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 { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { allPropTypes } from "../../global/js/utils/props-helper.js";
import { StringFormatterAlignment, deprecated_StringFormatterAlignment, propMappingFunction } from "./utils/enums.js";
import React, { useRef, useState } from "react";
import PropTypes from "prop-types";
import { DefinitionTooltip } from "@carbon/react";

//#region src/components/StringFormatter/StringFormatter.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__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--string-formatter`;
const componentName = "StringFormatter";
const defaults = {
	autoAlign: false,
	lines: 1,
	tooltipDirection: StringFormatterAlignment.BOTTOM_START,
	truncate: false,
	width: null
};
/**
* StringFormatter allows for truncating text while displaying a tooltip
* overlay on hover or focus with the entirety of the provided copy.
* @deprecated This component is deprecated and will be removed in the next major version. Please use TruncatedText instead.
*/
let StringFormatter = React.forwardRef(({ className, autoAlign = defaults.autoAlign, lines = defaults.lines, tooltipDirection = defaults.tooltipDirection, truncate = defaults.truncate, width = defaults.width, value, ...rest }, ref) => {
	const outerRef = useRef(null);
	const contentRef = useRef(null);
	const [isTextTruncated, setIsTextTruncated] = useState(false);
	const mergedRefs = (node) => {
		outerRef.current = node;
		if (typeof ref === "function") ref(node);
		else if (ref) ref.current = node;
	};
	useIsomorphicEffect(() => {
		const checkTruncation = () => {
			const element = contentRef.current;
			if (element) {
				element.style.webkitLineClamp = truncate ? lines : void 0;
				element.style.maxWidth = width;
				const buffer = element.clientHeight / (2 * lines);
				setIsTextTruncated(element.scrollHeight > element.clientHeight + buffer);
			}
		};
		const resizeObserver = new ResizeObserver(checkTruncation);
		if (outerRef.current) {
			resizeObserver.observe(outerRef.current);
			checkTruncation();
		}
		return () => {
			resizeObserver.disconnect();
		};
	}, [
		lines,
		value,
		width,
		truncate
	]);
	const stringFormatterContent = /* @__PURE__ */ React.createElement("span", {
		ref: contentRef,
		className: (0, import_classnames.default)(`${blockClass}--content`, { [`${blockClass}--truncate`]: truncate })
	}, value);
	return /* @__PURE__ */ React.createElement("span", {
		...rest,
		className: (0, import_classnames.default)(blockClass, className),
		ref: mergedRefs,
		...getDevtoolsProps(componentName)
	}, truncate && isTextTruncated ? /* @__PURE__ */ React.createElement(DefinitionTooltip, {
		className: `${blockClass}__tooltip`,
		align: tooltipDirection,
		autoAlign,
		definition: value,
		openOnHover: true
	}, stringFormatterContent) : stringFormatterContent);
});
/**@ts-ignore*/
StringFormatter.deprecated = {
	level: "warn",
	details: `Please replace ${componentName} with TruncatedText`
};
StringFormatter = pkg.checkComponentEnabled(StringFormatter, componentName);
StringFormatter.displayName = componentName;
StringFormatter.validateAlignment = () => (props, propName, componentName) => {
	const prop = props[propName];
	if (Object.values(deprecated_StringFormatterAlignment).includes(prop)) {
		const mappedNewProp = propMappingFunction(prop);
		console.warn(`"${prop}" is a deprecated value for the "${propName}" prop on the "${componentName}" component. Use "${mappedNewProp}" instead. Allowable values are: ${Object.values(StringFormatterAlignment).join(", ")}.`);
	}
};
StringFormatter.propTypes = {
	className: PropTypes.string,
	lines: PropTypes.number,
	tooltipDirection: allPropTypes([StringFormatter.validateAlignment(), PropTypes.oneOf(Object.values(deprecated_StringFormatterAlignment), Object.values(StringFormatterAlignment))]),
	truncate: PropTypes.bool,
	value: PropTypes.string.isRequired,
	width: PropTypes.string
};

//#endregion
export { StringFormatter };