/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-a795c5b | Cube Dev Team */
import { chainRaf } from "../../../utils/raf.js";
import { useCallback, useState } from "react";

//#region src/components/navigation/Tabs/use-tab-editing.ts
/**
* Hook to manage tab title editing state.
*
* Provides state and callbacks for inline title editing with support for:
* - Starting edit mode (selecting the tab and focusing input)
* - Submitting changes (with tab-level or parent-level callback)
* - Canceling edits
*/
function useTabEditing({ onChange, onTitleChange } = {}) {
	const [editingKey, setEditingKey] = useState(null);
	const [editValue, setEditValue] = useState("");
	return {
		editingKey,
		editValue,
		setEditValue,
		startEditing: useCallback((key, currentTitle) => {
			onChange?.(key);
			chainRaf(() => {
				setEditingKey(key);
				setEditValue(currentTitle);
			}, 2);
		}, [onChange]),
		cancelEditing: useCallback(() => {
			setEditingKey(null);
			setEditValue("");
		}, []),
		submitEditing: useCallback((key, newTitle, tabOnTitleChange) => {
			const trimmed = newTitle.trim();
			if (trimmed) {
				if (tabOnTitleChange) tabOnTitleChange(trimmed);
				else if (onTitleChange) onTitleChange(key, trimmed);
			}
			setEditingKey(null);
			setEditValue("");
		}, [onTitleChange])
	};
}

//#endregion
export { useTabEditing };
//# sourceMappingURL=use-tab-editing.js.map