/**
 * 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 defer from '../../utils/defer';

var TILE_URL = {
  img: '//p{s}.map.gtimg.com/sateTiles/{z}/{sx}/{sy}/{x}_{reverseY}.jpg&scene=0',
  terrain: '//p{s}.map.gtimg.com/demTiles/{z}/{sx}/{sy}/{x}_{reverseY}.jpg&scene=0',
  vector: '//rt{s}.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&type=vector&styleid={style}&scene=0'
};

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

    var _Cesium = Cesium,
        Resource = _Cesium.Resource,
        defaultValue = _Cesium.defaultValue,
        Credit = _Cesium.Credit,
        Event = _Cesium.Event;
    this._subdomains = options.subdomains || ['1', '2', '3'];
    this._url = options.url || [options.protocol || '', TILE_URL[options.mapStyle] || TILE_URL.vector].join('');
    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 || 20;
    this._tilingScheme = new Cesium.WebMercatorTilingScheme();
    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._style = options.styleId;
  }

  _createClass(TencentImageryProvider, [{
    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 TencentImageryProvider;
}();

function buildImageResource(x, y, level, request) {
  var url = this._url;
  var subdomains = this._subdomains;
  var reverseY = this.tilingScheme.getNumberOfYTilesAtLevel(level) - y - 1;
  url = url.replace('{s}', subdomains[(x + y + level) % subdomains.length]).replace('{style}', this._style).replace('{x}', x).replace('{y}', -y).replace('{z}', level).replace('{sx}', x >> 4).replace('{sy}', (1 << level) - y >> 4).replace('{reverseY}', reverseY);

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

  return resource;
}

export default TencentImageryProvider;
