//#region src/channels/account-snapshot-fields.ts
function isRecord(value) {
	return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeOptionalString(value) {
	return typeof value === "string" && value.trim() ? value.trim() : void 0;
}
function normalizeOptionalStringLike(value) {
	return typeof value === "string" || typeof value === "number" ? String(value).trim() || void 0 : void 0;
}
function readBoolean(record, key) {
	return typeof record[key] === "boolean" ? record[key] : void 0;
}
function readNumber(record, key) {
	const value = record[key];
	return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function readNullableNumber(record, key) {
	if (record[key] === null) return null;
	return readNumber(record, key);
}
function readStringArray(record, key) {
	const value = record[key];
	if (!Array.isArray(value)) return;
	const normalized = value.map((entry) => typeof entry === "string" || typeof entry === "number" ? String(entry) : "").map((entry) => entry.trim()).filter(Boolean);
	return normalized.length > 0 ? normalized : void 0;
}
function stripUrlUserInfo(value) {
	try {
		const url = new URL(value);
		url.username = "";
		url.password = "";
		return url.toString();
	} catch {
		return value;
	}
}
function redactWebhookUrl(value) {
	try {
		const url = new URL(value);
		url.username = "";
		url.password = "";
		url.search = "";
		url.hash = "";
		return `${url.origin}${url.pathname && url.pathname !== "/" ? "/..." : ""}`;
	} catch {
		return;
	}
}
function projectSafeChannelAccountSnapshotFields(account) {
	const record = isRecord(account) ? account : null;
	if (!record) return {};
	const name = normalizeOptionalString(record.name);
	const mode = normalizeOptionalString(record.mode);
	const dmPolicy = normalizeOptionalString(record.dmPolicy);
	const audienceType = normalizeOptionalString(record.audienceType);
	const audience = normalizeOptionalStringLike(record.audience) ?? normalizeOptionalStringLike(record.defaultTo);
	const webhookPath = normalizeOptionalString(record.webhookPath);
	const webhookUrl = normalizeOptionalString(record.webhookUrl);
	const safeWebhookUrl = webhookUrl ? redactWebhookUrl(webhookUrl) : void 0;
	const tokenSource = normalizeOptionalString(record.tokenSource);
	const botTokenSource = normalizeOptionalString(record.botTokenSource);
	const appTokenSource = normalizeOptionalString(record.appTokenSource);
	const credentialSource = normalizeOptionalString(record.credentialSource);
	const secretSource = normalizeOptionalString(record.secretSource);
	const baseUrl = normalizeOptionalString(record.baseUrl);
	const cliPath = normalizeOptionalString(record.cliPath);
	const dbPath = normalizeOptionalString(record.dbPath);
	return {
		...name ? { name } : {},
		...readBoolean(record, "enabled") !== void 0 ? { enabled: readBoolean(record, "enabled") } : {},
		...readBoolean(record, "configured") !== void 0 ? { configured: readBoolean(record, "configured") } : {},
		...readBoolean(record, "linked") !== void 0 ? { linked: readBoolean(record, "linked") } : {},
		...readBoolean(record, "running") !== void 0 ? { running: readBoolean(record, "running") } : {},
		...readBoolean(record, "connected") !== void 0 ? { connected: readBoolean(record, "connected") } : {},
		...readNumber(record, "reconnectAttempts") !== void 0 ? { reconnectAttempts: readNumber(record, "reconnectAttempts") } : {},
		...readNullableNumber(record, "lastConnectedAt") !== void 0 ? { lastConnectedAt: readNullableNumber(record, "lastConnectedAt") } : {},
		...readNullableNumber(record, "lastMessageAt") !== void 0 ? { lastMessageAt: readNullableNumber(record, "lastMessageAt") } : {},
		...readNullableNumber(record, "lastEventAt") !== void 0 ? { lastEventAt: readNullableNumber(record, "lastEventAt") } : {},
		...readNullableNumber(record, "lastInboundAt") !== void 0 ? { lastInboundAt: readNullableNumber(record, "lastInboundAt") } : {},
		...readNullableNumber(record, "lastOutboundAt") !== void 0 ? { lastOutboundAt: readNullableNumber(record, "lastOutboundAt") } : {},
		...normalizeOptionalString(record.lastError) ? { lastError: normalizeOptionalString(record.lastError) } : {},
		...readNullableNumber(record, "lastStartAt") !== void 0 ? { lastStartAt: readNullableNumber(record, "lastStartAt") } : {},
		...readNullableNumber(record, "lastStopAt") !== void 0 ? { lastStopAt: readNullableNumber(record, "lastStopAt") } : {},
		...mode ? { mode } : {},
		...dmPolicy ? { dmPolicy } : {},
		...audienceType ? { audienceType } : {},
		...audience ? { audience } : {},
		...webhookPath ? { webhookPath } : {},
		...safeWebhookUrl ? { webhookUrl: safeWebhookUrl } : {},
		...readStringArray(record, "allowFrom") ? { allowFrom: readStringArray(record, "allowFrom") } : {},
		...tokenSource ? { tokenSource } : {},
		...botTokenSource ? { botTokenSource } : {},
		...appTokenSource ? { appTokenSource } : {},
		...credentialSource ? { credentialSource } : {},
		...secretSource ? { secretSource } : {},
		...baseUrl ? { baseUrl: stripUrlUserInfo(baseUrl) } : {},
		...readBoolean(record, "allowUnmentionedGroups") !== void 0 ? { allowUnmentionedGroups: readBoolean(record, "allowUnmentionedGroups") } : {},
		...cliPath ? { cliPath } : {},
		...dbPath ? { dbPath } : {},
		...readNumber(record, "port") !== void 0 ? { port: readNumber(record, "port") } : {}
	};
}

//#endregion
export { projectSafeChannelAccountSnapshotFields as t };