const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_styles = require("./styles.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _pisell_icon_es_ChevronRight = require("@pisell/icon/es/ChevronRight");
_pisell_icon_es_ChevronRight = require_runtime.__toESM(_pisell_icon_es_ChevronRight);
let _pisell_icon_es_ChevronLeft = require("@pisell/icon/es/ChevronLeft");
_pisell_icon_es_ChevronLeft = require_runtime.__toESM(_pisell_icon_es_ChevronLeft);
//#region src/plus/productSelect/components/TabsStructure/TabsStructure.tsx
const _excluded = ["children", "className"], _excluded2 = [
	"children",
	"isSelected",
	"isDisabled",
	"className",
	"onClick",
	"tabId",
	"autoRegister"
], _excluded3 = ["children", "className"], _excluded4 = [
	"focusRingClasses",
	"isFocusVisible",
	"style",
	"className"
], _excluded5 = [
	"visible",
	"location",
	"className"
], _excluded6 = [
	"direction",
	"disabled",
	"onClick",
	"className",
	"aria-label"
], _excluded7 = [
	"isPrevDisabled",
	"isNextDisabled",
	"onPrevClick",
	"onNextClick",
	"prevAction",
	"nextAction",
	"className"
], _excluded8 = [
	"selectedTab",
	"onTabChange",
	"autoScrollToSelected",
	"children",
	"className",
	"railRef"
];
const TabContext = (0, react.createContext)({});
const useTabContext = () => {
	const context = (0, react.useContext)(TabContext);
	if (!context) throw new Error("useTabContext must be used within a TabContext.Provider");
	return context;
};
const TabListContainer = (0, react.forwardRef)((_ref, ref) => {
	let { children, className } = _ref, props = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	return /* @__PURE__ */ react.default.createElement("ul", require_objectSpread2._objectSpread2({
		ref,
		className: (0, classnames.default)(require_styles.tabListClasses.list, className),
		role: "tablist"
	}, props), children);
});
const TabItem = (0, react.forwardRef)((_ref2, ref) => {
	let { children, isSelected = false, isDisabled = false, className, onClick, tabId, autoRegister = true } = _ref2, props = require_objectWithoutProperties._objectWithoutProperties(_ref2, _excluded2);
	const { selectedTab, onTabChange, registerTab, unregisterTab } = useTabContext();
	const elementRef = (0, react.useRef)(null);
	const combinedRef = (0, react.useCallback)((element) => {
		elementRef.current = element;
		if (typeof ref === "function") ref(element);
		else if (ref && "current" in ref) ref.current = element;
	}, [ref]);
	(0, react.useEffect)(() => {
		if (autoRegister && tabId && elementRef.current && registerTab) {
			registerTab(tabId, elementRef.current);
			return () => {
				if (unregisterTab) unregisterTab(tabId);
			};
		}
	}, [
		tabId,
		autoRegister,
		registerTab,
		unregisterTab
	]);
	const handleClick = (0, react.useCallback)(() => {
		if (!isDisabled) {
			if (tabId && onTabChange) onTabChange(tabId);
			onClick === null || onClick === void 0 || onClick(tabId);
		}
	}, [
		isDisabled,
		tabId,
		onTabChange,
		onClick
	]);
	const isTabSelected = tabId ? selectedTab === tabId : isSelected;
	return /* @__PURE__ */ react.default.createElement("li", require_objectSpread2._objectSpread2({
		id: tabId,
		ref: combinedRef,
		className: (0, classnames.default)(require_styles.tabItemClasses.item, isTabSelected && require_styles.tabItemClasses.selected, isDisabled && require_styles.tabItemClasses.disabled, className),
		role: "tab",
		"aria-selected": isTabSelected,
		"aria-disabled": isDisabled,
		disabled: isDisabled,
		onClick: handleClick,
		tabIndex: isTabSelected ? 0 : -1
	}, props), /* @__PURE__ */ react.default.createElement("span", null, children));
});
const Rail = (0, react.forwardRef)((_ref3, ref) => {
	let { children, className } = _ref3, props = require_objectWithoutProperties._objectWithoutProperties(_ref3, _excluded3);
	const { railRef: contextRailRef } = useTabContext();
	const mergedRef = (0, react.useCallback)((element) => {
		if (contextRailRef && "current" in contextRailRef) contextRailRef.current = element;
		if (typeof ref === "function") ref(element);
		else if (ref && "current" in ref) ref.current = element;
	}, [ref, contextRailRef]);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		ref: mergedRef,
		className: (0, classnames.default)(require_styles.railClasses.rail, className)
	}, props), children);
});
const SelectionIndicator = (0, react.forwardRef)((_ref4, ref) => {
	let { focusRingClasses, isFocusVisible = false, style, className } = _ref4, props = require_objectWithoutProperties._objectWithoutProperties(_ref4, _excluded4);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		ref,
		className: (0, classnames.default)(require_styles.selectionClasses.selection, require_styles.selectionClasses.position, { [require_styles.selectionClasses.focused]: isFocusVisible && focusRingClasses }, className),
		style,
		"aria-hidden": "true"
	}, props));
});
const ScrollShadow = (0, react.forwardRef)((_ref5, ref) => {
	let { visible, location, className } = _ref5, props = require_objectWithoutProperties._objectWithoutProperties(_ref5, _excluded5);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		ref,
		className: (0, classnames.default)(require_styles.shadowClasses.shadow, {
			[require_styles.shadowClasses.isVisible]: visible,
			[require_styles.shadowClasses.locationStart]: location === "start",
			[require_styles.shadowClasses.locationEnd]: location === "end"
		}, className)
	}, props));
});
const ChevronButton = (0, react.forwardRef)((_ref6, ref) => {
	let { direction, disabled = false, onClick, className, "aria-label": ariaLabel } = _ref6, props = require_objectWithoutProperties._objectWithoutProperties(_ref6, _excluded6);
	const defaultAriaLabel = direction === "prev" ? "prev" : "next";
	return /* @__PURE__ */ react.default.createElement("button", require_objectSpread2._objectSpread2({
		ref,
		className: (0, classnames.default)(require_styles.chevronButtonClasses.button, {
			[require_styles.chevronButtonClasses.disabled]: disabled,
			[require_styles.chevronButtonClasses.prev]: direction === "prev",
			[require_styles.chevronButtonClasses.next]: direction === "next"
		}, className),
		disabled,
		onClick,
		"aria-label": ariaLabel || defaultAriaLabel,
		type: "button"
	}, props), direction === "prev" ? /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronLeft.default, null) : /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronRight.default, null));
});
const ChevronControls = (_ref7) => {
	let { isPrevDisabled = false, isNextDisabled = false, onPrevClick, onNextClick, prevAction, nextAction, className } = _ref7, props = require_objectWithoutProperties._objectWithoutProperties(_ref7, _excluded7);
	const handlePrevClick = onPrevClick || prevAction;
	const handleNextClick = onNextClick || nextAction;
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: (0, classnames.default)(require_styles.chevronClasses.controls, className) }, props), /* @__PURE__ */ react.default.createElement(ChevronButton, {
		direction: "prev",
		disabled: isPrevDisabled,
		onClick: handlePrevClick
	}), /* @__PURE__ */ react.default.createElement(ChevronButton, {
		direction: "next",
		disabled: isNextDisabled,
		onClick: handleNextClick
	}));
};
const TabsStructure = (_ref8) => {
	let { selectedTab, onTabChange, autoScrollToSelected = true, children, className, railRef: externalRailRef } = _ref8, props = require_objectWithoutProperties._objectWithoutProperties(_ref8, _excluded8);
	const tabElementsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
	const internalRailRef = (0, react.useRef)(null);
	const railRef = externalRailRef || internalRailRef;
	if (!selectedTab && children) console.warn("TabsStructure: No selectedTab provided, consider setting a default value");
	const registerTab = (0, react.useCallback)((tabId, element) => {
		if (!tabId || !element) {
			console.warn("TabsStructure: Invalid tabId or element provided to registerTab");
			return;
		}
		if (typeof WeakRef !== "undefined") tabElementsRef.current.set(tabId, new WeakRef(element));
		else tabElementsRef.current.set(tabId, element);
	}, []);
	const unregisterTab = (0, react.useCallback)((tabId) => {
		if (tabId) tabElementsRef.current.delete(tabId);
	}, []);
	const clearAllTabs = (0, react.useCallback)(() => {
		tabElementsRef.current.clear();
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			if (typeof WeakRef === "undefined") clearAllTabs();
		};
	}, [clearAllTabs]);
	const getTabElement = (0, react.useCallback)((tabId) => {
		const ref = tabElementsRef.current.get(tabId);
		if (!ref) return null;
		if (typeof WeakRef !== "undefined" && ref instanceof WeakRef) {
			const element = ref.deref();
			if (!element) {
				tabElementsRef.current.delete(tabId);
				return null;
			}
			return element;
		} else return ref;
	}, []);
	const scrollToTab = (0, react.useCallback)((tabId) => {
		if (!tabId || !railRef.current) return;
		const targetElement = getTabElement(tabId);
		if (!targetElement) {
			console.warn(`TabsStructure: Tab element with id "${tabId}" not found`);
			return;
		}
		if (!targetElement.offsetParent) {
			console.warn(`TabsStructure: Tab element "${tabId}" is not visible or not in DOM`);
			return;
		}
		const rail = railRef.current;
		const targetLeft = targetElement.offsetLeft;
		const currentScrollLeft = rail.scrollLeft;
		if (Math.abs(targetLeft - currentScrollLeft) > 1) require_utils.safeScrollTo(rail, {
			left: targetLeft,
			behavior: "smooth"
		});
	}, [getTabElement]);
	(0, react.useEffect)(() => {
		if (autoScrollToSelected && selectedTab) scrollToTab(selectedTab);
	}, [
		selectedTab,
		autoScrollToSelected,
		scrollToTab
	]);
	const contextValue = (0, react.useMemo)(() => ({
		selectedTab,
		onTabChange,
		registerTab,
		unregisterTab,
		railRef
	}), [
		selectedTab,
		onTabChange,
		registerTab,
		unregisterTab,
		railRef
	]);
	return /* @__PURE__ */ react.default.createElement(TabContext.Provider, { value: contextValue }, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, { className: (0, classnames.default)(require_styles.tabWrapperClasses.wrapper, className) }), children));
};
const TabsStructureWithSubComponents = TabsStructure;
TabsStructureWithSubComponents.Rail = Rail;
TabsStructureWithSubComponents.ListContainer = TabListContainer;
TabsStructureWithSubComponents.Item = TabItem;
TabsStructureWithSubComponents.SelectionIndicator = SelectionIndicator;
TabsStructureWithSubComponents.ScrollShadow = ScrollShadow;
TabsStructureWithSubComponents.ChevronControls = ChevronControls;
TabsStructureWithSubComponents.ChevronButton = ChevronButton;
TabsStructure.displayName = "TabsStructure";
Rail.displayName = "TabsStructure.Rail";
TabListContainer.displayName = "TabsStructure.ListContainer";
TabItem.displayName = "TabsStructure.Item";
SelectionIndicator.displayName = "TabsStructure.SelectionIndicator";
ScrollShadow.displayName = "TabsStructure.ScrollShadow";
ChevronControls.displayName = "TabsStructure.ChevronControls";
ChevronButton.displayName = "TabsStructure.ChevronButton";
//#endregion
exports.default = TabsStructureWithSubComponents;
