const OSS_HOST_RE = /\.aliyuncs\.com/;

export function ossThumb(url: string | undefined | null, width = 400): string {
  if (!url) return "";
  if (!OSS_HOST_RE.test(url)) return url;
  if (url.includes("x-oss-process")) return url;
  const sep = url.includes("?") ? "&" : "?";
  return `${url}${sep}x-oss-process=image/resize,w_${width}/format,webp`;
}

export function ossWebp(url: string | undefined | null): string {
  if (!url) return "";
  if (!OSS_HOST_RE.test(url)) return url;
  if (url.includes("x-oss-process")) return url;
  const sep = url.includes("?") ? "&" : "?";
  return `${url}${sep}x-oss-process=image/format,webp`;
}
