"use strict";
const sheep_store_index = require("../store/index.js");
const sheep_config_index = require("../config/index.js");
const cdn = (url = "", cdnurl = "") => {
  if (!url)
    return "";
  if (url.indexOf("http") === 0) {
    return url;
  }
  if (cdnurl === "") {
    cdnurl = sheep_store_index.$store("app").info.cdnurl;
  }
  return cdnurl + url;
};
const $url = {
  // 添加cdn域名前缀
  cdn,
  // 对象存储自动剪裁缩略图
  thumb: (url = "", params) => {
    url = cdn(url);
    return append_thumbnail_params(url, params);
  },
  // 静态资源地址
  static: (url = "", staticurl = "") => {
    if (staticurl === "") {
      staticurl = sheep_config_index.staticUrl;
    }
    if (staticurl !== "local") {
      url = cdn(url, staticurl);
    }
    return url;
  },
  // css背景图片地址
  css: (url = "", staticurl = "") => {
    if (staticurl === "") {
      staticurl = sheep_config_index.staticUrl;
    }
    if (staticurl !== "local") {
      url = cdn(url, staticurl);
    }
    return `url(${url})`;
  }
};
function append_thumbnail_params(url, params) {
  const filesystem = sheep_store_index.$store("app").info.filesystem;
  if (filesystem === "public") {
    return url;
  }
  let width = params.width || "200";
  let height = params.height || "200";
  let mode = params.mode || "lfit";
  let quality = params.quality || 90;
  let gravity = params.gravity || "center";
  let suffix = "";
  let crop_str = "";
  let quality_str = "";
  let size = width + "x" + height;
  switch (filesystem) {
    case "aliyun":
      if (!gravity && gravity != "center") {
        mode = "mfit";
        crop_str = "/crop,g_" + gravityFormat("aliyun", gravity) + ",w_" + width + ",h_" + height;
      }
      if (quality > 0 && quality < 100) {
        quality_str = "/quality,q_" + quality;
      }
      suffix = "x-oss-process=image/resize,m_" + mode + ",w_" + width + ",h_" + height;
      suffix += crop_str + quality_str;
      break;
    case "qcloud":
      let mode_str = "thumbnail";
      if (mode == "fill" || !gravity && gravity != "center") {
        mode_str = "crop";
        mode = "fill";
        crop_str = "/gravity/" + gravityFormat("qcloud", gravity);
      }
      if (quality > 0 && quality < 100) {
        quality_str = "/rquality/" + quality;
      }
      switch (mode) {
        case "lfit":
          size = "" + size + ">";
          break;
        case "mfit":
          size = "!" + size + "r";
        case "fill":
          break;
        case "pad":
          size = size + "/pad/1";
          break;
        case "fixed":
          size = size + "!";
          break;
      }
      suffix = "imageMogr2/" + mode_str + "/" + size + crop_str + quality_str;
      break;
    case "qiniu":
      if (mode == "fill" || !gravity && gravity != "center") {
        mode = "mfit";
        crop_str = "/gravity/" + gravityFormat("qiniu", gravity) + "/crop/" + size;
      }
      if (quality > 0 && quality < 100) {
        quality_str = "/quality/" + quality;
      }
      switch (mode) {
        case "lfit":
        case "pad":
          size = size + ">";
          break;
        case "mfit":
          size = "!" + size + "r";
          break;
        case "fill":
          break;
        case "fixed":
          size = size + "!";
          break;
      }
      suffix = "imageMogr2/thumbnail/" + size + crop_str + quality_str;
      break;
  }
  return url + "?" + suffix;
}
exports.$url = $url;
