var _excluded = ["needResult", "scanType", "desc", "success"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// 官方文档: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

import _ from 'lodash';
import BridgeBase from "./base";
import _back from "./utils/back";
import _ready from "./utils/ready";
import coordToFit from "./utils/coordToFit";

// 内库使用-start
import LocaleUtil from "./../LocaleUtil";
import Device from "./../Device";
import Toast from "./../../components/Toast";
// 内库使用-end

/* 测试使用-start
import { LocaleUtil, Device, Toast } from 'seedsui-react'
测试使用-end */

var Bridge = _objectSpread(_objectSpread({}, BridgeBase), {}, {
  ready: function ready(callback, options) {
    _ready(callback, options, this.platform);
  },
  back: function back(backLvl, options) {
    _back(backLvl, options, Bridge);
  },
  /**
   * 定制功能
   */

  // 自定义操作
  invoke: function invoke(api, params, callback) {
    if (!window.top.wx.invoke) {
      console.log('没有wx.invoke的方法');
      return;
    }
    window.top.wx.invoke(api, params, callback);
  },
  // 关闭窗口
  closeWindow: function closeWindow() {
    // 小程序内web-view关闭
    if (['wechatMiniprogram', 'weworkMiniprogram'].includes(Device.platform || '')) {
      window.top.wx.miniProgram.navigateBack({}); // eslint-disable-line
    }
    // 微信关闭
    window.top.wx.closeWindow(); // eslint-disable-line
  },
  // 返回监听
  onHistoryBack: function onHistoryBack(params) {
    if (typeof window.top.wx.onHistoryBack === 'function') {
      window.top.wx.onHistoryBack(params); // eslint-disable-line
    }
  },
  // 地图查看
  openLocation: function openLocation(params) {
    // 微信PC端不支持地图查看
    if (Device.device === 'pc' || this.platform === 'wechat') {
      var errMsg = LocaleUtil.locale('openLocation仅可在企业微信或APP中使用', 'SeedsUI_open_location_prompt', ['openLocation']);
      Toast.show({
        content: errMsg
      });
      (params === null || params === void 0 ? void 0 : params.fail) && params.fail({
        errMsg: errMsg
      });
      return;
    }
    if (_.isEmpty(params)) return;
    var newParams = coordToFit(params);
    console.log('调用企业微信地图...', newParams);
    window.top.wx.openLocation(newParams); // eslint-disable-line
  },
  /**
   * 获取当前地理位置
   * @param {Object} params
   * @prop {String} type 'wgs84'|'gcj02'坐标类型微信默认使用国际坐标'wgs84',
   * @return {Object} {latitude: '纬度', longitude: '经度', speed:'速度', accuracy:'位置精度'}
   */
  getLocation: function getLocation() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    if (!(params !== null && params !== void 0 && params.type)) {
      params.type = 'gcj02';
    }
    // 微信PC端不支持定位
    if (Device.device === 'pc') {
      var _Bridge$getBrowserLoc;
      console.log('PC端微信定位...', params);
      (_Bridge$getBrowserLoc = Bridge.getBrowserLocation) === null || _Bridge$getBrowserLoc === void 0 || _Bridge$getBrowserLoc.call(Bridge, params);
      return;
    }
    console.log('调用微信定位...', params);
    window.top.wx.getLocation(params);
  },
  /*
   * 扫描二维码并返回结果
   * 返回：{resultStr:''}
   * */
  scanQRCode: function scanQRCode() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    // 微信PC端不支持扫码
    if (Device.device === 'pc') {
      Toast.show({
        content: LocaleUtil.locale('scanQRCode仅可在移动端微信或APP中使用', 'SeedsUI_scanQRCode_prompt', LocaleUtil.locale('', 'SeedsUI_scancode'))
      });
      return;
    }
    var _ref = params || {},
      needResult = _ref.needResult,
      scanType = _ref.scanType,
      desc = _ref.desc,
      _success = _ref.success,
      othersParams = _objectWithoutProperties(_ref, _excluded);
    window.top.wx.scanQRCode(_objectSpread({
      needResult: needResult || 1,
      // 默认为0，扫描结果由微信处理，1则直接返回扫描结果
      scanType: scanType || ['qrCode', 'barCode'],
      desc: desc || '二维码／条码',
      success: function success(res) {
        if (!_success) return;
        var wxRes = res;
        // 如果没有设置prefix为false或者空,则清除前缀
        if (!params.prefix) {
          if (res.resultStr.indexOf('QR,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('QR,'.length);
          } else if (res.resultStr.indexOf('EAN_13,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('EAN_13,'.length);
          } else if (res.resultStr.indexOf('EAN_8,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('EAN_8,'.length);
          } else if (res.resultStr.indexOf('AZTEC,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('AZTEC,'.length);
          } else if (res.resultStr.indexOf('DATAMATRIX,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('DATAMATRIX,'.length);
          } else if (res.resultStr.indexOf('UPCA,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('UPCA,'.length);
          } else if (res.resultStr.indexOf('UPC_A,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('UPC_A,'.length);
          } else if (res.resultStr.indexOf('UPCE,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('UPCE,'.length);
          } else if (res.resultStr.indexOf('UPC_E,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('UPC_E,'.length);
          } else if (res.resultStr.indexOf('CODABAR,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('CODABAR,'.length);
          } else if (res.resultStr.indexOf('CODE_39,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('CODE_39,'.length);
          } else if (res.resultStr.indexOf('CODE_93,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('CODE_93,'.length);
          } else if (res.resultStr.indexOf('CODE_128,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('CODE_128,'.length);
          } else if (res.resultStr.indexOf('ITF,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('ITF,'.length);
          } else if (res.resultStr.indexOf('MAXICODE,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('MAXICODE,'.length);
          } else if (res.resultStr.indexOf('PDF_417,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('PDF_417,'.length);
          } else if (res.resultStr.indexOf('RSS_14,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('RSS_14,'.length);
          } else if (res.resultStr.indexOf('RSSEXPANDED,') >= 0) {
            wxRes.resultStr = res.resultStr.substring('RSSEXPANDED,'.length);
          }
        }
        // 回调
        _success(wxRes);
      }
    }, othersParams));
  },
  chooseImage: function chooseImage(params) {
    // 微信PC端不支持扫码
    if (Device.device === 'pc') {
      var _params$fail;
      var errMsg = LocaleUtil.locale('chooseImage仅可在移动端微信或APP中使用', 'SeedsUI_chooseImage_prompt', LocaleUtil.locale('', 'SeedsUI_chooseimage'));
      Toast.show({
        content: errMsg
      });
      params === null || params === void 0 || (_params$fail = params.fail) === null || _params$fail === void 0 || _params$fail.call(params, {
        errCode: 'PC_NOT_IMPLENMENTED',
        errMsg: errMsg
      });
      return;
    }
    window.top.wx.chooseImage(params);
  },
  uploadImage: function uploadImage(params) {
    // 微信PC端不支持扫码
    if (Device.device === 'pc') {
      var _params$fail2;
      var errMsg = LocaleUtil.locale('uploadImage仅可在移动端微信或APP中使用', 'SeedsUI_uploadImage_prompt', LocaleUtil.locale('', 'SeedsUI_uploadimage'));
      Toast.show({
        content: errMsg
      });
      params === null || params === void 0 || (_params$fail2 = params.fail) === null || _params$fail2 === void 0 || _params$fail2.call(params, {
        errCode: 'PC_NOT_IMPLENMENTED',
        errMsg: errMsg
      });
      return;
    }
    window.top.wx.uploadImage(params);
  },
  previewImage: function previewImage(params) {
    // 微信PC端不支持扫码
    if (Device.device === 'pc') {
      Toast.show({
        content: LocaleUtil.locale('previewImage仅可在移动端微信或APP中使用', 'SeedsUI_previewImage_prompt', LocaleUtil.locale('', 'SeedsUI_previewimage'))
      });
      return;
    }
    window.top.wx.previewImage(params);
  },
  /**
   * 文件操作: 预览文件
   * @param {Object} params
   * params: {
   *  url: '', // 需要预览文件的地址(必填，可以使用相对路径)
   *  name: '', // 需要预览文件的文件名(不填的话取url的最后部分)
   *  size: 1048576 // 需要预览文件的字节大小(必填)
   * }
   */
  previewFile: function previewFile(params) {
    // 微信PC端不支持预览文件
    if (Device.device === 'pc' || this.platform === 'wechat') {
      var errMsg = LocaleUtil.locale('previewFile仅可在企业微信或APP中使用', 'SeedsUI_previewFile_prompt', ['previewFile']);
      Toast.show({
        content: errMsg
      });
      (params === null || params === void 0 ? void 0 : params.fail) && params.fail({
        errMsg: errMsg
      });
      return;
    }
    window.top.wx.previewFile(params);
  }
});
export default Bridge;