'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultServicesQueryOptions = exports.mapQueryTypeKeys = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _basicMapApiClass = require('../util/basicMapApiClass');

var _basicMapApiClass2 = _interopRequireDefault(_basicMapApiClass);

var _assign = require('lodash/assign');

var _assign2 = _interopRequireDefault(_assign);

var _methodsUtil = require('../../_util/methods-util');

var _wfs = require('./wfs');

var _wfs2 = _interopRequireDefault(_wfs);

var _arcgisWFS = require('./arcgisWFS');

var _arcgisWFS2 = _interopRequireDefault(_arcgisWFS);

var _geoQuery = require('./geoQuery');

var _geoQuery2 = _interopRequireDefault(_geoQuery);

var _arcgisQuery = require('./arcgisQuery');

var _arcgisQuery2 = _interopRequireDefault(_arcgisQuery);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

// 内置地图Web GIS服务查询类型枚举名
var mapQueryTypeKeys = exports.mapQueryTypeKeys = ['WFS', 'ArcgisWFS', 'GeoQuery', 'ArcgisQuery'];

// 服务数据源的默认参数选项
/**
 * @文件说明: 构建Map.Query地图Web GIS服务查询对象
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-27 21:50:00
 */

var defaultServicesQueryOptions = exports.defaultServicesQueryOptions = {
  // 是否拼接代理服务地址
  proxy: true,
  // 查询过滤条件，规则：['==', 'key', 'value'] 或 ['AND | OR | NOT', [], [], ...]
  filters: null,
  // 主键字段，返回要素作为Feature的ID，若为空，则以随机值替代
  idField: null
};

// 效验Query查询服务的基础属性的有效性
var validateQueryOptions = function validateQueryOptions(url, typeName, options) {
  var proxyURL = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';

  if (!url || !typeName) {
    console.error('WebGIS Query\u67E5\u8BE2\u670D\u52A1[url]\u3001[typeName]\u5C5E\u6027\u5FC5\u9700\u8BBE\u5B9A.');
    return;
  }
  var errorUrlMsg = 'WebGIS Query查询服务[url]属性格式不是一个有效的链接地址.';
  if (!(0, _methodsUtil.isHttpUrl)(url)) {
    console.error(errorUrlMsg);
    return;
  }
  var opts = (0, _assign2['default'])({}, defaultServicesQueryOptions, options);
  // 判断是否需要代理服务
  var isProxyUrl = (0, _methodsUtil.isBooleanFlase)(opts.proxy) ? false : true;
  var queryUrl = url;
  isProxyUrl && (queryUrl = '' + proxyURL + url);

  return { url: queryUrl, typeName: typeName, options: opts };
};

