/**
 * Utilitaires pour la manipulation des URLs
 */

export function buildUrl(
  baseUrl: string,
  path: string,
  params?: Record<string, any>,
): string {
  const url = new URL(path, baseUrl);

  if (params) {
    Object.entries(params).forEach(([key, value]) => {
      if (value !== null && value !== undefined) {
        url.searchParams.set(key, String(value));
      }
    });
  }

  return url.toString();
}

export function parseQueryString(queryString: string): Record<string, string> {
  const params: Record<string, string> = {};

  if (!queryString) return params;

  const searchParams = new URLSearchParams(queryString);
  searchParams.forEach((value, key) => {
    params[key] = value;
  });

  return params;
}

export function buildQueryString(params: Record<string, any>): string {
  const searchParams = new URLSearchParams();

  Object.entries(params).forEach(([key, value]) => {
    if (value !== null && value !== undefined) {
      searchParams.set(key, String(value));
    }
  });

  return searchParams.toString();
}

export function getUrlPathname(url: string): string {
  try {
    return new URL(url).pathname;
  } catch {
    return url;
  }
}

export function getUrlHostname(url: string): string {
  try {
    return new URL(url).hostname;
  } catch {
    return '';
  }
}

export function isAbsoluteUrl(url: string): boolean {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
}

export function isRelativeUrl(url: string): boolean {
  return !isAbsoluteUrl(url) && url.startsWith('/');
}

export function normalizeUrl(url: string): string {
  try {
    const urlObj = new URL(url);
    return urlObj.toString();
  } catch {
    return url;
  }
}

export function addQueryParam(url: string, key: string, value: string): string {
  try {
    const urlObj = new URL(url);
    urlObj.searchParams.set(key, value);
    return urlObj.toString();
  } catch {
    return url;
  }
}

export function removeQueryParam(url: string, key: string): string {
  try {
    const urlObj = new URL(url);
    urlObj.searchParams.delete(key);
    return urlObj.toString();
  } catch {
    return url;
  }
}

export function getQueryParam(url: string, key: string): string | null {
  try {
    const urlObj = new URL(url);
    return urlObj.searchParams.get(key);
  } catch {
    return null;
  }
}

export function hasQueryParam(url: string, key: string): boolean {
  try {
    const urlObj = new URL(url);
    return urlObj.searchParams.has(key);
  } catch {
    return false;
  }
}

export function extractDomain(url: string): string {
  try {
    return new URL(url).hostname;
  } catch {
    return '';
  }
}

export function extractProtocol(url: string): string {
  try {
    return new URL(url).protocol;
  } catch {
    return '';
  }
}

export function extractPort(url: string): string {
  try {
    return new URL(url).port;
  } catch {
    return '';
  }
}

export function isValidUrl(url: string): boolean {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
}

export function isSecureUrl(url: string): boolean {
  try {
    return new URL(url).protocol === 'https:';
  } catch {
    return false;
  }
}

export function getUrlWithoutQuery(url: string): string {
  try {
    const urlObj = new URL(url);
    return `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}`;
  } catch {
    return url;
  }
}

export function getUrlWithoutHash(url: string): string {
  try {
    const urlObj = new URL(url);
    return urlObj.toString().split('#')[0];
  } catch {
    return url;
  }
}

export function getHashFromUrl(url: string): string {
  try {
    return new URL(url).hash;
  } catch {
    return '';
  }
}

export function setHashInUrl(url: string, hash: string): string {
  try {
    const urlObj = new URL(url);
    urlObj.hash = hash;
    return urlObj.toString();
  } catch {
    return url;
  }
}

export function removeHashFromUrl(url: string): string {
  try {
    const urlObj = new URL(url);
    urlObj.hash = '';
    return urlObj.toString();
  } catch {
    return url;
  }
}

export function getBaseUrl(url: string): string {
  try {
    const urlObj = new URL(url);
    return `${urlObj.protocol}//${urlObj.host}`;
  } catch {
    return '';
  }
}

export function getRelativePath(url: string): string {
  try {
    return new URL(url).pathname;
  } catch {
    return url;
  }
}

export function isSameOrigin(url1: string, url2: string): boolean {
  try {
    const urlObj1 = new URL(url1);
    const urlObj2 = new URL(url2);
    return urlObj1.origin === urlObj2.origin;
  } catch {
    return false;
  }
}

export function getUrlSegments(url: string): string[] {
  try {
    const pathname = new URL(url).pathname;
    return pathname.split('/').filter((segment) => segment !== '');
  } catch {
    return [];
  }
}

export function getLastUrlSegment(url: string): string {
  const segments = getUrlSegments(url);
  return segments[segments.length - 1] || '';
}

export function getParentUrl(url: string): string {
  try {
    const urlObj = new URL(url);
    const segments = urlObj.pathname
      .split('/')
      .filter((segment) => segment !== '');
    segments.pop();
    urlObj.pathname = `/${segments.join('/')}`;
    return urlObj.toString();
  } catch {
    return url;
  }
}
