export function T() {
  return true;
} // Fix IE file.status problem
// via coping a new Object

export function fileToObject(file) {
  return {
    lastModified: file.lastModified,
    lastModifiedDate: file.lastModifiedDate,
    name: file.filename || file.name,
    size: file.size,
    type: file.type,
    uid: file.uid,
    response: file.response,
    error: file.error,
    percent: 0,
    originFileObj: file
  };
}
/**
 * 生成Progress percent: 0.1 -> 0.98
 *   - for ie
 */

export function genPercentAdd() {
  var k = 0.1;
  var i = 0.01;
  var end = 0.98;
  return function (s) {
    var start = s;

    if (start >= end) {
      return start;
    }

    start += k;
    k -= i;

    if (k < 0.001) {
      k = 0.001;
    }

    return start;
  };
}
export function getFileItem(file, fileList) {
  var matchKey = file.uid !== undefined ? 'uid' : 'name';
  return fileList.filter(function (item) {
    return item[matchKey] === file[matchKey];
  })[0];
}
export function removeFileItem(file, fileList) {
  var matchKey = file.uid !== undefined ? 'uid' : 'name';
  var removed = fileList.filter(function (item) {
    return item[matchKey] !== file[matchKey];
  });

  if (removed.length === fileList.length) {
    return null;
  }

  return removed;
}

var isImageFileType = function isImageFileType(type) {
  return type.indexOf('image/') === 0;
};

var MEASURE_SIZE = 200;
export function previewImage(file) {
  return new Promise(function (resolve) {
    if (!file.type || !isImageFileType(file.type)) {
      var reader = new FileReader();

      reader.onloadend = function () {
        return resolve((reader.result || '').toString());
      };

      reader.readAsDataURL(file);
      return;
    }

    var canvas = document.createElement('canvas');
    canvas.width = MEASURE_SIZE;
    canvas.height = MEASURE_SIZE;
    canvas.style.cssText = "position: fixed; left: 0; top: 0; width: ".concat(MEASURE_SIZE, "px; height: ").concat(MEASURE_SIZE, "px; z-index: 9999; display: none;");
    document.body.appendChild(canvas);
    var ctx = canvas.getContext('2d');
    var img = new Image();

    img.onload = function () {
      var width = img.width,
          height = img.height;
      var drawWidth = MEASURE_SIZE;
      var drawHeight = MEASURE_SIZE;
      var offsetX = 0;
      var offsetY = 0;

      if (width < height) {
        drawHeight = height * (MEASURE_SIZE / width);
        offsetY = -(drawHeight - drawWidth) / 2;
      } else {
        drawWidth = width * (MEASURE_SIZE / height);
        offsetX = -(drawWidth - drawHeight) / 2;
      }

      ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
      var dataURL = canvas.toDataURL();
      document.body.removeChild(canvas);
      resolve(dataURL);
    };

    img.src = window.URL.createObjectURL(file);
  });
}
/**
 * 获取部分文件类型
 * @param filename 文件名
 * @returns 文件类型
 */

export function getFileType(filename) {
  if (/\.(zip|rar|rar4|7z|Z|gz|bz2|xz|tar|tar\.gz|tar\.bz2|tar\.xz)$/i.test(filename)) {
    return 'compressedfile';
  }

  var suffix = filename ? filename.lastIndexOf('.') === -1 ? '' : filename.split('.').pop() : '';

  switch (suffix ? suffix.toLowerCase() : '') {
    case 'doc':
    case 'docx':
      return 'doc';

    case 'webp':
    case 'svg':
    case 'png':
    case 'gif':
    case 'jpg':
    case 'jpeg':
    case 'jfif':
    case 'bmp':
    case 'dpg':
    case 'ico':
      return 'image';

    case 'pdf':
      return 'pdf';

    case 'xls':
    case 'xlsx':
      return 'xls';

    default:
      return 'other';
  }
}
export function getFileSizeStr(filesize) {
  var scale = 1024;

  if (isNaN(filesize / scale)) {
    return '';
  }

  return filesize / scale / scale >= 0.09 ? filesize / scale / scale / scale >= 0.09 ? "".concat((filesize / scale / scale / scale).toFixed(1), "GB") : "".concat((filesize / scale / scale).toFixed(1), "MB") : "".concat((filesize / scale).toFixed(1), "KB");
}

var extname = function extname() {
  var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var temp = url.split('/');
  var filename = temp[temp.length - 1];
  var filenameWithoutSuffix = filename.split(/#|\?/)[0];
  return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
};

export var isImageUrl = function isImageUrl(file) {
  if (file.type && !file.thumbUrl) {
    return isImageFileType(file.type);
  }

  var url = file.name || file.thumbUrl || file.url || '';
  var extension = extname(url);

  if (/^data:image\//.test(url) || /(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico)$/i.test(extension)) {
    return true;
  }

  if (/^data:/.test(url)) {
    // other file types of base64
    return false;
  }

  if (extension) {
    // other file types which have extension
    return false;
  }

  return true;
};
//# sourceMappingURL=utils.js.map
