"use strict";

exports.__esModule = true;
exports.default = ossDprURL;

require("core-js/modules/es6.regexp.replace");

require("core-js/modules/es6.regexp.match");

var _constant = require("../utils/constant");

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

function ossDprURL(url, defaultDpr) {
  if (defaultDpr === void 0) {
    defaultDpr = 2;
  }

  if (USER_DPR === defaultDpr || !OSS_URL_REG.test(url)) return url;
  var matched = url.match(OSS_URL_REG);
  var factor = USER_DPR / defaultDpr;
  var width = Math.floor(matched[WIDTH_INDEX] * factor);
  var height = '';

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

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

;