import { n as BylineSchemaRegistry, r as mapBylineSchemaError, t as BylineSchemaError } from "./byline-registry-BOjqDOim.mjs";

//#region src/api/handlers/byline-fields.ts
/**
* Build a structured failure envelope from a `BylineSchemaError`.
* Centralised so every handler emits the same shape.
*/
function bylineSchemaErrorResult(error) {
	const mapped = mapBylineSchemaError(error);
	return {
		success: false,
		error: {
			code: mapped.code,
			message: mapped.message,
			details: mapped.details
		}
	};
}
/**
* Build a 500-class failure envelope. Logs the underlying error
* server-side; the message returned to the client is the static
* fallback to avoid leaking internals.
*/
function internalErrorResult(error, code, fallbackMessage) {
	console.error(`[${code}]`, error);
	return {
		success: false,
		error: {
			code,
			message: fallbackMessage
		}
	};
}
async function handleBylineFieldList(db) {
	try {
		return {
			success: true,
			data: { items: await new BylineSchemaRegistry(db).listFields() }
		};
	} catch (error) {
		return internalErrorResult(error, "SCHEMA_FIELD_LIST_ERROR", "Failed to list byline fields");
	}
}
async function handleBylineFieldCreate(db, input) {
	try {
		return {
			success: true,
			data: await new BylineSchemaRegistry(db).createField(input)
		};
	} catch (error) {
		if (error instanceof BylineSchemaError) return bylineSchemaErrorResult(error);
		return internalErrorResult(error, "SCHEMA_FIELD_CREATE_ERROR", "Failed to create byline field");
	}
}
async function handleBylineFieldGet(db, slug) {
	try {
		const field = await new BylineSchemaRegistry(db).getField(slug);
		if (!field) return {
			success: false,
			error: {
				code: "NOT_FOUND",
				message: "Byline field not found"
			}
		};
		return {
			success: true,
			data: field
		};
	} catch (error) {
		return internalErrorResult(error, "SCHEMA_FIELD_GET_ERROR", "Failed to get byline field");
	}
}
async function handleBylineFieldUpdate(db, slug, input) {
	try {
		return {
			success: true,
			data: await new BylineSchemaRegistry(db).updateField(slug, input)
		};
	} catch (error) {
		if (error instanceof BylineSchemaError) return bylineSchemaErrorResult(error);
		return internalErrorResult(error, "SCHEMA_FIELD_UPDATE_ERROR", "Failed to update byline field");
	}
}
async function handleBylineFieldDelete(db, slug) {
	try {
		await new BylineSchemaRegistry(db).deleteField(slug);
		return {
			success: true,
			data: { deleted: true }
		};
	} catch (error) {
		if (error instanceof BylineSchemaError) return bylineSchemaErrorResult(error);
		return internalErrorResult(error, "SCHEMA_FIELD_DELETE_ERROR", "Failed to delete byline field");
	}
}
async function handleBylineFieldUsage(db, slug) {
	try {
		return {
			success: true,
			data: await new BylineSchemaRegistry(db).getFieldUsage(slug)
		};
	} catch (error) {
		if (error instanceof BylineSchemaError) return bylineSchemaErrorResult(error);
		return internalErrorResult(error, "SCHEMA_FIELD_GET_ERROR", "Failed to read byline field usage");
	}
}
async function handleBylineFieldReorder(db, slugs) {
	try {
		const registry = new BylineSchemaRegistry(db);
		await registry.reorderFields(slugs);
		return {
			success: true,
			data: { items: await registry.listFields() }
		};
	} catch (error) {
		if (error instanceof BylineSchemaError) return bylineSchemaErrorResult(error);
		return internalErrorResult(error, "SCHEMA_FIELD_REORDER_ERROR", "Failed to reorder byline fields");
	}
}

//#endregion
export { handleBylineFieldReorder as a, handleBylineFieldList as i, handleBylineFieldDelete as n, handleBylineFieldUpdate as o, handleBylineFieldGet as r, handleBylineFieldUsage as s, handleBylineFieldCreate as t };
//# sourceMappingURL=byline-fields-CIx_GuN4.mjs.map