var Query = function (_BasicMapApi) {
  (0, _inherits3['default'])(Query, _BasicMapApi);
  (0, _createClass3['default'])(Query, [{
    key: 'typeKeys',

    /**
     * 获取Web GIS服务查询类型的名称
     * @readonly
     */
    get: function get() {
      return mapQueryTypeKeys;
    }

    /**
     * Query地图Web GIS服务
     * @param {MapApi} iMapApi 地图Api实例化对象
     */

  }]);

  function Query() {
    var _ref;

    (0, _classCallCheck3['default'])(this, Query);

    for (var _len = arguments.length, arg = Array(_len), _key = 0; _key < _len; _key++) {
      arg[_key] = arguments[_key];
    }

    var _this = (0, _possibleConstructorReturn3['default'])(this, (_ref = Query.__proto__ || Object.getPrototypeOf(Query)).call.apply(_ref, [this].concat(arg)));

    var iMapApi = _this.iMapApi;
    _this.proxyURL = iMapApi && iMapApi.proxyURL || '';
    return _this;
  }

  /**
   * 发送WFS类型查询服务请求，获取标准GeoJSON格式的要素数据
   * @param {String} url 服务地址
   * @param {String} typeName 要素类型标识名
   * @param {Object} options 查询服务的参数选项
   */


  (0, _createClass3['default'])(Query, [{
    key: 'fetchWFSTask',
    value: function () {
      var _ref2 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee(url, typeName) {
        var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
        var result, GeoGlobe, query, promise;
        return _regenerator2['default'].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                result = validateQueryOptions(url, typeName, options, this.proxyURL);

                if (result) {
                  _context.next = 3;
                  break;
                }

                return _context.abrupt('return', null);

              case 3:
                // 判断是否有GeoGlobe对象
                GeoGlobe = this.iMapApi && this.iMapApi.exports && this.iMapApi.exports.GeoGlobe || null;

                if (!GeoGlobe) {
                  options.GeoGlobe && (GeoGlobe = options.GeoGlobe);
                }

                if (GeoGlobe) {
                  _context.next = 8;
                  break;
                }

                console.error('WFS服务缺少前置GeoGlobe对象，请在[options.GeoGlobe]属性中赋值.');
                return _context.abrupt('return', null);

              case 8:
                query = new _wfs2['default'](GeoGlobe);
                promise = query.queryTask(result.url, result.typeName, result.options);

                query = null;
                return _context.abrupt('return', promise);

              case 12:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function fetchWFSTask(_x3, _x4) {
        return _ref2.apply(this, arguments);
      }

      return fetchWFSTask;
    }()

    /**
     * 发送Arcgis WFS类型查询服务请求，获取标准GeoJSON格式的要素数据
     * @param {String} url 服务地址
     * @param {String} typeName 要素类型标识名
     * @param {Object} options 查询服务的参数选项
     */

  }, {
    key: 'fetchArcgisWFSTask',
    value: function () {
      var _ref3 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee2(url, typeName) {
        var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
        var result, GeoGlobe, query, promise;
        return _regenerator2['default'].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                result = validateQueryOptions(url, typeName, options, this.proxyURL);

                if (result) {
                  _context2.next = 3;
                  break;
                }

                return _context2.abrupt('return', null);

              case 3:
                // 判断是否有GeoGlobe对象
                GeoGlobe = this.iMapApi && this.iMapApi.exports && this.iMapApi.exports.GeoGlobe || null;

                if (!GeoGlobe) {
                  options.GeoGlobe && (GeoGlobe = options.GeoGlobe);
                }

                if (GeoGlobe) {
                  _context2.next = 8;
                  break;
                }

                console.error('Arcgis WFS服务缺少前置GeoGlobe对象，请在[options.GeoGlobe]属性中赋值.');
                return _context2.abrupt('return', null);

              case 8:
                query = new _arcgisWFS2['default'](GeoGlobe);
                promise = query.queryTask(result.url, result.typeName, result.options);

                query = null;
                return _context2.abrupt('return', promise);

              case 12:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function fetchArcgisWFSTask(_x6, _x7) {
        return _ref3.apply(this, arguments);
      }

      return fetchArcgisWFSTask;
    }()

    /**
     * 发送GeoQuery类型查询服务请求，获取标准GeoJSON格式的要素数据
     * @param {String} url 服务地址
     * @param {String} typeName 要素类型标识名
     * @param {Object} options 查询服务的参数选项
     */

  }, {
    key: 'fetchGeoQueryTask',
    value: function () {
      var _ref4 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee3(url, typeName) {
        var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
        var result, query, promise;
        return _regenerator2['default'].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                result = validateQueryOptions(url, typeName, options, this.proxyURL);

                if (result) {
                  _context3.next = 3;
                  break;
                }

                return _context3.abrupt('return', null);

              case 3:
                query = new _geoQuery2['default']();
                promise = query.queryTask(result.url, result.typeName, result.options);

                query = null;
                return _context3.abrupt('return', promise);

              case 7:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function fetchGeoQueryTask(_x9, _x10) {
        return _ref4.apply(this, arguments);
      }

      return fetchGeoQueryTask;
    }()

    /**
     * 发送Arcgis Query类型查询服务请求，获取标准GeoJSON格式的要素数据
     * @param {String} url 服务地址
     * @param {String} typeName 要素类型标识名
     * @param {Object} options 查询服务的参数选项
     */

  }, {
    key: 'fetchArcgisQueryTask',
    value: function () {
      var _ref5 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee4(url, typeName) {
        var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
        var result, query, promise;
        return _regenerator2['default'].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                result = validateQueryOptions(url, typeName, options, this.proxyURL);

                if (result) {
                  _context4.next = 3;
                  break;
                }

                return _context4.abrupt('return', null);

              case 3:
                query = new _arcgisQuery2['default']();
                // Arcgis Query接口无需代理服务

                promise = query.queryTask(result.url.replace(this.proxyURL || '', ''), result.typeName, result.options);

                query = null;
                return _context4.abrupt('return', promise);

              case 7:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function fetchArcgisQueryTask(_x12, _x13) {
        return _ref5.apply(this, arguments);
      }

      return fetchArcgisQueryTask;
    }()

    /**
     * 根据对应内置的Web GIS Service服务查询类型发送请求，获取标准GeoJSON格式的要素数据
     * @param {String} type 内置Web GIS Service查询服务类型
     * @param {String} url 服务地址
     * @param {String} typeName 要素类型标识名
     * @param {Object} options 查询服务的参数选项
     */

  }, {
    key: 'fetchServicesTask',
    value: function () {
      var _ref6 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee5(type, url, typeName) {
        var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
        return _regenerator2['default'].wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _context5.t0 = type;
                _context5.next = _context5.t0 === 'WFS' ? 3 : _context5.t0 === 'ArcgisWFS' ? 4 : _context5.t0 === 'GeoQuery' ? 5 : _context5.t0 === 'ArcgisQuery' ? 6 : 7;
                break;

              case 3:
                return _context5.abrupt('return', this.fetchWFSTask(url, typeName, options));

              case 4:
                return _context5.abrupt('return', this.fetchArcgisWFSTask(url, typeName, options));

              case 5:
                return _context5.abrupt('return', this.fetchGeoQueryTask(url, typeName, options));

              case 6:
                return _context5.abrupt('return', this.fetchArcgisQueryTask(url, typeName, options));

              case 7:
                return _context5.abrupt('return', null);

              case 8:
              case 'end':
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      function fetchServicesTask(_x15, _x16, _x17) {
        return _ref6.apply(this, arguments);
      }

      return fetchServicesTask;
    }()

    /**
     * 根据配置方案的查询分析服务Key名获取Web GIS Service服务查询对象，并发送请求获取要素数据
     * @param {String} key 配置方案的查询服务项Key名
     * @param {Object} options 查询服务的参数选项
     */

  }, {
    key: 'fetchServicesTaskByKey',
    value: function () {
      var _ref7 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee6(key) {
        var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
        var service, opts;
        return _regenerator2['default'].wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                if (!(this.iMapApi && key)) {
                  _context6.next = 7;
                  break;
                }

                service = this.iMapApi.getMapQueryServiceByKey(key);

                if (!service) {
                  _context6.next = 5;
                  break;
                }

                opts = (0, _assign2['default'])({}, service.options, options);
                return _context6.abrupt('return', this.fetchServicesTask(service.type, service.url, service.featureType, opts));

              case 5:
                _context6.next = 8;
                break;

              case 7:
                console.error('地图的静态Query对象暂不支持该函数接口.');

              case 8:
                return _context6.abrupt('return', null);

              case 9:
              case 'end':
                return _context6.stop();
            }
          }
        }, _callee6, this);
      }));

      function fetchServicesTaskByKey(_x19) {
        return _ref7.apply(this, arguments);
      }

      return fetchServicesTaskByKey;
    }()
  }]);
  return Query;
}(_basicMapApiClass2['default']);

exports['default'] = Query;