import { isServer } from '../_utils/constant';
const USER_DPR = Math.min(isServer ? 2 : devicePixelRatio, 2);
const OSS_URL_REG = /(x-oss-process=image\/resize,w_)(\d+)((,h_)(\d+))?/;
const WIDTH_INDEX = 2;
const HEIGHT_INDEX = 5;
/**
 * @param  {String} url
 * @param  {Number} defaultDpr 目前项目内大多数默认 dpr 为2
 *
 * @returns {String}
 */

export default function ossDprURL(url, defaultDpr = 2) {
  if (USER_DPR === defaultDpr || !OSS_URL_REG.test(url)) return url;
  const matched = url.match(OSS_URL_REG);
  const factor = USER_DPR / defaultDpr;
  const width = Math.floor(matched[WIDTH_INDEX] * factor);
  let height = '';

  if (matched[HEIGHT_INDEX]) {
    height = Math.floor(matched[HEIGHT_INDEX] * factor);
  }

  return url.replace(OSS_URL_REG, `$1${width}$4${height}`);
}
;