"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getWMSFeatureInfoByLayers = exports.getWMSFeatureInfoByLayer = exports.getWFSFeatureInfoByLayers = exports.getWFSFeatureInfoByLayer = void 0;
var _format = require("ol/format");
var _http = _interopRequireDefault(require("../_util/http"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); }
var geoJsonFormat = new _format.GeoJSON();

/**
 * 获取图形信息
 * @param layer 图层
 * @param cql_filter 条件
 * @returns {*} Promise
 */
var getWFSFeatureInfoByLayer = exports.getWFSFeatureInfoByLayer = function getWFSFeatureInfoByLayer(layer, cql_filter) {
  return new Promise(function (resolve) {
    var url = layer.url;
    var params = layer.params;
    if (!url) {
      resolve([]);
    }
    var CQL_FILTER;
    if (cql_filter) {
      if (params !== null && params !== void 0 && params.cql_filter) {
        CQL_FILTER = "".concat(params.cql_filter, " and ").concat(cql_filter);
      } else {
        CQL_FILTER = cql_filter;
      }
    } else {
      CQL_FILTER = (params === null || params === void 0 ? void 0 : params.cql_filter) || '';
    }
    _http["default"].get(url, {
      body: _objectSpread(_objectSpread({}, params), {}, {
        cql_filter: CQL_FILTER
      })
    }).then(function (response) {
      var _response$features;
      var features = [];
      if (response !== null && response !== void 0 && (_response$features = response.features) !== null && _response$features !== void 0 && _response$features.length) {
        features = geoJsonFormat.readFeatures(response);
        features.map(function (item) {
          item.key = layer === null || layer === void 0 ? void 0 : layer.key;
        });
      }
      resolve(features);
    })["catch"](function () {
      resolve([]);
    });
  });
};

/**
 * 获取图形信息
 * @param layers
 * @returns {Promise<FlatArray<Awaited<unknown>[], 2>[]>|*}
 */
var getWFSFeatureInfoByLayers = exports.getWFSFeatureInfoByLayers = function getWFSFeatureInfoByLayers(layers, cql_filter) {
  if (Array.isArray(layers)) {
    var req = [];
    layers.map(function (v) {
      req.push(getWFSFeatureInfoByLayer(v, cql_filter));
    });
    return Promise.all(req).then(function (layers) {
      return layers.flat(2);
    });
  } else {
    return getWFSFeatureInfoByLayer(layers, cql_filter);
  }
};

/**
 * 获取图形信息
 * @param event
 * @param map
 * @param layer
 * @returns {*}
 */
var getWMSFeatureInfoByLayer = exports.getWMSFeatureInfoByLayer = function getWMSFeatureInfoByLayer(event, map, layer) {
  var source = layer.getSource();
  var view = map.getView();
  var url = source.getFeatureInfoUrl(event.coordinate, view.getResolution(), view.getProjection(), {
    cql_filter: source.params_.cql_filter,
    QUERY_LAYERS: source.params_.layers,
    INFO_FORMAT: 'application/json',
    FEATURE_COUNT: 50
  });
  return fetch(url).then(function (res) {
    return res.json();
  }).then(function (data) {
    var _data$features;
    var features = [];
    if (data !== null && data !== void 0 && (_data$features = data.features) !== null && _data$features !== void 0 && _data$features.length) {
      features = geoJsonFormat.readFeatures(data);
      features.map(function (item) {
        item.key = layer === null || layer === void 0 ? void 0 : layer.key;
      });
    }
    return features;
  })["catch"](function () {
    return [];
  });
};

/**
 * 获取图形信息
 * @param event
 * @param map
 * @param layers
 * @returns {Promise<FlatArray<Awaited<unknown>[], 2>[]>|*}
 */
var getWMSFeatureInfoByLayers = exports.getWMSFeatureInfoByLayers = function getWMSFeatureInfoByLayers(event, map, layers) {
  if (Array.isArray(layers)) {
    var req = [];
    layers.reverse().map(function (v) {
      req.push(getWMSFeatureInfoByLayer(event, map, v));
    });
    return Promise.all(req).then(function (layers) {
      return layers.flat(2);
    });
  } else {
    return getWMSFeatureInfoByLayer(event, map, layers);
  }
};
//# sourceMappingURL=layer.js.map
