import { addLeadingSlash, normalizeSlash, } from '@rspress/shared';
import { UNVERSIONED, UNVERSIONED_PREFIX } from "./constants.js";
export const removeBothEndsSlashes = (str) => str?.replace(/^\/|\/$/g, '') || '';
export const getPdfName = (lang, userBase, title) => `${removeBothEndsSlashes(userBase) || title || 'exported'}-${lang}.pdf`;
export const isExplicitlyUnversioned = (version) => version === UNVERSIONED || !!version?.startsWith(UNVERSIONED_PREFIX);
export const isUnversioned = (version) => !version || isExplicitlyUnversioned(version);
export const getUnversionedVersion = (version) => {
    if (!version || version === UNVERSIONED) {
        return;
    }
    return version.startsWith(UNVERSIONED_PREFIX)
        ? version.slice(UNVERSIONED_PREFIX.length)
        : version;
};
/**
 * hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
 */
export const extractTextAndId = (title) => {
    const customIdReg = /\\?\{#[^}]*\}/;
    const text = title.replace(customIdReg, '').trimEnd();
    const customId = title.match(customIdReg)?.[0]?.slice(2, -1) || '';
    return [
        text.replace(/\\{2}/g, '').replace(/(?:[^\\]\\([[\]]))+/g, '$1'),
        customId,
    ];
};
export const withoutBase = (path, base) => addLeadingSlash(path).replace(normalizeSlash(base), '');
export const matchNavbar = (item, currentPathname, base) => new RegExp(item.activeMatch || item.link).test(withoutBase(currentPathname, base));
