"use client";
import { useStudio } from "../../providers/StudioProvider.js";
import { cn } from "../../lib/utils.js";
import { c } from "react/compiler-runtime";
import { useEffect, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { ChevronDownIcon, ChevronUpIcon, ExternalLinkIcon, LinkIcon, icons } from "lucide-react";
var EMBED_RESIZE_MESSAGE_TYPE = "loopstack:embed:resize", PIPELINE_HREF_PATTERN = /^\/pipelines\/([a-zA-Z0-9_-]+)$/, link_default = (m) => {
	let h = c(53), { className: g, href: _, label: v, caption: y, icon: b, type: x, embed: S, defaultExpanded: C, iconClassName: w } = m, { router: T } = useStudio(), [E, D] = useState(C ?? !1), [O, k] = useState(0), A = b && icons[b] ? icons[b] : LinkIcon, j;
	h[0] !== _ || h[1] !== v ? (j = v || (() => {
		if (!_) return "";
		try {
			return new URL(_).hostname.replace("www.", "");
		} catch {
			return _;
		}
	})(), h[0] = _, h[1] = v, h[2] = j) : j = h[2];
	let M = j, N = x === "success", P, F;
	h[3] !== S || h[4] !== _ || h[5] !== T ? (P = _?.match(PIPELINE_HREF_PATTERN)?.[1] ?? null, F = S === !0 && P != null ? T.getEmbedPipeline(P) : null, h[3] = S, h[4] = _, h[5] = T, h[6] = P, h[7] = F) : (P = h[6], F = h[7]);
	let I = F, L, R;
	h[8] !== E || h[9] !== P ? (L = () => {
		if (!P || !E) return;
		let t = (t) => {
			if (t.origin !== window.location.origin) return;
			let f = t.data;
			if (f?.type !== EMBED_RESIZE_MESSAGE_TYPE || f?.pipelineId !== P) return;
			let p = f?.height;
			typeof p == "number" && p > 0 && k(p);
		};
		return window.addEventListener("message", t), () => window.removeEventListener("message", t);
	}, R = [P, E], h[8] = E, h[9] = P, h[10] = L, h[11] = R) : (L = h[10], R = h[11]), useEffect(L, R);
	let z;
	h[12] === g ? z = h[13] : (z = cn("not-prose flex w-full flex-col rounded-md border bg-background", g), h[12] = g, h[13] = z);
	let B = N ? "bg-green-50 text-green-600 border-green-200" : "text-muted-foreground bg-muted/50", V;
	h[14] === B ? V = h[15] : (V = cn("flex size-8 shrink-0 items-center justify-center rounded-md border", B), h[14] = B, h[15] = V);
	let H;
	h[16] === w ? H = h[17] : (H = cn("size-4", w), h[16] = w, h[17] = H);
	let U;
	h[18] !== A || h[19] !== H ? (U = /* @__PURE__ */ jsx(A, { className: H }), h[18] = A, h[19] = H, h[20] = U) : U = h[20];
	let W;
	h[21] !== V || h[22] !== U ? (W = /* @__PURE__ */ jsx("div", {
		className: V,
		children: U
	}), h[21] = V, h[22] = U, h[23] = W) : W = h[23];
	let G;
	h[24] === M ? G = h[25] : (G = /* @__PURE__ */ jsx("span", {
		className: "truncate text-sm font-medium",
		children: M
	}), h[24] = M, h[25] = G);
	let K;
	h[26] !== y || h[27] !== _ ? (K = y ? /* @__PURE__ */ jsx("span", {
		className: "text-muted-foreground truncate text-xs",
		children: y
	}) : _ && /* @__PURE__ */ jsx("a", {
		href: _,
		target: "_blank",
		rel: "noopener noreferrer",
		className: "text-muted-foreground truncate text-xs hover:underline",
		children: _
	}), h[26] = y, h[27] = _, h[28] = K) : K = h[28];
	let q;
	h[29] !== G || h[30] !== K ? (q = /* @__PURE__ */ jsxs("div", {
		className: "flex min-w-0 flex-1 flex-col",
		children: [G, K]
	}), h[29] = G, h[30] = K, h[31] = q) : q = h[31];
	let J;
	h[32] !== I || h[33] !== E ? (J = I && /* @__PURE__ */ jsx("button", {
		type: "button",
		onClick: () => D(_temp),
		className: "text-muted-foreground hover:text-foreground flex size-7 items-center justify-center rounded-md transition-colors hover:bg-muted/50",
		children: jsx(E ? ChevronUpIcon : ChevronDownIcon, { className: "size-4" })
	}), h[32] = I, h[33] = E, h[34] = J) : J = h[34];
	let Y;
	h[35] === _ ? Y = h[36] : (Y = _ && /* @__PURE__ */ jsx("a", {
		href: _,
		target: "_blank",
		rel: "noopener noreferrer",
		className: "text-muted-foreground hover:text-foreground flex size-7 items-center justify-center rounded-md transition-colors hover:bg-muted/50",
		children: /* @__PURE__ */ jsx(ExternalLinkIcon, { className: "size-4" })
	}), h[35] = _, h[36] = Y);
	let X;
	h[37] !== J || h[38] !== Y ? (X = /* @__PURE__ */ jsxs("div", {
		className: "flex shrink-0 items-center gap-1",
		children: [J, Y]
	}), h[37] = J, h[38] = Y, h[39] = X) : X = h[39];
	let Z;
	h[40] !== W || h[41] !== q || h[42] !== X ? (Z = /* @__PURE__ */ jsxs("div", {
		className: "flex w-full items-center gap-3 p-3",
		children: [
			W,
			q,
			X
		]
	}), h[40] = W, h[41] = q, h[42] = X, h[43] = Z) : Z = h[43];
	let Q;
	h[44] !== M || h[45] !== I || h[46] !== E || h[47] !== O ? (Q = E && I && /* @__PURE__ */ jsx("div", {
		className: "border-t",
		children: /* @__PURE__ */ jsx("iframe", {
			src: I,
			className: "w-full overflow-hidden border-0",
			style: { height: `${O}px` },
			scrolling: "no",
			title: M
		})
	}), h[44] = M, h[45] = I, h[46] = E, h[47] = O, h[48] = Q) : Q = h[48];
	let $;
	return h[49] !== Z || h[50] !== Q || h[51] !== z ? ($ = /* @__PURE__ */ jsxs("div", {
		className: z,
		children: [Z, Q]
	}), h[49] = Z, h[50] = Q, h[51] = z, h[52] = $) : $ = h[52], $;
};
function _temp(t) {
	return !t;
}
export { link_default as default };
