/**
 * 上传地址
 * @returns {*}
 */
import config from "@/config";
import { Session } from "../utils";

/**
 * 上传成功返回详情信息
 * @returns {string}
 */
export const uploadURLReturnDetail = () => {
  return config.get("file/uploadReturnDetail");
};

/**
 * 上传成功仅返回fileId
 * @returns {string}
 */
export const uploadURLReturnId = () => {
  return config.get("file/uploadReturnId");
};

/**
 * 文件下载地址
 * @param fileId
 * @returns {string}
 */
export const downloadURL = (fileId) => {
  const token = Session.getAccessToken() || Session.getRefreshToken();
  return config.get("file/download") + "/" + fileId + `?access_token=${token}`;
};

/**
 * 头像/图片查看地址
 * @param fileId
 * @returns {string}
 */
export const viewImg = (fileId) => {
  const token = Session.getAccessToken() || Session.getRefreshToken();
  return config.get("file/viewImg") + "/" + fileId + `?access_token=${token}`;
};

/**
 * 获取图片后缀
 * @returns {string}
 */
export const getPicSuffix = () => {
  return config.get("picSuffix");
};

/**
 * 数组中某项移至数组末尾
 * @returns {array}
 */
export const convertColumns = (array, fromIndex, toIndex) => {
  array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
  return array;
};

/**
 * 判断是否为JSON字符串
 * @returns {string}
 */
export const exceptStr = (str) => {
  try {
    return JSON.parse(str);
  } catch (err) {
    return {};
  }
};

export const paginationFun = (tableListPageObj, sizeChange, onChange, i18n = {}) => {
  return {
    current: tableListPageObj.pageNum,
    pageSize: tableListPageObj.size,
    total: tableListPageObj.total,
    showTotal: total => `${i18n["共"] ? i18n["共"] : "共"} ${total} ${i18n["条"] ? i18n["条"] : "条"}`,
    showQuickJumper: true,
    showSizeChanger: true,
    pageSizeOptions: ["10", "20", "50", "100"],
    onShowSizeChange: () => sizeChange(),
    onChange: (page, size) => {
      const { total } = tableListPageObj;
      onChange({
        pageNum: page,
        size,
        total
      });
    }
  };
};
