/**
 * vue-cesium - https://github.com/zouyaoji/vue-cesium
 * Vue 2.x components for CesiumJS.
 *
 * @package VueCesium
 * @author zouyaoji <370681295@qq.com>
 * @version 2.3.12
 * @license MIT
 * @homepage https://zouyaoji.top/vue-cesium
 * @copyright (c) 2018-2022, zouyaoji <370681295@qq.com>
 */
import _classCallCheck from '@babel/runtime-corejs2/helpers/esm/classCallCheck';
import _createClass from '@babel/runtime-corejs2/helpers/esm/createClass';
import BaiduMapMercatorTilingScheme from './BaiduMapTilingScheme';
import defer from '../../utils/defer';

var BaiduMapImageryProvider = /*#__PURE__*/function () {
  function BaiduMapImageryProvider(options) {
    _classCallCheck(this, BaiduMapImageryProvider);

    var _Cesium = Cesium,
        Resource = _Cesium.Resource,
        defaultValue = _Cesium.defaultValue,
        Credit = _Cesium.Credit,
        Event = _Cesium.Event;
    this._subdomains = defaultValue(options.subdomains, ['0', '1', '2', '3']);

    if (options.url) {
      this._url = options.url;
    } else {
      if (options.mapStyle === 'img') {
        this._url = '//maponline{s}.bdimg.com/starpic/u=x={x};y={y};z={z};v=009;type=sate&qt=satepc&app=webearth2&udt={udt}&fm=46&v=009';
      } else if (options.mapStyle === 'vec') {
        this._url = '//maponline{s}.bdimg.com/tile/?qt={qt}&x={x}&y={y}&z={z}&styles={styles}&scaler={scale}&udt={udt}&from=jsapi2_0';
      } else if (options.mapStyle === 'traffic') {
        this._url = 'https://its.map.baidu.com/traffic/TrafficTileService?time={time}&label={labelStyle}&v=016&level={z}&x={x}&y={y}&scaler={scale}';
      } else {
        this._url = '//api.map.baidu.com/customimage/tile?&x={x}&y={y}&z={z}&udt={udt}&scale={scale}&ak={ak}&customid={mapStyle}';
      }
    }

    var resource = Resource.createIfNeeded(this._url);
    resource.appendForwardSlash();
    this._ready = false;
    this._resource = resource;
    this._tileDiscardPolicy = options.tileDiscardPolicy;
    this._tileWidth = 256;
    this._tileHeight = 256;
    this._minimumLevel = options.minimumLevel || 0;
    this._maximumLevel = options.maximumLevel || 18;
    this._tilingScheme = new BaiduMapMercatorTilingScheme(options);
    this._rectangle = defaultValue(options.rectangle, this._tilingScheme.rectangle);
    var credit = options.credit;

    if (typeof credit === 'string') {
      credit = new Credit(credit);
    }

    this._credit = credit;
    this.enablePickFeatures = defaultValue(options.enablePickFeatures, false);
    this._hasAlphaChannel = defaultValue(options.hasAlphaChannel, true);
    this._errorEvent = new Event();
    this._readyPromise = defer();
    this._ready = true;

    this._readyPromise.resolve(true);

    this._labelStyle = options.labelStyle || 'web2D';
    this._qt = options.qt;
    this._styles = options.styles;
    this._scale = options.scale;
    this._ak = options.ak;
    this._mapStyle = options.mapStyle;
  }

  _createClass(BaiduMapImageryProvider, [{
    key: "url",
    get: function get() {
      return this._resource._url;
    }
  }, {
    key: "proxy",
    get: function get() {
      return this._resource.proxy;
    }
  }, {
    key: "tileWidth",
    get: function get() {
      if (!this._ready) {
        throw new Cesium.DeveloperError('tileWidth must not be called before the imagery provider is ready.');
      }

      return this._tileWidth;
    }
  }, {
    key: "tileHeight",
    get: function get() {
      if (!this._ready) {
        throw new Cesium.DeveloperError('tileHeight must not be called before the imagery provider is ready.');
      }

      return this._tileHeight;
    }
  }, {
    key: "maximumLevel",
    get: function get() {
      if (!this._ready) {
        throw new Cesium.DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
      }

      return this._maximumLevel;
    }
  }, {
    key: "minimumLevel",
    get: function get() {
      if (!this.ready) {
        throw new Cesium.DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
      }

      return this._minimumLevel;
    }
  }, {
    key: "tilingScheme",
    get: function get() {
      if (!this._ready) {
        throw new Cesium.DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
      }

      return this._tilingScheme;
    }
  }, {
    key: "rectangle",
    get: function get() {
      if (!this.ready) {
        throw new Cesium.DeveloperError('rectangle must not be called before the imagery provider is ready.');
      }

      return this._rectangle;
    }
  }, {
    key: "tileDiscardPolicy",
    get: function get() {
      if (!this.ready) {
        throw new Cesium.DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
      }

      return this._tileDiscardPolicy;
    }
  }, {
    key: "errorEvent",
    get: function get() {
      return this._errorEvent;
    }
  }, {
    key: "ready",
    get: function get() {
      return this._ready;
    }
  }, {
    key: "readyPromise",
    get: function get() {
      return this._readyPromise.promise;
    }
  }, {
    key: "credit",
    get: function get() {
      if (!this.ready) {
        throw new Cesium.DeveloperError('credit must not be called before the imagery provider is ready.');
      }

      return this._credit;
    }
  }, {
    key: "hasAlphaChannel",
    get: function get() {
      if (!this.ready) {
        throw new Cesium.DeveloperError('hasAlphaChannel must not be called before the imagery provider is ready.');
      }

      return this._hasAlphaChannel;
    }
  }, {
    key: "getTileCredits",
    value: function getTileCredits(x, y, level) {
      if (!this.ready) {
        throw new Cesium.DeveloperError('getTileCredits must not be called before the imagery provider is ready.');
      }

      return undefined;
    }
  }, {
    key: "requestImage",
    value: function requestImage(x, y, level, request) {
      if (!this.ready) {
        throw new Cesium.DeveloperError('requestImage must not be called before the imagery provider is ready.');
      }

      return Cesium.ImageryProvider.loadImage(this, buildImageResource.call(this, x, y, level, request));
    }
  }, {
    key: "pickFeatures",
    value: function pickFeatures(x, y, level, longitude, latitude) {
      return undefined;
    }
  }]);

  return BaiduMapImageryProvider;
}();

function buildImageResource(x, y, level, request) {
  var url = this._url;
  var subdomains = this._subdomains;
  url = url.replace('{s}', subdomains[(x + y + level) % subdomains.length]).replace('{qt}', this._qt).replace('{x}', x).replace('{y}', -y).replace('{z}', level).replace('{styles}', this._styles).replace('{scale}', this._scale).replace('{mapStyle}', this._mapStyle).replace('{labelStyle}', this._labelStyle).replace('{time}', String(new Date().getTime())).replace('{udt}', String(new Date().getTime()));

  var resource = this._resource.getDerivedResource({
    url: url,
    request: request
  });

  return resource;
}

export default BaiduMapImageryProvider;
