"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSubPath = exports.getDeepestPaths = exports.getContentAsCodePathFromLtreePath = exports.getLtreePathFromContentAsCodePath = exports.getLtreePathFromSlug = exports.generateSlug = void 0;
const sanitizeSlug = (slug, trimHyphens = true) => {
    let sanitizedSlug = slug
        .toLowerCase()
        // Then replace remaining non-alphanumeric characters with hyphens
        .replace(/[^a-z0-9]+/g, '-');
    if (trimHyphens) {
        // Trim leading and trailing hyphens
        sanitizedSlug = sanitizedSlug.replace(/^-+|-+$/g, '');
    }
    return sanitizedSlug;
};
const generateSlug = (name) => {
    const sanitizedSlug = sanitizeSlug(name);
    if (sanitizedSlug.length === 0) {
        // Return a random 5 character string
        // Base-36 uses all digits (0-9) and all letters (a-z)
        return Math.random().toString(36).substring(2, 7);
    }
    return sanitizedSlug;
};
exports.generateSlug = generateSlug;
const getLtreePathFromSlug = (slug) => {
    let path = slug;
    // This is for backwards compatibility with the old slug format that contained hierarchy
    if (path.includes('/')) {
        path = path.split('/').join('___');
    }
    return path.replace(/-/g, '_');
};
exports.getLtreePathFromSlug = getLtreePathFromSlug;
const getLtreePathFromContentAsCodePath = (path) => path.replace(/-/g, '_').replace(/\//g, '.');
exports.getLtreePathFromContentAsCodePath = getLtreePathFromContentAsCodePath;
const getContentAsCodePathFromLtreePath = (path) => path.replace(/_/g, '-').replace(/\./g, '/');
exports.getContentAsCodePathFromLtreePath = getContentAsCodePathFromLtreePath;
const getDeepestPaths = (paths) => {
    const uniquePaths = Array.from(new Set(paths));
    const result = [];
    for (const path of uniquePaths) {
        const pathSegments = path.split('.');
        const isPrefix = uniquePaths.some((otherPath) => {
            if (path === otherPath)
                return false;
            const otherSegments = otherPath.split('.');
            if (pathSegments.length < otherSegments.length) {
                const potentialPrefix = otherSegments
                    .slice(0, pathSegments.length)
                    .join('.');
                return potentialPrefix === path;
            }
            return false;
        });
        if (!isPrefix) {
            result.push(path);
        }
    }
    return result;
};
exports.getDeepestPaths = getDeepestPaths;
const isSubPath = (path, subPath) => subPath.startsWith(path) && subPath[path.length] === '.';
exports.isSubPath = isSubPath;
//# sourceMappingURL=slugs.js.map