import { useFileContent, useFileTree } from "../../../hooks/useFiles.js";
import { c } from "react/compiler-runtime";
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
import { jsx } from "react/jsx-runtime";
function extractErrorMessage(e) {
	if (!e) return null;
	if ("isAxiosError" in e && e.isAxiosError) {
		let t = e.response?.data;
		if (typeof t == "object" && t && "message" in t) return Error(String(t.message));
		if (typeof t == "string") return Error(t);
	}
	return e;
}
var CodeExplorerContext = createContext(null);
function mapDtoToNode(e) {
	return {
		id: e.id,
		name: e.name,
		path: e.path,
		type: e.type,
		children: e.children?.map(mapDtoToNode)
	};
}
function CodeExplorerProvider(r) {
	let i = c(49), { children: a, pipelineId: o, initialSelectedPath: s, fileExplorerEnabled: l } = r, u = l === void 0 ? !1 : l, d;
	i[0] === Symbol.for("react.memo_cache_sentinel") ? (d = [], i[0] = d) : d = i[0];
	let [f, p] = useState(d), [m, h] = useState(null), [g, _] = useState(""), v;
	i[1] === Symbol.for("react.memo_cache_sentinel") ? (v = /* @__PURE__ */ new Set(), i[1] = v) : v = i[1];
	let [y, b] = useState(v), x = useFileTree(o, u), S = useFileContent(o, m?.path, u), C;
	bb0: {
		if (!x.data) {
			let e;
			i[2] === Symbol.for("react.memo_cache_sentinel") ? (e = [], i[2] = e) : e = i[2], C = e;
			break bb0;
		}
		let e;
		i[3] === x.data ? e = i[4] : (e = x.data.map(mapDtoToNode), i[3] = x.data, i[4] = e), C = e;
	}
	let w = C, T = S.data?.content ?? null, E = S.data?.workflowConfig ?? null, D = x.isLoading && !x.data, O = S.isLoading && m !== null && !S.data, k;
	i[5] !== S.error || i[6] !== x.error ? (k = extractErrorMessage(x.error) || extractErrorMessage(S.error) || null, i[5] = S.error, i[6] = x.error, i[7] = k) : k = i[7];
	let A = k, j, M;
	i[8] !== w || i[9] !== s || i[10] !== f.length || i[11] !== m ? (j = () => {
		if (s && w.length > 0 && !m && f.length === 0) {
			let e = (t, n) => {
				for (let r of t) {
					if (r.path === n) return r;
					if (r.children) {
						let t = e(r.children, n);
						if (t) return t;
					}
				}
				return null;
			}, t = e(w, s);
			t && t.type === "file" && (p([t]), h(t));
		}
	}, M = [
		s,
		w,
		m,
		f.length
	], i[8] = w, i[9] = s, i[10] = f.length, i[11] = m, i[12] = j, i[13] = M) : (j = i[12], M = i[13]), useEffect(j, M);
	let N;
	i[14] === Symbol.for("react.memo_cache_sentinel") ? (N = (e) => {
		e.type === "file" && p((t) => {
			if (t.findIndex((t) => t.path === e.path) >= 0) return h(e), t;
			{
				let n = [...t, e];
				return h(e), n;
			}
		});
	}, i[14] = N) : N = i[14];
	let P = N, F;
	i[15] === m ? F = i[16] : (F = () => {
		m && p((e) => {
			let t = e.filter((e) => e.path !== m.path);
			if (t.length > 0) {
				let n = e.findIndex((e) => e.path === m.path);
				h(t[n > 0 ? n - 1 : 0]);
			} else h(null);
			return t;
		});
	}, i[15] = m, i[16] = F);
	let I = F, L;
	i[17] === m?.path ? L = i[18] : (L = (e) => {
		p((t) => {
			let n = t.filter((t) => t.path !== e.path);
			if (m?.path === e.path) if (n.length > 0) {
				let r = t.findIndex((t) => t.path === e.path);
				h(n[r > 0 ? r - 1 : 0]);
			} else h(null);
			return n;
		});
	}, i[17] = m?.path, i[18] = L);
	let R = L, z;
	i[19] === Symbol.for("react.memo_cache_sentinel") ? (z = () => {
		p([]), h(null);
	}, i[19] = z) : z = i[19];
	let B = z, V;
	i[20] === Symbol.for("react.memo_cache_sentinel") ? (V = (e) => {
		p([e]), h(e);
	}, i[20] = V) : V = i[20];
	let H = V, U;
	i[21] === m ? U = i[22] : (U = (e) => {
		p((t) => {
			let n = t.findIndex((t) => t.path === e.path);
			if (n <= 0) return t;
			let r = t.slice(n);
			return m && t.findIndex((e) => e.path === m.path) < n && h(e), r;
		});
	}, i[21] = m, i[22] = U);
	let W = U, G;
	i[23] === m ? G = i[24] : (G = (e) => {
		p((t) => {
			let n = t.findIndex((t) => t.path === e.path);
			if (n < 0 || n >= t.length - 1) return t;
			let r = t.slice(0, n + 1);
			return m && t.findIndex((e) => e.path === m.path) > n && h(e), r;
		});
	}, i[23] = m, i[24] = G);
	let K = G, q;
	i[25] !== S || i[26] !== x || i[27] !== m ? (q = async () => {
		await x.refetch(), m && await S.refetch();
	}, i[25] = S, i[26] = x, i[27] = m, i[28] = q) : q = i[28];
	let J = q, Y;
	i[29] === Symbol.for("react.memo_cache_sentinel") ? (Y = (e) => {
		b((t) => {
			let n = new Set(t);
			return n.has(e) ? n.delete(e) : n.add(e), n;
		});
	}, i[29] = Y) : Y = i[29];
	let X = Y, Z;
	i[30] !== I || i[31] !== R || i[32] !== W || i[33] !== K || i[34] !== A || i[35] !== y || i[36] !== T || i[37] !== w || i[38] !== O || i[39] !== D || i[40] !== f || i[41] !== J || i[42] !== g || i[43] !== m || i[44] !== E ? (Z = {
		fileTree: w,
		openFiles: f,
		selectedFile: m,
		fileContent: T,
		workflowConfig: E,
		isTreeLoading: D,
		isContentLoading: O,
		error: A,
		searchQuery: g,
		setSearchQuery: _,
		selectFile: P,
		clearSelection: I,
		closeFile: R,
		closeAll: B,
		closeOthers: H,
		closeToLeft: W,
		closeToRight: K,
		refresh: J,
		expandedFolders: y,
		toggleFolder: X
	}, i[30] = I, i[31] = R, i[32] = W, i[33] = K, i[34] = A, i[35] = y, i[36] = T, i[37] = w, i[38] = O, i[39] = D, i[40] = f, i[41] = J, i[42] = g, i[43] = m, i[44] = E, i[45] = Z) : Z = i[45];
	let Q = Z, $;
	return i[46] !== a || i[47] !== Q ? ($ = /* @__PURE__ */ jsx(CodeExplorerContext.Provider, {
		value: Q,
		children: a
	}), i[46] = a, i[47] = Q, i[48] = $) : $ = i[48], $;
}
function useCodeExplorerContext() {
	let e = useContext(CodeExplorerContext);
	if (!e) throw Error("useCodeExplorerContext must be used within CodeExplorerProvider");
	return e;
}
export { CodeExplorerProvider, useCodeExplorerContext };
