import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { chevronButtonClasses, chevronClasses, railClasses, selectionClasses, shadowClasses, tabItemClasses, tabListClasses, tabWrapperClasses } from "./styles.js";
import { safeScrollTo } from "./utils.js";
import React, { createContext, forwardRef, useCallback, useContext, useEffect, useMemo, useRef } from "react";
import classNames from "classnames";
import ChevronRight from "@pisell/icon/es/ChevronRight";
import ChevronLeft from "@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 = createContext({});
const useTabContext = () => {
	const context = useContext(TabContext);
	if (!context) throw new Error("useTabContext must be used within a TabContext.Provider");
	return context;
};
const TabListContainer = forwardRef((_ref, ref) => {
	let { children, className } = _ref, props = _objectWithoutProperties(_ref, _excluded);
	return /* @__PURE__ */ React.createElement("ul", _objectSpread2({
		ref,
		className: classNames(tabListClasses.list, className),
		role: "tablist"
	}, props), children);
});
const TabItem = forwardRef((_ref2, ref) => {
	let { children, isSelected = false, isDisabled = false, className, onClick, tabId, autoRegister = true } = _ref2, props = _objectWithoutProperties(_ref2, _excluded2);
	const { selectedTab, onTabChange, registerTab, unregisterTab } = useTabContext();
	const elementRef = useRef(null);
	const combinedRef = useCallback((element) => {
		elementRef.current = element;
		if (typeof ref === "function") ref(element);
		else if (ref && "current" in ref) ref.current = element;
	}, [ref]);
	useEffect(() => {
		if (autoRegister && tabId && elementRef.current && registerTab) {
			registerTab(tabId, elementRef.current);
			return () => {
				if (unregisterTab) unregisterTab(tabId);
			};
		}
	}, [
		tabId,
		autoRegister,
		registerTab,
		unregisterTab
	]);
	const handleClick = 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.createElement("li", _objectSpread2({
		id: tabId,
		ref: combinedRef,
		className: classNames(tabItemClasses.item, isTabSelected && tabItemClasses.selected, isDisabled && tabItemClasses.disabled, className),
		role: "tab",
		"aria-selected": isTabSelected,
		"aria-disabled": isDisabled,
		disabled: isDisabled,
		onClick: handleClick,
		tabIndex: isTabSelected ? 0 : -1
	}, props), /* @__PURE__ */ React.createElement("span", null, children));
});
const Rail = forwardRef((_ref3, ref) => {
	let { children, className } = _ref3, props = _objectWithoutProperties(_ref3, _excluded3);
	const { railRef: contextRailRef } = useTabContext();
	const mergedRef = 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.createElement("div", _objectSpread2({
		ref: mergedRef,
		className: classNames(railClasses.rail, className)
	}, props), children);
});
const SelectionIndicator = forwardRef((_ref4, ref) => {
	let { focusRingClasses, isFocusVisible = false, style, className } = _ref4, props = _objectWithoutProperties(_ref4, _excluded4);
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({
		ref,
		className: classNames(selectionClasses.selection, selectionClasses.position, { [selectionClasses.focused]: isFocusVisible && focusRingClasses }, className),
		style,
		"aria-hidden": "true"
	}, props));
});
const ScrollShadow = forwardRef((_ref5, ref) => {
	let { visible, location, className } = _ref5, props = _objectWithoutProperties(_ref5, _excluded5);
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({
		ref,
		className: classNames(shadowClasses.shadow, {
			[shadowClasses.isVisible]: visible,
			[shadowClasses.locationStart]: location === "start",
			[shadowClasses.locationEnd]: location === "end"
		}, className)
	}, props));
});
const ChevronButton = forwardRef((_ref6, ref) => {
	let { direction, disabled = false, onClick, className, "aria-label": ariaLabel } = _ref6, props = _objectWithoutProperties(_ref6, _excluded6);
	const defaultAriaLabel = direction === "prev" ? "prev" : "next";
	return /* @__PURE__ */ React.createElement("button", _objectSpread2({
		ref,
		className: classNames(chevronButtonClasses.button, {
			[chevronButtonClasses.disabled]: disabled,
			[chevronButtonClasses.prev]: direction === "prev",
			[chevronButtonClasses.next]: direction === "next"
		}, className),
		disabled,
		onClick,
		"aria-label": ariaLabel || defaultAriaLabel,
		type: "button"
	}, props), direction === "prev" ? /* @__PURE__ */ React.createElement(ChevronLeft, null) : /* @__PURE__ */ React.createElement(ChevronRight, null));
});
const ChevronControls = (_ref7) => {
	let { isPrevDisabled = false, isNextDisabled = false, onPrevClick, onNextClick, prevAction, nextAction, className } = _ref7, props = _objectWithoutProperties(_ref7, _excluded7);
	const handlePrevClick = onPrevClick || prevAction;
	const handleNextClick = onNextClick || nextAction;
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({ className: classNames(chevronClasses.controls, className) }, props), /* @__PURE__ */ React.createElement(ChevronButton, {
		direction: "prev",
		disabled: isPrevDisabled,
		onClick: handlePrevClick
	}), /* @__PURE__ */ React.createElement(ChevronButton, {
		direction: "next",
		disabled: isNextDisabled,
		onClick: handleNextClick
	}));
};
const TabsStructure = (_ref8) => {
	let { selectedTab, onTabChange, autoScrollToSelected = true, children, className, railRef: externalRailRef } = _ref8, props = _objectWithoutProperties(_ref8, _excluded8);
	const tabElementsRef = useRef(/* @__PURE__ */ new Map());
	const internalRailRef = useRef(null);
	const railRef = externalRailRef || internalRailRef;
	if (!selectedTab && children) console.warn("TabsStructure: No selectedTab provided, consider setting a default value");
	const registerTab = 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 = useCallback((tabId) => {
		if (tabId) tabElementsRef.current.delete(tabId);
	}, []);
	const clearAllTabs = useCallback(() => {
		tabElementsRef.current.clear();
	}, []);
	useEffect(() => {
		return () => {
			if (typeof WeakRef === "undefined") clearAllTabs();
		};
	}, [clearAllTabs]);
	const getTabElement = 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 = 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) safeScrollTo(rail, {
			left: targetLeft,
			behavior: "smooth"
		});
	}, [getTabElement]);
	useEffect(() => {
		if (autoScrollToSelected && selectedTab) scrollToTab(selectedTab);
	}, [
		selectedTab,
		autoScrollToSelected,
		scrollToTab
	]);
	const contextValue = useMemo(() => ({
		selectedTab,
		onTabChange,
		registerTab,
		unregisterTab,
		railRef
	}), [
		selectedTab,
		onTabChange,
		registerTab,
		unregisterTab,
		railRef
	]);
	return /* @__PURE__ */ React.createElement(TabContext.Provider, { value: contextValue }, /* @__PURE__ */ React.createElement("div", _objectSpread2(_objectSpread2({}, props), {}, { className: classNames(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
export { TabsStructureWithSubComponents as default };
