import { t as __exportAll } from "./chunk-ClPoSABd.mjs";
import { t as validateIdentifier } from "./validate-CqRJb_xU.mjs";
import { t as BylineRepository } from "./byline-CL847F26.mjs";
import { n as getDb } from "./loader-fz8Q_3EO.mjs";
import { sql } from "kysely";

//#region src/bylines/index.ts
/**
* Runtime API for bylines
*
* Provides functions to query byline profiles and byline credits
* associated with content entries. Follows the same pattern as
* the taxonomies runtime API.
*/
var bylines_exports = /* @__PURE__ */ __exportAll({
	getByline: () => getByline,
	getBylineBySlug: () => getBylineBySlug,
	getBylinesForEntries: () => getBylinesForEntries
});
/**
* Get a byline by ID.
*
* @example
* ```ts
* import { getByline } from "emdash";
*
* const byline = await getByline("01HXYZ...");
* if (byline) {
*   console.log(byline.displayName);
* }
* ```
*/
async function getByline(id) {
	return new BylineRepository(await getDb()).findById(id);
}
/**
* Get a byline by slug.
*
* @example
* ```ts
* import { getBylineBySlug } from "emdash";
*
* const byline = await getBylineBySlug("jane-doe");
* if (byline) {
*   console.log(byline.displayName); // "Jane Doe"
* }
* ```
*/
async function getBylineBySlug(slug) {
	return new BylineRepository(await getDb()).findBySlug(slug);
}
/**
* Batch-fetch byline credits for multiple content entries in a single query.
*
* This is more efficient than calling getEntryBylines for each entry
* when you need bylines for a list of entries (e.g., a blog index page).
*
* @param collection - The collection slug (e.g., "posts")
* @param entryIds - Array of entry IDs
* @returns Map from entry ID to array of byline credits
*
* @example
* ```ts
* import { getBylinesForEntries, getEmDashCollection } from "emdash";
*
* const { entries } = await getEmDashCollection("posts");
* const ids = entries.map(e => e.data.id);
* const bylinesMap = await getBylinesForEntries("posts", ids);
*
* for (const entry of entries) {
*   const bylines = bylinesMap.get(entry.data.id) ?? [];
*   // render bylines
* }
* ```
*/
async function getBylinesForEntries(collection, entryIds) {
	validateIdentifier(collection, "collection");
	const result = /* @__PURE__ */ new Map();
	for (const id of entryIds) result.set(id, []);
	if (entryIds.length === 0) return result;
	const db = await getDb();
	const repo = new BylineRepository(db);
	const bylinesMap = await repo.getContentBylinesMany(collection, entryIds);
	const fallbackEntryIds = [];
	const needsFallback = /* @__PURE__ */ new Map();
	for (const id of entryIds) if (!bylinesMap.has(id)) fallbackEntryIds.push(id);
	if (fallbackEntryIds.length > 0) {
		const authorMap = await getAuthorIds(db, collection, fallbackEntryIds);
		for (const [entryId, authorId] of authorMap) needsFallback.set(entryId, authorId);
	}
	const uniqueAuthorIds = [...new Set(needsFallback.values())];
	const authorBylineMap = await repo.findByUserIds(uniqueAuthorIds);
	for (const id of entryIds) {
		const explicit = bylinesMap.get(id);
		if (explicit && explicit.length > 0) {
			result.set(id, explicit.map((c) => ({
				...c,
				source: "explicit"
			})));
			continue;
		}
		const authorId = needsFallback.get(id);
		if (authorId) {
			const fallback = authorBylineMap.get(authorId);
			if (fallback) {
				result.set(id, [{
					byline: fallback,
					sortOrder: 0,
					roleLabel: null,
					source: "inferred"
				}]);
				continue;
			}
		}
	}
	return result;
}
/**
* Batch-fetch author_ids for multiple content entries.
* Returns Map<entryId, authorId> (only entries with non-null author_id).
*/
async function getAuthorIds(db, collection, entryIds) {
	const tableName = `ec_${collection}`;
	validateIdentifier(tableName, "content table");
	const result = await sql`
		SELECT id, author_id FROM ${sql.ref(tableName)}
		WHERE id IN (${sql.join(entryIds.map((id) => sql`${id}`))})
	`.execute(db);
	const map = /* @__PURE__ */ new Map();
	for (const row of result.rows) if (row.author_id) map.set(row.id, row.author_id);
	return map;
}

//#endregion
export { getByline as n, getBylineBySlug as r, bylines_exports as t };
//# sourceMappingURL=bylines-C2a-2TGt.mjs.map