'use strict';

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

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 _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

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

var _assign2 = _interopRequireDefault(_assign);

var _hat = require('hat');

var _hat2 = _interopRequireDefault(_hat);

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

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

var ArcgisWFS = function () {
  function ArcgisWFS(GeoGlobe) {
    (0, _classCallCheck3['default'])(this, ArcgisWFS);

    this.GeoGlobe = GeoGlobe;
  }

  /**
   * 发送ArcgisWFS服务任务，获取指定过滤条件的GeoJSON要素数据集合
   * @param {String} url 服务地址
   * @param {String} typeName 要素图层标识名
   * @param {Object} options 解析服务的参数选项
   */


  (0, _createClass3['default'])(ArcgisWFS, [{
    key: 'queryTask',
    value: function () {
      var _ref = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee(url) {
        var _this = this;

        var typeName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
        var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
        var opts;
        return _regenerator2['default'].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                opts = (0, _assign2['default'])({}, _wfs.defaultQueryOptions, options);
                return _context.abrupt('return', new Promise(function (resolve, reject) {
                  // 获取查询服务实例
                  var wfsQuery = (0, _wfs.getWFSQuery)(_this.GeoGlobe, url, typeName, opts);
                  if (wfsQuery.geometryName === 'GEOMETRY') {
                    wfsQuery.geometryName = 'SHAPE';
                  }
                  // 获取查询过滤条件
                  var filters = (0, _wfs.getQueryFilters)(_this.GeoGlobe, opts, wfsQuery);
                  // 发送查询请求任务
                  var errorMsg = 'Arcgis WFS\u67E5\u8BE2\u670D\u52A1[ ' + url + ' ]\u8981\u7D20\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5[filters]\u8FC7\u6EE4\u6761\u4EF6\u6216\u5C5E\u6027\u4FE1\u606F\u7B49\u662F\u5426\u6709\u8BEF.';
                  var failFun = function failFun() {
                    console.error(errorMsg);
                    reject();
                  };
                  var successFun = function successFun(data) {
                    if (opts.mode !== 'count') {
                      var geojson = data && data.features && data.features.length > 0 ? data.geojson : { type: 'FeatureCollection', features: [] };
                      geojson.features.map(function (f) {
                        f.id = opts.idField ? f.properties[opts.idField].toString() : (0, _hat2['default'])();
                      });
                      resolve(geojson);
                    } else {
                      resolve(data);
                    }
                  };
                  if (wfsQuery) {
                    switch (opts.mode) {
                      case 'count':
                        {
                          (0, _wfs.fetchTotalTask)(GeoGlobe, wfsQuery).then(successFun)['catch'](failFun);
                          break;
                        }
                      default:
                        {
                          wfsQuery.query(filters, successFun, failFun);
                          break;
                        }
                    }
                  } else {
                    failFun();
                  }
                }));

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

      function queryTask(_x3) {
        return _ref.apply(this, arguments);
      }

      return queryTask;
    }()
  }]);
  return ArcgisWFS;
}(); /**
      * @文件说明: Query.ArcgisWFS - Arcgis WFS服务
      * @创建人: pjtools@vip.qq.com
      * @创建时间: 2020-03-28 15:53:18
      */

exports['default'] = ArcgisWFS;