var _excluded = ["type", "success", "fail", "complete"];
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://myjsapi.alipay.com/alipayjsapi/media/image/chooseImage.html
// 小程序文档: https://opendocs.alipay.com/mini/component?pathHash=0cf5b4c0
import _ from 'lodash';
import BridgeBase from "./base";
import _back from "./utils/back";
import _ready from "./utils/ready";
import coordToFit from "./utils/coordToFit";

// 内库使用-start
import GeoUtil from "./../GeoUtil";
// 内库使用-end

/* 测试使用-start
import { GeoUtil } 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);
  },
  /**
   * 定制功能
   */
  // 关闭窗口
  closeWindow: function closeWindow() {
    var _window$top$ap;
    (_window$top$ap = window.top.ap) === null || _window$top$ap === void 0 || _window$top$ap.popWindow();
  },
  // 返回监听
  onHistoryBack: function onHistoryBack(params) {
    console.log('支付宝不支持监听物理返回');
  },
  // 地图查看
  openLocation: function openLocation(params) {
    if (_.isEmpty(params)) return;
    var newParams = coordToFit(params);
    console.log('调用支付宝地图...', newParams);
    window.top.ap.openLocation({
      latitude: newParams.latitude,
      longitude: newParams.longitude,
      name: newParams.name || '',
      address: newParams.address || '',
      fail: function fail(error) {
        console.log('Alipay openLocation fail:', error);
      }
    });
  },
  /**
   * 获取当前地理位置
   * @param {Object} params
   * params: {
   * type {String}: 'wgs84'|'gcj02'坐标类型微信默认使用国际坐标'wgs84',
   * }
   * @returns {Object} {latitude: '纬度', longitude: '经度', speed:'速度', accuracy:'位置精度'}
   */
  getLocation: function getLocation() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var _ref = params || {},
      type = _ref.type,
      _success = _ref.success,
      _fail = _ref.fail,
      complete = _ref.complete,
      otherParams = _objectWithoutProperties(_ref, _excluded);

    // 调用定位
    console.log('调用支付宝定位...', params);
    window.top.ap.getLocation(_objectSpread(_objectSpread({}, otherParams), {}, {
      type: '2',
      success: function success(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        if (!longitude || !latitude) {
          console.error('支付宝定位失败', res);
          if (_fail) {
            _fail({
              errMsg: 'getLocation:fail'
            });
          }
          return;
        }
        if (type === 'wgs84') {
          var points = GeoUtil.coordtransform([longitude, latitude], 'gcj02', 'wgs84');
          longitude = points[0];
          latitude = points[1];
        }
        if (_success) {
          _success({
            errMsg: 'getLocation:ok',
            longitude: longitude,
            latitude: latitude,
            type: type || 'gcj02',
            accuracy: res.accuracy
          });
        }
      },
      fail: function fail(err) {
        console.log('getLocation:fail', err);
        if (_fail) {
          _fail({
            errMsg: 'getLocation:fail'
          });
        }
      }
    }));
  },
  /**
   * 扫码
   * @param {Object} params
   * @returns {Object} {latitude: '纬度', longitude: '经度', speed:'速度', accuracy:'位置精度'}
   */
  scanQRCode: function scanQRCode() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var _ref2 = params || {},
      scanType = _ref2.scanType,
      _success2 = _ref2.success,
      _fail2 = _ref2.fail;
    var type = '';
    if (scanType.length === 1) {
      if (scanType.includes('qrCode')) {
        type = 'qr';
      } else if (scanType.includes('barCode')) {
        type = 'bar';
      }
    }
    window.top.ap.scan({
      type: type,
      success: function success(res) {
        _success2 && _success2({
          resultStr: res.code
        });
      },
      fail: function fail(res) {
        if (_fail2) {
          _fail2({
            errCode: res.errorCode,
            errMsg: res.errorMessage
          });
        }
      }
    });
  },
  /**
   * 照片预览
   * @param {Object} params
     {
       index: 0, // 当前显示图片索引，默认 0
       current: '', // 当前显示图片的http链接
       urls: [] // 需要预览的图片http链接列表
     }
   */
  previewImage: function previewImage(params) {
    var index = (params === null || params === void 0 ? void 0 : params.index) || 0;
    if (typeof (params === null || params === void 0 ? void 0 : params.index) !== 'number' && typeof (params === null || params === void 0 ? void 0 : params.current) === 'string') {
      var _params$urls, _params$urls$indexOf;
      index = params === null || params === void 0 || (_params$urls = params.urls) === null || _params$urls === void 0 || (_params$urls$indexOf = _params$urls.indexOf) === null || _params$urls$indexOf === void 0 ? void 0 : _params$urls$indexOf.call(_params$urls, params === null || params === void 0 ? void 0 : params.current);
      if (index < 0) index = 0;
    }
    window.top.ap.previewImage({
      urls: params === null || params === void 0 ? void 0 : params.urls,
      current: index
    });
  }
});
export default Bridge;