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 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://open.feishu.cn/document/client-docs/h5/
// 鉴权: https://open.feishu.cn/document/uYjL24iN/uQjMuQjMuQjM/authentication/h5sdkconfig

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() {
    window.top.tt.closeWindow({
      fail: function fail(res) {
        console.log("closeWindow fail: ", res);
      }
    });
  },
  // 返回监听
  onHistoryBack: function onHistoryBack(params) {
    console.log('飞书不支持监听物理返回');
  },
  // 地图查看
  openLocation: function openLocation(params) {
    if (_.isEmpty(params)) return;
    var newParams = coordToFit(params);
    console.log('调用飞书地图...', newParams);
    var scale = params === null || params === void 0 ? void 0 : params.scale;
    if (!scale) {
      scale = 12;
    } else if (scale < 5) {
      scale = 5;
    } else if (scale > 18) {
      scale = 18;
    }
    window.top.tt.openLocation({
      latitude: newParams.latitude,
      longitude: newParams.longitude,
      scale: scale,
      name: newParams.name,
      address: newParams.address,
      fail: function fail(error) {
        console.log('Lark 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;
    var targetType = type || 'gcj02';
    console.log('调用飞书定位...', params);
    window.top.tt.getLocation({
      // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
      type: targetType,
      success: function success(res) {
        if (!res.longitude || !res.latitude) {
          console.error('飞书定位失败', res);
          if (_fail) {
            _fail({
              errMsg: 'getLocation:fail'
            });
          }
          return;
        }
        var result = {
          errMsg: 'getLocation:ok',
          longitude: res.longitude,
          latitude: res.latitude,
          type: res.type,
          accuracy: res.accuracy
        };

        // If result type not params type, transform result type to params type
        if (res.type && res.type !== targetType) {
          var points = GeoUtil.coordtransform([res.longitude, res.latitude], res.type, targetType);
          result = {
            errMsg: 'getLocation:ok',
            longitude: points[0],
            latitude: points[1],
            type: targetType,
            accuracy: res.accuracy
          };
        }
        if (_success) _success(result);
      },
      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;
    window.top.tt.scanCode({
      scanType: scanType,
      barCodeInput: true,
      success: function success(res) {
        _success2 && _success2(res);
      },
      fail: function fail(res) {
        _fail2 && _fail2(res);
      }
    });
  },
  /**
   * 照片预览
   * @param {Object} params
     {
       index: 0, // 当前显示图片索引，默认 0
       current: '', // 当前显示图片的http链接
       urls: [] // 需要预览的图片http链接列表
     }
   */
  previewImage: function previewImage(params) {
    var _params$urls2;
    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.current);
      if (index < 0) index = 0;
    }
    var current = params === null || params === void 0 || (_params$urls2 = params.urls) === null || _params$urls2 === void 0 ? void 0 : _params$urls2[index];
    window.top.tt.previewImage({
      urls: params === null || params === void 0 ? void 0 : params.urls,
      current: current,
      success: function success(res) {
        (params === null || params === void 0 ? void 0 : params.success) && params.success(res);
      },
      fail: function fail(res) {
        (params === null || params === void 0 ? void 0 : params.fail) && params.fail(res);
      }
    });
  }
});
export default Bridge;