import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
/**
 * @文件说明: Query.ArcgisWFS - Arcgis WFS服务
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-28 15:53:18
 */

import assign from 'lodash/assign';
import hat from 'hat';
import { defaultQueryOptions, getWFSQuery, getQueryFilters, fetchTotalTask } from './wfs';

var ArcgisWFS = function () {
  function ArcgisWFS(GeoGlobe) {
    _classCallCheck(this, ArcgisWFS);

    this.GeoGlobe = GeoGlobe;
  }

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


  _createClass(ArcgisWFS, [{
    key: 'queryTask',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.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 _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                opts = assign({}, defaultQueryOptions, options);
                return _context.abrupt('return', new Promise(function (resolve, reject) {
                  // 获取查询服务实例
                  var wfsQuery = getWFSQuery(_this.GeoGlobe, url, typeName, opts);
                  if (wfsQuery.geometryName === 'GEOMETRY') {
                    wfsQuery.geometryName = 'SHAPE';
                  }
                  // 获取查询过滤条件
                  var filters = 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() : hat();
                      });
                      resolve(geojson);
                    } else {
                      resolve(data);
                    }
                  };
                  if (wfsQuery) {
                    switch (opts.mode) {
                      case 'count':
                        {
                          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;
}();

export default ArcgisWFS;