import { useRuntimeConfig } from '#imports';

const getBasePath = (): string => {
    try {
        const config = useRuntimeConfig();
        return config.public.basePath;
    } catch (e) {
        console.error('Error getting base path:', e);
        return process.env.NODE_ENV === 'production' ? process.env.BASE_PATH || '/' : '/';
    }
};


export const withBasePath = (path: string): string => {
    if (path.startsWith('http://') || path.startsWith('https://')) {return path;}
    const currentBasePath = getBasePath();
    if (!currentBasePath || path === '') {return path;}
    if (path.startsWith(currentBasePath)) {return path;}
    if (path.startsWith('~/')) {return path;}
    const normalizedPath = path.startsWith('/') ? path : `/${path}`;
    return `${currentBasePath}${normalizedPath}`;
};