"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(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var geoJsonFormat = new _format.GeoJSON();

/**
 * 获取图形信息
 * @param layer
 * @returns {*}
 */
var getWFSFeatureInfoByLayer = function getWFSFeatureInfoByLayer(layer, cql_filter) {
  return new Promise(function (resolve) {
    var geoserverParams = layer.get('geoserverParams');
    if (!(geoserverParams !== null && geoserverParams !== void 0 && geoserverParams.geoserverUrl)) {
      resolve([]);
    }
    ;
    var CQL_FILTER;
    if (cql_filter) {
      if (geoserverParams !== null && geoserverParams !== void 0 && geoserverParams.cql_filter) {
        CQL_FILTER = "".concat(geoserverParams.cql_filter, " and ").concat(cql_filter);
      } else {
        CQL_FILTER = cql_filter;
      }
    } else {
      CQL_FILTER = (geoserverParams === null || geoserverParams === void 0 ? void 0 : geoserverParams.cql_filter) || '';
    }
    _http["default"].get(geoserverParams.geoserverUrl, {
      body: _objectSpread(_objectSpread({}, geoserverParams), {}, {
        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.setProperties({
            key: layer === null || layer === void 0 ? void 0 : layer.key,
            label: layer.get('title')
          });
        });
      }
      resolve(features);
    })["catch"](function () {
      resolve([]);
    });
  });
};

/**
 * 获取图形信息
 * @param layers
 * @returns {Promise<FlatArray<Awaited<unknown>[], 2>[]>|*}
 */
exports.getWFSFeatureInfoByLayer = getWFSFeatureInfoByLayer;
var 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 {*}
 */
exports.getWFSFeatureInfoByLayers = getWFSFeatureInfoByLayers;
var 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>[]>|*}
 */
exports.getWMSFeatureInfoByLayer = getWMSFeatureInfoByLayer;
var 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);
  }
};
exports.getWMSFeatureInfoByLayers = getWMSFeatureInfoByLayers;
//# sourceMappingURL=layer.js.map
