import { cn } from "../../../lib/utils.js";
import { Button } from "../../../components/ui/button.js";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "../../../components/ui/dropdown-menu.js";
import { ScrollArea, ScrollBar } from "../../../components/ui/scroll-area.js";
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger } from "../../../components/ui/context-menu.js";
import { useCodeExplorerContext } from "../providers/CodeExplorerProvider.js";
import { getFileIcon } from "../utils/fileIcons.js";
import { c } from "react/compiler-runtime";
import { useEffect, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { ClipboardCopy, MoreHorizontal, X, XCircle } from "lucide-react";
function FileTabsBar() {
	let g = c(50), { openFiles: _, selectedFile: v, selectFile: y, closeFile: b, closeAll: x, closeOthers: S, closeToLeft: C, closeToRight: w } = useCodeExplorerContext(), T = useRef(null), E = useRef(_.length), D, O;
	g[0] === _.length ? (D = g[1], O = g[2]) : (D = () => {
		if (_.length > E.current && T.current) {
			let e = T.current.closest("[data-slot=\"scroll-area-viewport\"]");
			e && requestAnimationFrame(() => {
				e.scrollTo({
					left: e.scrollWidth,
					behavior: "smooth"
				});
			});
		}
		E.current = _.length;
	}, O = [_.length], g[0] = _.length, g[1] = D, g[2] = O), useEffect(D, O);
	let k, A;
	if (g[3] === v ? (k = g[4], A = g[5]) : (k = () => {
		if (v && T.current) {
			let e = T.current.querySelector(`[data-tab-path="${v.path}"]`);
			e && requestAnimationFrame(() => {
				e.scrollIntoView({
					behavior: "smooth",
					block: "nearest",
					inline: "nearest"
				});
			});
		}
	}, A = [v], g[3] = v, g[4] = k, g[5] = A), useEffect(k, A), _.length === 0) return null;
	let j;
	g[6] === y ? j = g[7] : (j = (e) => {
		y(e);
	}, g[6] = y, g[7] = j);
	let M = j, N;
	g[8] === b ? N = g[9] : (N = (e, l) => {
		e.stopPropagation(), b(l);
	}, g[8] = b, g[9] = N);
	let P = N, F = _temp2, I;
	g[10] === Symbol.for("react.memo_cache_sentinel") ? (I = { width: "max-content" }, g[10] = I) : I = g[10];
	let L;
	g[11] !== x || g[12] !== b || g[13] !== S || g[14] !== C || g[15] !== w || g[16] !== P || g[17] !== M || g[18] !== _ || g[19] !== v?.path ? (L = _.map((u) => {
		let d = getFileIcon(u.name), f = v?.path === u.path, p = _.findIndex((e) => e.path === u.path), m = p > 0, h = p < _.length - 1;
		return /* @__PURE__ */ jsxs(ContextMenu, { children: [/* @__PURE__ */ jsx(ContextMenuTrigger, {
			asChild: !0,
			children: /* @__PURE__ */ jsxs("button", {
				type: "button",
				"data-tab-path": u.path,
				onClick: () => M(u),
				className: cn("group relative flex h-9 min-w-[140px] max-w-[280px] shrink-0 items-center gap-2 border-r border-border/50 px-3 text-sm transition-all", "hover:bg-muted/70", f ? "bg-background text-foreground border-b-2 border-b-primary shadow-sm" : "text-muted-foreground bg-muted/30"),
				"aria-selected": f,
				"aria-label": `Tab: ${u.name}`,
				children: [
					/* @__PURE__ */ jsx(d, {
						className: "h-3.5 w-3.5 shrink-0",
						"aria-hidden": !0
					}),
					/* @__PURE__ */ jsx("span", {
						className: "truncate flex-1 text-left font-medium",
						children: u.name
					}),
					/* @__PURE__ */ jsx(Button, {
						variant: "ghost",
						size: "icon",
						className: cn("h-5 w-5 shrink-0 rounded opacity-0 group-hover:opacity-100 transition-opacity", "hover:bg-destructive/10 hover:text-destructive", f && "opacity-70"),
						onClick: (e) => P(e, u),
						"aria-label": `Close ${u.name}`,
						children: /* @__PURE__ */ jsx(X, { className: "h-3.5 w-3.5" })
					})
				]
			})
		}), /* @__PURE__ */ jsxs(ContextMenuContent, {
			className: "min-w-[180px]",
			children: [
				/* @__PURE__ */ jsx(ContextMenuItem, {
					onClick: () => b(u),
					children: "Close"
				}),
				/* @__PURE__ */ jsx(ContextMenuItem, {
					onClick: () => S(u),
					disabled: _.length <= 1,
					children: "Close Others"
				}),
				/* @__PURE__ */ jsx(ContextMenuItem, {
					onClick: () => C(u),
					disabled: !m,
					children: "Close to the Left"
				}),
				/* @__PURE__ */ jsx(ContextMenuItem, {
					onClick: () => w(u),
					disabled: !h,
					children: "Close to the Right"
				}),
				/* @__PURE__ */ jsx(ContextMenuSeparator, {}),
				/* @__PURE__ */ jsx(ContextMenuItem, {
					onClick: x,
					children: "Close All"
				}),
				/* @__PURE__ */ jsx(ContextMenuSeparator, {}),
				/* @__PURE__ */ jsxs(ContextMenuItem, {
					onClick: () => F(u.path),
					children: [/* @__PURE__ */ jsx(ClipboardCopy, { className: "h-4 w-4" }), "Copy Path"]
				})
			]
		})] }, u.path);
	}), g[11] = x, g[12] = b, g[13] = S, g[14] = C, g[15] = w, g[16] = P, g[17] = M, g[18] = _, g[19] = v?.path, g[20] = L) : L = g[20];
	let R;
	g[21] === L ? R = g[22] : (R = /* @__PURE__ */ jsx("div", {
		ref: T,
		className: "flex h-9 items-center",
		style: I,
		children: L
	}), g[21] = L, g[22] = R);
	let z;
	g[23] === Symbol.for("react.memo_cache_sentinel") ? (z = /* @__PURE__ */ jsx(ScrollBar, { orientation: "horizontal" }), g[23] = z) : z = g[23];
	let B;
	g[24] === R ? B = g[25] : (B = /* @__PURE__ */ jsxs(ScrollArea, {
		className: "flex-1 min-w-0",
		children: [R, z]
	}), g[24] = R, g[25] = B);
	let V;
	g[26] === Symbol.for("react.memo_cache_sentinel") ? (V = /* @__PURE__ */ jsx(DropdownMenuTrigger, {
		asChild: !0,
		children: /* @__PURE__ */ jsx(Button, {
			variant: "ghost",
			size: "icon",
			className: "h-9 w-9 rounded-none text-muted-foreground hover:bg-muted/70 hover:text-foreground",
			"aria-label": "Tab actions",
			children: /* @__PURE__ */ jsx(MoreHorizontal, { className: "h-4 w-4" })
		})
	}), g[26] = V) : V = g[26];
	let H;
	g[27] !== b || g[28] !== v ? (H = () => v && b(v), g[27] = b, g[28] = v, g[29] = H) : H = g[29];
	let U = !v, W;
	g[30] !== H || g[31] !== U ? (W = /* @__PURE__ */ jsx(DropdownMenuItem, {
		onClick: H,
		disabled: U,
		children: "Close Active Tab"
	}), g[30] = H, g[31] = U, g[32] = W) : W = g[32];
	let G;
	g[33] !== S || g[34] !== v ? (G = () => v && S(v), g[33] = S, g[34] = v, g[35] = G) : G = g[35];
	let K = !v || _.length <= 1, q;
	g[36] !== G || g[37] !== K ? (q = /* @__PURE__ */ jsx(DropdownMenuItem, {
		onClick: G,
		disabled: K,
		children: "Close Other Tabs"
	}), g[36] = G, g[37] = K, g[38] = q) : q = g[38];
	let J;
	g[39] === Symbol.for("react.memo_cache_sentinel") ? (J = /* @__PURE__ */ jsx(DropdownMenuSeparator, {}), g[39] = J) : J = g[39];
	let Y;
	g[40] === Symbol.for("react.memo_cache_sentinel") ? (Y = /* @__PURE__ */ jsx(XCircle, { className: "h-4 w-4" }), g[40] = Y) : Y = g[40];
	let Z;
	g[41] === x ? Z = g[42] : (Z = /* @__PURE__ */ jsxs(DropdownMenuItem, {
		onClick: x,
		children: [Y, "Close All Tabs"]
	}), g[41] = x, g[42] = Z);
	let Q;
	g[43] !== W || g[44] !== q || g[45] !== Z ? (Q = /* @__PURE__ */ jsx("div", {
		className: "shrink-0 border-l border-border/50 bg-muted/30 h-9 flex items-center",
		children: /* @__PURE__ */ jsxs(DropdownMenu, { children: [V, /* @__PURE__ */ jsxs(DropdownMenuContent, {
			align: "end",
			className: "min-w-[180px]",
			children: [
				W,
				q,
				J,
				Z
			]
		})] })
	}), g[43] = W, g[44] = q, g[45] = Z, g[46] = Q) : Q = g[46];
	let $;
	return g[47] !== B || g[48] !== Q ? ($ = /* @__PURE__ */ jsxs("div", {
		className: "shrink-0 border-b bg-muted/50 flex items-center overflow-hidden",
		children: [B, Q]
	}), g[47] = B, g[48] = Q, g[49] = $) : $ = g[49], $;
}
function _temp2(e) {
	navigator.clipboard.writeText(e).catch(_temp);
}
function _temp(e) {
	console.error("Failed to copy path:", e);
}
export { FileTabsBar };
