import { reactive as e, ref as t, watch as n } from "vue";
import { useTimeoutPoll as r, whenever as i } from "@vueuse/core";
import { getRawPlatformaInstance as a } from "@platforma-sdk/model";
var o = "[==PROGRESS==]";
function s(e) {
	return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function c(c) {
	let l = t(), u = e({ errorCount: 0 });
	async function d() {
		let e = l.value;
		if (e === void 0) return;
		let t = c.mockPlatforma ?? a();
		if (!t) {
			console.warn("Platforma API is not available");
			return;
		}
		for (;;) {
			let n = await t.logDriver.readText(e.logHandle, 100, e.lastOffset);
			if (e.error = void 0, u.errorCount = 0, n.shouldUpdateHandle || e.logHandle !== l.value?.logHandle) return;
			let r = c.progressPrefix ?? o, i = new TextDecoder().decode(n.data).replace(RegExp(`${s(r)}`, "g"), "");
			if (e = Object.assign(e, {
				lines: e.lines + i,
				lastOffset: n.newOffset,
				finished: !n.live
			}), n.newOffset >= n.size) break;
		}
	}
	let f = r(() => d().catch((e) => {
		l.value && (u.errorCount++, u.errorCount > (c.maxRetries ?? 3) ? l.value.error = e : console.warn("skip error:", e, "retry..."));
	}), 1500, {
		immediate: !1,
		immediateCallback: !0
	});
	return i(() => l?.value?.finished, () => f.pause()), n(() => c.logHandle, (e) => {
		e === void 0 ? (l.value = void 0, f.pause()) : e !== l.value?.logHandle && (l.value = {
			logHandle: e,
			lastOffset: 0,
			lines: "",
			finished: !1,
			error: void 0
		}, u.errorCount = 0, f.resume());
	}, { immediate: !0 }), l;
}
export { c as useLogHandle };

//# sourceMappingURL=useLogHandle.js.map