import { a as hashSource, c as SourceUnavailableError, d as isNoAdaptersConfiguredError, f as isSourceUnavailableError, i as translateEntity, l as UnknownLocaleError, n as createTranslationWorker, o as I18nTranslatorError, p as isUnknownLocaleError, r as idempotencyKey, s as NoAdaptersConfiguredError, t as TRANSLATION_SWEEP_JOB, u as isI18nTranslatorError } from "./worker-BIsZ1056.js";
import { and, eq, inArray } from "drizzle-orm";
import { translationValue } from "@platform-modules/i18n-content";
//#region src/rollup.ts
function computeStatus(requestedFields, rows) {
	if (requestedFields.length === 0) return "complete";
	if (rows.length === 0) return "missing";
	const fieldCounts = /* @__PURE__ */ new Map();
	for (const row of rows) fieldCounts.set(row.fieldKey, (fieldCounts.get(row.fieldKey) ?? 0) + 1);
	for (const [fieldKey, count] of fieldCounts) if (count > 1) console.warn(`[i18n-translator] duplicate translation rows for fieldKey '${fieldKey}'; only one will be used`);
	const rowByField = new Map(rows.map((row) => [row.fieldKey, row]));
	for (const fieldKey of requestedFields) {
		const row = rowByField.get(fieldKey);
		if (row && (row.status === "FAILED" || row.value === "")) return "failed";
	}
	for (const fieldKey of requestedFields) if (!rowByField.has(fieldKey)) return "partial";
	for (const fieldKey of requestedFields) if (rowByField.get(fieldKey).status === "STALE") return "stale";
	const KNOWN_STATUSES = /* @__PURE__ */ new Set([
		"OK",
		"STALE",
		"FAILED"
	]);
	for (const fieldKey of requestedFields) {
		const row = rowByField.get(fieldKey);
		if (!KNOWN_STATUSES.has(row.status)) {
			console.warn(`[i18n-translator] unknown translation status '${row.status}' for fieldKey '${fieldKey}'; treating as failed`);
			return "failed";
		}
		if (row.status !== "OK" || row.value === "") return "failed";
	}
	return "complete";
}
/**
* Returns the aggregate translation status for a single entity.
*
* @remarks
* Ownership scoping is the caller's responsibility — the entityId must already
* be authorised for the calling tenant/user. No tenantId filter is applied here
* because the translationValue schema does not carry a tenant column; entity-level
* access control is enforced upstream by the caller.
*/
async function getEntityTranslationStatus(q, args) {
	if (args.fields.length === 0) return "complete";
	const rows = await q.select({
		fieldKey: translationValue.fieldKey,
		value: translationValue.value,
		status: translationValue.status
	}).from(translationValue).where(and(eq(translationValue.entityType, args.entityType), eq(translationValue.entityId, args.entityId), eq(translationValue.locale, args.locale), inArray(translationValue.fieldKey, [...args.fields])));
	return computeStatus(args.fields, rows);
}
/**
* Returns the aggregate translation status for a batch of entities.
*
* @remarks
* Ownership scoping is the caller's responsibility — all entityIds must already
* be authorised for the calling tenant/user. No tenantId filter is applied here
* because the translationValue schema does not carry a tenant column; entity-level
* access control is enforced upstream by the caller.
*/
async function getEntityTranslationStatusFor(q, args) {
	if (args.fields.length === 0) {
		const result = /* @__PURE__ */ new Map();
		for (const entityId of args.entityIds) result.set(entityId, "complete");
		return result;
	}
	if (args.entityIds.length === 0) return /* @__PURE__ */ new Map();
	const rows = await q.select({
		entityId: translationValue.entityId,
		fieldKey: translationValue.fieldKey,
		value: translationValue.value,
		status: translationValue.status
	}).from(translationValue).where(and(eq(translationValue.entityType, args.entityType), inArray(translationValue.entityId, [...args.entityIds]), eq(translationValue.locale, args.locale), inArray(translationValue.fieldKey, [...args.fields])));
	const rowsByEntity = /* @__PURE__ */ new Map();
	for (const row of rows) {
		const bucket = rowsByEntity.get(row.entityId) ?? [];
		bucket.push({
			fieldKey: row.fieldKey,
			value: row.value,
			status: row.status
		});
		rowsByEntity.set(row.entityId, bucket);
	}
	const result = /* @__PURE__ */ new Map();
	for (const entityId of args.entityIds) result.set(entityId, computeStatus(args.fields, rowsByEntity.get(entityId) ?? []));
	return result;
}
//#endregion
export { I18nTranslatorError, NoAdaptersConfiguredError, SourceUnavailableError, TRANSLATION_SWEEP_JOB, UnknownLocaleError, createTranslationWorker, getEntityTranslationStatus, getEntityTranslationStatusFor, hashSource, idempotencyKey, isI18nTranslatorError, isNoAdaptersConfiguredError, isSourceUnavailableError, isUnknownLocaleError, translateEntity };
