"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SLUG_REJECTION_CODES = void 0;
exports.normalizeCanonicalSlug = normalizeCanonicalSlug;
exports.isCanonicalSlug = isCanonicalSlug;
exports.SLUG_REJECTION_CODES = [
    "empty-input",
    "whitespace-only",
    "path-separator",
    "underscore",
    "forbidden-character",
    "uppercase-only-correction",
    "empty-segment",
    "non-canonical-value"
];
function normalizeCanonicalSlug(input) {
    if (input === "") {
        return rejectSlug(input, "empty-input", "slug", "Slugs must not be empty.");
    }
    const trimmed = input.trim();
    if (trimmed === "") {
        return rejectSlug(input, "whitespace-only", "slug", "Slugs must contain non-whitespace characters.");
    }
    if (/[\\/]/.test(trimmed)) {
        return rejectSlug(input, "path-separator", "slug", "Slugs must not contain path separators.", undefined, trimmed);
    }
    if (trimmed.includes("_")) {
        return rejectSlug(input, "underscore", "slug", "Slugs must not contain underscores.", "_");
    }
    const forbiddenCharacterMatch = trimmed.match(/[^A-Za-z0-9\s-]/u);
    if (forbiddenCharacterMatch !== null) {
        return rejectSlug(input, "forbidden-character", "slug", `Slugs must not contain ${JSON.stringify(forbiddenCharacterMatch[0])}.`, forbiddenCharacterMatch[0]);
    }
    const lowercased = trimmed.toLowerCase();
    const collapsed = lowercased.replace(/[\s-]+/g, "-");
    const separatorNormalized = lowercased !== collapsed;
    if (collapsed === "") {
        return rejectSlug(input, "empty-segment", "slug", "Slugs must contain at least one canonical segment after normalization.", undefined, collapsed);
    }
    if (collapsed.startsWith("-") || collapsed.endsWith("-")) {
        return rejectSlug(input, "empty-segment", "slug", "Slugs must not begin or end with separator runs.", undefined, collapsed);
    }
    if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(collapsed)) {
        return rejectSlug(input, "non-canonical-value", "slug", "Slugs must normalize to lowercase letters, digits, and single hyphens only.", undefined, collapsed);
    }
    if (/[A-Z]/.test(trimmed) && !separatorNormalized) {
        return rejectSlug(input, "uppercase-only-correction", "slug", "Slugs that would only change by lowercasing should be rejected instead of silently rewritten.", undefined, collapsed);
    }
    return {
        kind: "canonical-slug",
        originalInput: input,
        normalizedSlug: collapsed
    };
}
function isCanonicalSlug(value) {
    return /^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(value);
}
function rejectSlug(originalInput, code, field, message, offendingCharacter, normalizedCandidate = originalInput.trim()) {
    return {
        kind: "slug-rejection",
        originalInput,
        normalizedInput: normalizedCandidate,
        reason: {
            code,
            field,
            message,
            details: {
                offendingCharacter,
                normalizedCandidate
            }
        }
    };
}
