'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var WRITE_PHOTO_ALBUM_SCOPE = 'scope.writePhotosAlbum';
var TIP_MAP = {
  saveImage: '保存到相册',
  saveImageSuccess: '已保存到相册',
  saveImageFail: '保存相册失败',
  saveImageFailOfAuth: '保存相册失败，请检查权限',
  authConfirmContent: '检测到您没打开保存图片权限，是否去设置打开？',
  authConfirmFailToast: '您没有授权，无法保存到相册'
};
function authConfirm(options) {
  return new Promise(function (resolve, reject) {
    wx.showModal({
      content: options.authConfirmContent,
      confirmText: '确认',
      cancelText: '取消',
      success: function success(res) {
        if (res.confirm) {
          wx.openSetting({
            success: function success(res) {
              if (res.authSetting[WRITE_PHOTO_ALBUM_SCOPE]) {
                resolve(true);
              } else {
                wx.showToast({
                  title: options.authConfirmFailToast,
                  icon: 'none'
                });
                reject();
              }
            }
          });
        } else {
          wx.showToast({
            title: options.authConfirmFailToast,
            icon: 'none'
          });
          reject();
        }
      }
    });
  });
}
function showSaveImageModal(_ref) {
  var url = _ref.url,
    options = _ref.options;
  return new Promise(function (resolve, reject) {
    wx.showActionSheet({
      itemList: [options.saveImage],
      success: function success() {
        resolve({
          url: url,
          options: options
        });
      },
      fail: function fail(err) {
        reject(err);
      }
    });
  });
}
// 授权
function checkWritePhotoAuth(_ref2) {
  var url = _ref2.url,
    options = _ref2.options;
  return new Promise(function (resolve, reject) {
    wx.getSetting({
      success: function success(settings) {
        if (!settings.authSetting[WRITE_PHOTO_ALBUM_SCOPE]) {
          wx.authorize({
            scope: WRITE_PHOTO_ALBUM_SCOPE,
            success: function success() {
              // 同意授权
              resolve({
                url: url,
                options: options
              });
            },
            fail: function fail() {
              authConfirm(options).then(function () {
                resolve({
                  url: url,
                  options: options
                });
              })["catch"](reject);
            }
          });
        } else {
          // 已经有权限了
          resolve({
            url: url,
            options: options
          });
        }
      }
    });
  });
}
// 转换图片格式为本地路径
function getImageLocalPath(_ref3) {
  var url = _ref3.url,
    options = _ref3.options;
  return new Promise(function (resolve, reject) {
    wx.getImageInfo({
      src: url,
      success: function success(res) {
        resolve({
          path: res.path,
          options: options
        });
      },
      fail: function fail(err) {
        reject(err);
      }
    });
  });
}
function saveImageToAlbum(_ref4) {
  var path = _ref4.path,
    options = _ref4.options;
  var p4 = new Promise(function (resolve, reject) {
    wx.saveImageToPhotosAlbum({
      filePath: path,
      success: function success() {
        wx.showToast({
          title: options.saveImageSuccess
        });
        resolve(true);
      },
      fail: function fail(err) {
        var message = options.saveImageFail;
        var _ref5 = err || {},
          _ref5$errMsg = _ref5.errMsg,
          errMsg = _ref5$errMsg === void 0 ? '' : _ref5$errMsg;
        if (errMsg.indexOf('fail system deny') > -1 || errMsg.indexOf('fail save error') > -1) {
          message = options.saveImageFailOfAuth;
        }
        wx.showToast({
          title: message,
          icon: 'none'
        });
        console.log(err);
        reject(err);
      }
    });
  });
  return p4;
}
/**
 * 小程序下保存图片
 * @param {string} url 图片地址
 * @param {object} options 提示选项
 * @example
 * ```ts
 * saveMpImage('https://xxx.png');
 * ```
 */
function saveMpImage(url) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TIP_MAP;
  var newOptions = Object.assign(Object.assign({}, TIP_MAP), options);
  showSaveImageModal({
    url: url,
    options: newOptions
  }).then(checkWritePhotoAuth).then(getImageLocalPath).then(saveImageToAlbum)["catch"](function () {});
}

exports.saveMpImage = saveMpImage;
