"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCqlFilterByGroup = exports["default"] = void 0;
var _cesium = require("cesium");
var _layer = require("../_util/layer");
var _encryptParams = _interopRequireDefault(require("../_util/encryptParams"));
var _getToken = require("../_util/getToken");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropSet(t, e, o, r, p, f) { return _set(_getPrototypeOf(f ? t.prototype : t), e, o, r, p); }
function set(e, r, t, o) { return set = "undefined" != typeof Reflect && Reflect.set ? Reflect.set : function (e, r, t, o) { var f, i = _superPropBase(e, r); if (i) { if ((f = Object.getOwnPropertyDescriptor(i, r)).set) return f.set.call(o, t), !0; if (!f.writable) return !1; } if (f = Object.getOwnPropertyDescriptor(o, r)) { if (!f.writable) return !1; f.value = t, Object.defineProperty(o, r, f); } else _defineProperty(o, r, t); return !0; }, set(e, r, t, o); }
function _set(e, r, t, o, f) { if (!set(e, r, t, o || e) && f) throw new TypeError("failed to set property"); return t; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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); }
function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var getCqlFilterByGroup = exports.getCqlFilterByGroup = function getCqlFilterByGroup(group) {
  var cql_filter = '';
  if (group !== null && group !== void 0 && group.length) {
    group.map(function (v) {
      if (!v.hasOwnProperty('show') || v.show) {
        cql_filter = cql_filter ? "".concat(cql_filter, " or (").concat(v.filter, ")") : "(".concat(v.filter, ")");
      }
    });
  }
  return cql_filter;
};
var ImageLayer = /*#__PURE__*/function (_ImageryLayer) {
  function ImageLayer(options) {
    var _this;
    _classCallCheck(this, ImageLayer);
    var url = options.url,
      _options$params = options.params,
      params = _options$params === void 0 ? {} : _options$params,
      _options$geoserverPar = options.geoserverParams,
      geoserverParams = _options$geoserverPar === void 0 ? {} : _options$geoserverPar,
      encrypt = options.encrypt,
      key = options.key,
      modalType = options.modalType,
      name = options.name,
      labelField = options.labelField,
      template = options.template,
      autoRefresh = options.autoRefresh,
      style = options.style,
      codeField = options.codeField,
      _options$show = options.show,
      show = _options$show === void 0 ? true : _options$show,
      _options$layerControl = options.layerControl,
      layerControl = _options$layerControl === void 0 ? true : _options$layerControl,
      viewOnly = options.viewOnly,
      legend = options.legend,
      map = options.map,
      group = options.group,
      zIndex = options.zIndex,
      groupLayer = options.groupLayer,
      _options$unclickable = options.unclickable,
      unclickable = _options$unclickable === void 0 ? false : _options$unclickable,
      cancelPopup = options.cancelPopup,
      token = options.token;
    var _params = _objectSpread({}, params);
    if (group !== null && group !== void 0 && group.length) {
      var _group = JSON.parse(JSON.stringify(group));
      if (!show) {
        _group.map(function (f) {
          f.show = false;
        });
      } else {
        _group.map(function (f) {
          var _f$show;
          f.show = (_f$show = f.show) !== null && _f$show !== void 0 ? _f$show : true;
        });
      }
      var _cql_filter = getCqlFilterByGroup(_group);
      if (_cql_filter) {
        _params.cql_filter = _params.cql_filter ? "".concat(_params.cql_filter, " and (").concat(_cql_filter, ")") : _cql_filter;
      } else {
        show = false;
      }
    }
    if (encrypt) {
      _params = (0, _encryptParams["default"])(_params, encrypt);
    }
    var imageryProvider = new _cesium.WebMapServiceImageryProvider(_objectSpread({
      url: (0, _getToken.getResource)(url, {
        token: token
      }),
      enablePickFeatures: !(viewOnly || unclickable),
      getFeatureInfoParameters: _params,
      parameters: _objectSpread({
        service: 'WMS',
        format: 'image/png',
        request: 'GetMap',
        version: '1.1.1',
        transparent: true
      }, _params),
      layers: _params.typeName || _params.QUERY_LAYERS || _params.layers,
      tileWidth: 1024,
      tileHeight: 1024,
      maximumLevel: 18
    }, geoserverParams));
    _this = _callSuper(this, ImageLayer, [imageryProvider, {
      show: show
    }]);

    // 接口地址
    _this.url = url;
    _this.style = style;

    // 加密
    _this.encrypt = encrypt;

    // 图层标识
    _this.key = key;

    // 弹窗类型
    _this.modalType = modalType;

    // 图层名称
    _this.name = name;

    // 设施类型
    _this.codeField = codeField;

    // 图例
    _this.legend = legend;

    // 标签字段
    _this.labelField = labelField;

    // 自动刷新标识
    _this.autoRefresh = autoRefresh;

    // 模版内容
    _this.template = template;

    // 地图
    _this.map = map;

    // 图层组
    _this.groupLayer = groupLayer;

    // 过滤
    _this.group = group;

    // 查询条件
    _this.params = params;

    // 图层层级
    _this.zIndex = zIndex;

    // 图层控制
    _this.layerControl = layerControl;

    // 仅展示
    _this.viewOnly = viewOnly;

    // 是否允许触发点击效果
    _this.unclickable = unclickable;

    // 取消气泡
    _this.cancelPopup = cancelPopup;

    // token
    _this.token = token;
    return _this;
  }

  // 重写父类的属性访问器
  _inherits(ImageLayer, _ImageryLayer);
  return _createClass(ImageLayer, [{
    key: "show",
    get: function get() {
      return _superPropGet(ImageLayer, "show", this, 1);
    },
    set: function set(value) {
      this.setVisible(value);
    }

    // 获取地图
  }, {
    key: "getMap",
    value: function getMap() {
      return this.map;
    }

    // 设置图层显示隐藏
  }, {
    key: "setVisible",
    value: function setVisible(visible) {
      _superPropSet(ImageLayer, "show", visible, this, 1, 1); // 将属性传递给父类

      var map = this.getMap && this.getMap();
      if (map) {
        map.layers.emit('visible', this, visible);
      }
    }

    // 设置过滤条件
  }, {
    key: "setGroup",
    value: function setGroup(group) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      if (group !== null && group !== void 0 && group.length) {
        var params = _objectSpread(_objectSpread({}, this.params), options);
        var filterGroup = group.filter(function (item) {
          return !item.hasOwnProperty('show') || item.show;
        });
        var cql_filter = '';
        filterGroup.map(function (v) {
          if (v.filter) {
            cql_filter = cql_filter ? "".concat(cql_filter, " or ").concat(v.filter) : v.filter;
          }
        });
        if (cql_filter) {
          params.cql_filter = params.cql_filter ? "(".concat(params.cql_filter, ") and (").concat(cql_filter, ")") : cql_filter;
        }
        this.group = group;
        this.updateParams(params);
        this.setVisibleByGroup();
      }
    }

    // 根据过滤条件组显示图层
  }, {
    key: "setVisibleByGroup",
    value: function setVisibleByGroup() {
      var group = this.group;
      if (group !== null && group !== void 0 && group.length) {
        var visible = false;
        group.map(function (v) {
          if (!v.hasOwnProperty('show') || v.show) {
            visible = true;
          }
        });
        this.setVisible(visible);
      }
    }

    // 设置查询条件
  }, {
    key: "updateParams",
    value: function updateParams(params, group) {
      var _imageryProvider$_rel;
      var filter = getCqlFilterByGroup(group);
      var _params = _objectSpread({}, params);
      var cql_filter = '';
      if (filter) {
        cql_filter = params.cql_filter ? "(".concat(params.cql_filter, ") and (").concat(filter, ")") : filter;
      }
      if (cql_filter) {
        _params = _objectSpread(_objectSpread({}, _params), {}, {
          cql_filter: cql_filter
        });
      }
      if (this.encrypt) {
        _params = (0, _encryptParams["default"])(_params, this.encrypt);
      }
      var imageryProvider = this.imageryProvider;
      imageryProvider._pickFeaturesResource.setQueryParameters(_params);
      imageryProvider._resource.setQueryParameters(_params);

      // 真正参数变化需要设置 _tileProvider
      imageryProvider._tileProvider._pickFeaturesResource.setQueryParameters(_params);
      imageryProvider._tileProvider._resource.setQueryParameters(_params);

      // 触发重新请求
      imageryProvider === null || imageryProvider === void 0 || (_imageryProvider$_rel = imageryProvider._reload) === null || _imageryProvider$_rel === void 0 || _imageryProvider$_rel.call(imageryProvider);
    }
  }, {
    key: "getGroupIndex",
    value: function getGroupIndex() {
      return this.groupLayer.indexOf(this);
    }
  }, {
    key: "getMapIndex",
    value: function getMapIndex() {
      var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.key;
      return this.map.layers.getLayerIndex(key);
    }
  }, {
    key: "getFeatureByFieldValue",
    value: function getFeatureByFieldValue(field, value) {
      return (0, _layer.getWFSFeatureInfoByLayer)(this, "".concat(field, " = '").concat(value, "'"));
    }

    // 根据条件获取图形数据，用于地图搜索
  }, {
    key: "getFeaturesBySearch",
    value: function getFeaturesBySearch(field, value) {
      return (0, _layer.getWmsFeatureInfoByText)(this, field, value);
    }

    // 根据条件展示对应的视角
  }, {
    key: "fitViewByFilter",
    value: function fitViewByFilter(filter) {
      var _this2 = this;
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      (0, _layer.getFeaturesInfoByLayer)(this, {
        cql_filter: filter
      }).then(function (geoJson) {
        var _geoJson$features;
        if (geoJson !== null && geoJson !== void 0 && (_geoJson$features = geoJson.features) !== null && _geoJson$features !== void 0 && _geoJson$features.length) {
          _this2.map.fitView({
            geoJson: geoJson,
            options: _objectSpread({
              padding: [3000, 3000]
            }, options)
          });
        }
      });
    }
  }]);
}(_cesium.ImageryLayer);
var _default = exports["default"] = ImageLayer;
//# sourceMappingURL=ImageLayer.js.map
