const JOBS_BASE = '/influencer/jobs';

export const extractJobSlugFromPath = (path: string) =>
    path.match(/\/influencer\/jobs\/([^/?#]+)/)?.[1] ?? null;

const buildTabQuery = (tab?: string) => (tab && tab !== 'all' ? `?tab=${tab}` : '');

export const getJobListUrl = (tab?: string) => `${JOBS_BASE}${buildTabQuery(tab)}`;

export const getJobDetailUrl = (slug: string, tab?: string) =>
    `${JOBS_BASE}/${slug}${buildTabQuery(tab)}`;

/** Clean share URL — slug only, no query params (tab etc. live in React state). */
export const getJobShareUrl = (slug: string) => `${JOBS_BASE}/${slug}`;

/** Update the address bar without a Next.js navigation (keeps list state + scroll). */
export const pushJobListUrl = (tab?: string) => {
    if (typeof window === 'undefined') return;
    const url = getJobListUrl(tab);
    window.history.pushState({ jobSlug: null }, '', url);
};

export const pushJobDetailUrl = (slug: string, tab?: string) => {
    if (typeof window === 'undefined') return;
    const url = getJobDetailUrl(slug, tab);
    window.history.pushState({ jobSlug: slug }, '', url);
};
