'use strict';

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

var _index = require('../index.js');

var _index2 = _interopRequireDefault(_index);

var _array = require('../array.js');

var _array2 = _interopRequireDefault(_array);

var _gml = require('../format/gml2.js');

var _gml2 = _interopRequireDefault(_gml);

var _xmlfeature = require('../format/xmlfeature.js');

var _xmlfeature2 = _interopRequireDefault(_xmlfeature);

var _obj = require('../obj.js');

var _obj2 = _interopRequireDefault(_obj);

var _xml = require('../xml.js');

var _xml2 = _interopRequireDefault(_xml);

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

/**
 * @classdesc
 * Format for reading WMSGetFeatureInfo format. It uses
 * {@link ol.format.GML2} to read features.
 *
 * @constructor
 * @extends {ol.format.XMLFeature}
 * @param {olx.format.WMSGetFeatureInfoOptions=} opt_options Options.
 * @api
 */
var _ol_format_WMSGetFeatureInfo_ = function (opt_options) {

  var options = opt_options ? opt_options : {};

  /**
   * @private
   * @type {string}
   */
  this.featureNS_ = 'http://mapserver.gis.umn.edu/mapserver';

  /**
   * @private
   * @type {ol.format.GML2}
   */
  this.gmlFormat_ = new _gml2.default();

  /**
   * @private
   * @type {Array.<string>}
   */
  this.layers_ = options.layers ? options.layers : null;

  _xmlfeature2.default.call(this);
};

_index2.default.inherits(_ol_format_WMSGetFeatureInfo_, _xmlfeature2.default);

/**
 * @const
 * @type {string}
 * @private
 */
_ol_format_WMSGetFeatureInfo_.featureIdentifier_ = '_feature';

/**
 * @const
 * @type {string}
 * @private
 */
_ol_format_WMSGetFeatureInfo_.layerIdentifier_ = '_layer';

/**
 * @return {Array.<string>} layers
 */
_ol_format_WMSGetFeatureInfo_.prototype.getLayers = function () {
  return this.layers_;
};

/**
 * @param {Array.<string>} layers Layers to parse.
 */
_ol_format_WMSGetFeatureInfo_.prototype.setLayers = function (layers) {
  this.layers_ = layers;
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @return {Array.<ol.Feature>} Features.
 * @private
 */
_ol_format_WMSGetFeatureInfo_.prototype.readFeatures_ = function (node, objectStack) {
  node.setAttribute('namespaceURI', this.featureNS_);
  var localName = node.localName;
  /** @type {Array.<ol.Feature>} */
  var features = [];
  if (node.childNodes.length === 0) {
    return features;
  }
  if (localName == 'msGMLOutput') {
    for (var i = 0, ii = node.childNodes.length; i < ii; i++) {
      var layer = node.childNodes[i];
      if (layer.nodeType !== Node.ELEMENT_NODE) {
        continue;
      }
      var context = objectStack[0];

      var toRemove = _ol_format_WMSGetFeatureInfo_.layerIdentifier_;
      var layerName = layer.localName.replace(toRemove, '');

      if (this.layers_ && !_array2.default.includes(this.layers_, layerName)) {
        continue;
      }

      var featureType = layerName + _ol_format_WMSGetFeatureInfo_.featureIdentifier_;

      context['featureType'] = featureType;
      context['featureNS'] = this.featureNS_;

      var parsers = {};
      parsers[featureType] = _xml2.default.makeArrayPusher(this.gmlFormat_.readFeatureElement, this.gmlFormat_);
      var parsersNS = _xml2.default.makeStructureNS([context['featureNS'], null], parsers);
      layer.setAttribute('namespaceURI', this.featureNS_);
      var layerFeatures = _xml2.default.pushParseAndPop([], parsersNS, layer, objectStack, this.gmlFormat_);
      if (layerFeatures) {
        _array2.default.extend(features, layerFeatures);
      }
    }
  }
  if (localName == 'FeatureCollection') {
    var gmlFeatures = _xml2.default.pushParseAndPop([], this.gmlFormat_.FEATURE_COLLECTION_PARSERS, node, [{}], this.gmlFormat_);
    if (gmlFeatures) {
      features = gmlFeatures;
    }
  }
  return features;
};

/**
 * Read all features from a WMSGetFeatureInfo response.
 *
 * @function
 * @param {Document|Node|Object|string} source Source.
 * @param {olx.format.ReadOptions=} opt_options Options.
 * @return {Array.<ol.Feature>} Features.
 * @api
 */
_ol_format_WMSGetFeatureInfo_.prototype.readFeatures;

/**
 * @inheritDoc
 */
_ol_format_WMSGetFeatureInfo_.prototype.readFeaturesFromNode = function (node, opt_options) {
  var options = {};
  if (opt_options) {
    _obj2.default.assign(options, this.getReadOptions(node, opt_options));
  }
  return this.readFeatures_(node, [options]);
};

/**
 * Not implemented.
 * @inheritDoc
 */
_ol_format_WMSGetFeatureInfo_.prototype.writeFeatureNode = function (feature, opt_options) {};

/**
 * Not implemented.
 * @inheritDoc
 */
_ol_format_WMSGetFeatureInfo_.prototype.writeFeaturesNode = function (features, opt_options) {};

/**
 * Not implemented.
 * @inheritDoc
 */
_ol_format_WMSGetFeatureInfo_.prototype.writeGeometryNode = function (geometry, opt_options) {};
exports.default = _ol_format_WMSGetFeatureInfo_;
module.exports = exports['default'];