"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_primordials_promise = require('../primordials/promise.js');
let node_child_process = require("node:child_process");

//#region src/secrets/linux.ts
/**
* @file Linux Secret Service backend via `secret-tool`. `secret-tool` is the
*   user-facing CLI for libsecret, which talks to any running Secret Service
*   provider (gnome-keyring, kwallet5, KeePassXC's Secret Service integration,
*   etc.). Most desktop Linux installs have one; headless / containerized hosts
*   do not. Storage shape: each item has a `service=<svc> user=<account>`
*   attribute pair plus the password body. Reads + writes use those two
*   attributes as the lookup key. The label (`--label`) is what shows up in the
*   user's keyring UI; we set it to the same string passed in by the caller. No
*   backing-file fallback. If `secret-tool` isn't on PATH or no Secret Service
*   provider is running, reads return `undefined` and writes throw with an
*   actionable hint — callers fall back to env variables for that session.
*/
const SECRET_TOOL_BIN = "secret-tool";
async function deleteLinux(service, account) {
	return new require_primordials_promise.PromiseCtor((resolve) => {
		const child = (0, node_child_process.spawn)(SECRET_TOOL_BIN, [
			"clear",
			"service",
			service,
			"user",
			account
		], { stdio: "ignore" });
		child.on("error", () => resolve("absent"));
		child.on("close", (status) => resolve(status === 0 ? "removed" : "absent"));
	});
}
function deleteLinuxSync(service, account) {
	return (0, node_child_process.spawnSync)(SECRET_TOOL_BIN, [
		"clear",
		"service",
		service,
		"user",
		account
	], { stdio: "ignore" }).status === 0 ? "removed" : "absent";
}
function isLinuxBackendAvailable() {
	return (0, node_child_process.spawnSync)(SECRET_TOOL_BIN, ["--version"], { stdio: "ignore" }).status === 0;
}
async function readLinux(service, account) {
	return new require_primordials_promise.PromiseCtor((resolve) => {
		const child = (0, node_child_process.spawn)(SECRET_TOOL_BIN, [
			"lookup",
			"service",
			service,
			"user",
			account
		], { stdio: [
			"ignore",
			"pipe",
			"pipe"
		] });
		let stdout = "";
		child.stdout.setEncoding("utf8");
		child.stdout.on("data", (chunk) => {
			stdout += chunk;
		});
		child.on("error", () => resolve(void 0));
		child.on("close", (status) => {
			if (status !== 0) {
				resolve(void 0);
				return;
			}
			resolve(stdout.trim() || void 0);
		});
	});
}
function readLinuxSync(service, account) {
	const r = (0, node_child_process.spawnSync)(SECRET_TOOL_BIN, [
		"lookup",
		"service",
		service,
		"user",
		account
	], {
		encoding: "utf8",
		stdio: [
			"ignore",
			"pipe",
			"pipe"
		]
	});
	if (r.status !== 0) return;
	return r.stdout.trim() || void 0;
}
async function writeLinux(service, account, value, label) {
	return new require_primordials_promise.PromiseCtor((resolve, reject) => {
		const child = (0, node_child_process.spawn)(SECRET_TOOL_BIN, [
			"store",
			`--label=${label}`,
			"service",
			service,
			"user",
			account
		], { stdio: [
			"pipe",
			"pipe",
			"pipe"
		] });
		let stderr = "";
		child.stderr.setEncoding("utf8");
		child.stderr.on("data", (chunk) => {
			stderr += chunk;
		});
		child.on("error", (err) => reject(/* @__PURE__ */ new Error(`secret-tool store failed: ${err.message}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`)));
		child.on("close", (status) => {
			if (status === 0) {
				resolve();
				return;
			}
			reject(/* @__PURE__ */ new Error(`secret-tool store failed (status=${status}, user=${account}): ${stderr.trim()}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`));
		});
		child.stdin.end(value);
	});
}
function writeLinuxSync(service, account, value, label) {
	const r = (0, node_child_process.spawnSync)(SECRET_TOOL_BIN, [
		"store",
		`--label=${label}`,
		"service",
		service,
		"user",
		account
	], {
		encoding: "utf8",
		input: value,
		stdio: [
			"pipe",
			"pipe",
			"pipe"
		]
	});
	if (r.status !== 0) throw new require_primordials_error.ErrorCtor(`secret-tool store failed (status=${r.status}, user=${account}): ${r.stderr.trim()}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`);
}

//#endregion
exports.deleteLinux = deleteLinux;
exports.deleteLinuxSync = deleteLinuxSync;
exports.isLinuxBackendAvailable = isLinuxBackendAvailable;
exports.readLinux = readLinux;
exports.readLinuxSync = readLinuxSync;
exports.writeLinux = writeLinux;
exports.writeLinuxSync = writeLinuxSync;