import { n as getI18nConfig } from "../../../../../../config-CVssduLe.mjs";
import "../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../options-DTTML-Tx.mjs";
import { a as unwrapResult, n as apiSuccess, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { r as parseOptionalBody, t as isParseError } from "../../../../../../parse-GmjecGct.mjs";
import { on as contentPreviewUrlBody } from "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import "../../../../../../api/schemas/index.mjs";
import "../../../../../../tokens-BGT0TUUo.mjs";
import { i as getPreviewUrl } from "../../../../../../preview-SSXNVPAC.mjs";
import { i as resolveSecretsCached } from "../../../../../../secrets-kp760LlD.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";

//#region src/astro/routes/api/content/[collection]/[id]/preview-url.ts
const prerender = false;
const DURATION_PATTERN = /^(\d+)([smhdw])$/;
const POST = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "content:read_drafts");
	if (denied) return denied;
	const collection = params.collection;
	const id = params.id;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const { previewSecret } = await resolveSecretsCached(emdash.db);
	let entryLocale = null;
	if (emdash?.handleContentGet) {
		const result = await emdash.handleContentGet(collection, id);
		if (!result.success) return unwrapResult(result);
		entryLocale = result.data?.item?.locale ?? null;
	}
	const body = await parseOptionalBody(request, contentPreviewUrlBody, {});
	if (isParseError(body)) return body;
	const expiresIn = body.expiresIn || "1h";
	const defaultPathPattern = import.meta.env.EMDASH_PREVIEW_PATH_PATTERN || "/{collection}/{id}";
	const pathPattern = body.pathPattern || defaultPathPattern;
	const i18n = getI18nConfig();
	let localeSegment = "";
	if (entryLocale && i18n) localeSegment = entryLocale === i18n.defaultLocale && !i18n.prefixDefaultLocale ? "" : entryLocale;
	else if (entryLocale) localeSegment = entryLocale;
	const expiresInSeconds = typeof expiresIn === "number" ? expiresIn : parseExpiresIn(expiresIn);
	const expiresAt = Math.floor(Date.now() / 1e3) + expiresInSeconds;
	try {
		return apiSuccess({
			url: await getPreviewUrl({
				collection,
				id,
				secret: previewSecret,
				expiresIn,
				pathPattern,
				locale: localeSegment
			}),
			expiresAt
		});
	} catch (error) {
		return handleError(error, "Failed to generate preview URL", "TOKEN_ERROR");
	}
};
/**
* Parse duration string to seconds
*/
function parseExpiresIn(duration) {
	const match = duration.match(DURATION_PATTERN);
	if (!match) return 3600;
	const value = parseInt(match[1], 10);
	switch (match[2]) {
		case "s": return value;
		case "m": return value * 60;
		case "h": return value * 60 * 60;
		case "d": return value * 60 * 60 * 24;
		case "w": return value * 60 * 60 * 24 * 7;
		default: return 3600;
	}
}

//#endregion
export { POST, prerender };
//# sourceMappingURL=preview-url.mjs.map